Class ActivityService

java.lang.Object
fr.utc.miage.sporttrack.service.activity.ActivityService

@Service public class ActivityService extends Object
Service layer component responsible for managing Activity entities within the SportTrack application.

Provides business logic for creating, updating, deleting, and querying activities. Each mutation triggers recalculation of impacted challenge rankings and, where applicable, sends notifications to friends.

Author:
SportTrack Team
  • Constructor Details

    • ActivityService

      public ActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService)
      Constructs an ActivityService without friendship/notification support.
      Parameters:
      activityRepository - the activity repository
      sportRepository - the sport repository
      weatherReportRepository - the weather report repository
      challengeRankingService - the challenge ranking service
    • ActivityService

      @Autowired public ActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService, FriendshipService friendshipService, NotificationService notificationService)
      Constructs an ActivityService with full friendship and notification support.
      Parameters:
      activityRepository - the activity repository
      sportRepository - the sport repository
      weatherReportRepository - the weather report repository
      challengeRankingService - the challenge ranking service
      friendshipService - the friendship service for resolving friends
      notificationService - the notification service for pushing activity events
  • Method Details

    • findAll

      public List<Activity> findAll()
      Returns all activities in the database.
      Returns:
      a list of all activities
    • findAllByAthlete

      public List<Activity> findAllByAthlete(Athlete athlete)
      Returns all activities created by the specified athlete, ordered newest first.
      Parameters:
      athlete - the athlete whose activities should be retrieved
      Returns:
      a list of activities ordered by date and start time descending
      Throws:
      IllegalArgumentException - if the athlete or their identifier is null
    • findAllByAthleteIds

      public List<Activity> findAllByAthleteIds(List<Integer> athleteIds)
      Returns all activities created by any of the specified athlete identifiers.
      Parameters:
      athleteIds - the list of athlete identifiers; if null or empty, an empty list is returned
      Returns:
      a list of matching activities ordered newest first
    • findById

      public Optional<Activity> findById(int id)
      Finds an activity by its unique identifier.
      Parameters:
      id - the activity identifier
      Returns:
      an Optional containing the activity if found
    • findByIdForAthlete

      public Optional<Activity> findByIdForAthlete(int id, Athlete athlete)
      Finds an activity by identifier, restricted to those owned by the specified athlete.
      Parameters:
      id - the activity identifier
      athlete - the athlete who must own the activity
      Returns:
      an Optional containing the activity if found and owned by the athlete
      Throws:
      IllegalArgumentException - if the athlete or their identifier is null
    • createActivityForAthlete

      public Activity createActivityForAthlete(Athlete athlete, double duration, String title, String description, int repetition, double distance, LocalDate dateA, LocalTime startTime, String locationCity, int sportId)
      Creates and persists a new activity for the specified athlete with full validation. After saving, notifications are sent to friends and impacted challenge rankings are recomputed.
      Parameters:
      athlete - the athlete who is creating the activity
      duration - the duration of the activity in hours
      title - the user-defined title of the activity
      description - an optional description
      repetition - the repetition count (for repetition-based sports)
      distance - the distance in kilometres (for distance-based sports)
      dateA - the date the activity took place
      startTime - the local start time of the activity
      locationCity - the city or location name
      sportId - the identifier of the associated sport
      Returns:
      the newly created and persisted Activity
      Throws:
      IllegalArgumentException - if any validation fails
    • updateActivity

      public Activity updateActivity(int id, double duration, String title, String description, int repetition, double distance, LocalDate dateA, LocalTime startTime, String locationCity, int sportId)
      Updates an existing activity by its identifier with full validation. Both old and new challenge rankings are recomputed after the update.
      Parameters:
      id - the activity identifier to update
      duration - the new duration in hours
      title - the new title
      description - the new description
      repetition - the new repetition count
      distance - the new distance in kilometres
      dateA - the new activity date
      startTime - the new start time
      locationCity - the new location city
      sportId - the new sport identifier
      Returns:
      the updated and persisted Activity
      Throws:
      IllegalArgumentException - if the activity is not found or validation fails
    • updateActivityForAthlete

      public Activity updateActivityForAthlete(Athlete athlete, int id, double duration, String title, String description, int repetition, double distance, LocalDate dateA, LocalTime startTime, String locationCity, int sportId)
      Updates an activity owned by the specified athlete with full validation. Both old and new challenge rankings are recomputed after the update.
      Parameters:
      athlete - the athlete who must own the activity
      id - the activity identifier
      duration - the new duration in hours
      title - the new title
      description - the new description
      repetition - the new repetition count
      distance - the new distance in kilometres
      dateA - the new activity date
      startTime - the new start time
      locationCity - the new location city
      sportId - the new sport identifier
      Returns:
      the updated and persisted Activity
      Throws:
      IllegalArgumentException - if the athlete is invalid, the activity is not found, or validation fails
    • deleteById

      @Transactional public void deleteById(int id)
      Deletes an activity by its identifier, including its associated weather report. Impacted challenge rankings are recomputed after deletion.
      Parameters:
      id - the activity identifier to delete
      Throws:
      IllegalArgumentException - if no activity is found with the given identifier
    • deleteByIdForAthlete

      @Transactional public void deleteByIdForAthlete(Athlete athlete, int id)
      Deletes an activity owned by the specified athlete, including its associated weather report. Impacted challenge rankings are recomputed after deletion.
      Parameters:
      athlete - the athlete who must own the activity
      id - the activity identifier to delete
      Throws:
      IllegalArgumentException - if the athlete is invalid or the activity is not found
    • filterBySport

      public boolean filterBySport(Activity activity, Sport selectedSport)
      Filters an activity by matching its associated sport against a selected sport.
      Parameters:
      activity - the activity to test
      selectedSport - the sport to match against; if null, all activities pass
      Returns:
      true if the activity matches the selected sport, false otherwise
    • filterByDate

      public boolean filterByDate(Activity activity, LocalDate startDate, LocalDate endDate)
      Filters an activity by checking whether its date falls within the specified range.
      Parameters:
      activity - the activity to test
      startDate - the inclusive start date of the range; if null, no lower bound is applied
      endDate - the inclusive end date of the range; if null, no upper bound is applied
      Returns:
      true if the activity date is within the range, false otherwise