Class ActivityService
java.lang.Object
fr.utc.miage.sporttrack.service.activity.ActivityService
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 Summary
ConstructorsConstructorDescriptionActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService) Constructs anActivityServicewithout friendship/notification support.ActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService, FriendshipService friendshipService, NotificationService notificationService) Constructs anActivityServicewith full friendship and notification support. -
Method Summary
Modifier and TypeMethodDescriptioncreateActivityForAthlete(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.voiddeleteById(int id) Deletes an activity by its identifier, including its associated weather report.voiddeleteByIdForAthlete(Athlete athlete, int id) Deletes an activity owned by the specified athlete, including its associated weather report.booleanfilterByDate(Activity activity, LocalDate startDate, LocalDate endDate) Filters an activity by checking whether its date falls within the specified range.booleanfilterBySport(Activity activity, Sport selectedSport) Filters an activity by matching its associated sport against a selected sport.findAll()Returns all activities in the database.findAllByAthlete(Athlete athlete) Returns all activities created by the specified athlete, ordered newest first.findAllByAthleteIds(List<Integer> athleteIds) Returns all activities created by any of the specified athlete identifiers.findById(int id) Finds an activity by its unique identifier.findByIdForAthlete(int id, Athlete athlete) Finds an activity by identifier, restricted to those owned by the specified athlete.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.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.
-
Constructor Details
-
ActivityService
public ActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService) Constructs anActivityServicewithout friendship/notification support.- Parameters:
activityRepository- the activity repositorysportRepository- the sport repositoryweatherReportRepository- the weather report repositorychallengeRankingService- the challenge ranking service
-
ActivityService
@Autowired public ActivityService(ActivityRepository activityRepository, SportRepository sportRepository, WeatherReportRepository weatherReportRepository, ChallengeRankingService challengeRankingService, FriendshipService friendshipService, NotificationService notificationService) Constructs anActivityServicewith full friendship and notification support.- Parameters:
activityRepository- the activity repositorysportRepository- the sport repositoryweatherReportRepository- the weather report repositorychallengeRankingService- the challenge ranking servicefriendshipService- the friendship service for resolving friendsnotificationService- the notification service for pushing activity events
-
-
Method Details
-
findAll
Returns all activities in the database.- Returns:
- a list of all activities
-
findAllByAthlete
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
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
Finds an activity by its unique identifier.- Parameters:
id- the activity identifier- Returns:
- an
Optionalcontaining the activity if found
-
findByIdForAthlete
Finds an activity by identifier, restricted to those owned by the specified athlete.- Parameters:
id- the activity identifierathlete- the athlete who must own the activity- Returns:
- an
Optionalcontaining 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 activityduration- the duration of the activity in hourstitle- the user-defined title of the activitydescription- an optional descriptionrepetition- the repetition count (for repetition-based sports)distance- the distance in kilometres (for distance-based sports)dateA- the date the activity took placestartTime- the local start time of the activitylocationCity- the city or location namesportId- 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 updateduration- the new duration in hourstitle- the new titledescription- the new descriptionrepetition- the new repetition countdistance- the new distance in kilometresdateA- the new activity datestartTime- the new start timelocationCity- the new location citysportId- 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 activityid- the activity identifierduration- the new duration in hourstitle- the new titledescription- the new descriptionrepetition- the new repetition countdistance- the new distance in kilometresdateA- the new activity datestartTime- the new start timelocationCity- the new location citysportId- 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
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 activityid- the activity identifier to delete- Throws:
IllegalArgumentException- if the athlete is invalid or the activity is not found
-
filterBySport
Filters an activity by matching its associated sport against a selected sport.- Parameters:
activity- the activity to testselectedSport- the sport to match against; ifnull, all activities pass- Returns:
trueif the activity matches the selected sport,falseotherwise
-
filterByDate
Filters an activity by checking whether its date falls within the specified range.- Parameters:
activity- the activity to teststartDate- the inclusive start date of the range; ifnull, no lower bound is appliedendDate- the inclusive end date of the range; ifnull, no upper bound is applied- Returns:
trueif the activity date is within the range,falseotherwise
-