Class BadgeService
java.lang.Object
fr.utc.miage.sporttrack.service.event.BadgeService
Service layer component responsible for managing
Badge entities
within the SportTrack application.
Provides business logic for badge CRUD operations, querying earned and unearned badges per athlete, and automatically awarding badges based on cumulative activity metrics.
- Author:
- SportTrack Team
-
Constructor Summary
ConstructorsConstructorDescriptionBadgeService(BadgeRepository badgeRepository, ActivityRepository activityRepository) Constructs aBadgeServicewithout notification support.BadgeService(BadgeRepository badgeRepository, ActivityRepository activityRepository, NotificationService notificationService) Constructs aBadgeServicewith full notification support. -
Method Summary
Modifier and TypeMethodDescriptionvoidcheckAndAwardBadges(Activity activity) Checks and awards badges after an activity is created.voiddeleteById(int id) Deletes the badge with the given identifier.findAll()Returns all badges in the database.findById(int id) Finds a badge by its unique identifier.getEarnedBadges(Integer athleteId) Returns all badges earned by the specified athlete.getRecentBadges(Integer athleteId, int limit) Returns the N most recently earned badges for the specified athlete.getUnearnedBadges(Integer athleteId) Returns all badges not yet earned by the specified athlete.voidSaves a badge entity after associating it with the given sport.
-
Constructor Details
-
BadgeService
Constructs aBadgeServicewithout notification support.- Parameters:
badgeRepository- the badge repositoryactivityRepository- the activity repository
-
BadgeService
@Autowired public BadgeService(BadgeRepository badgeRepository, ActivityRepository activityRepository, NotificationService notificationService) Constructs aBadgeServicewith full notification support.- Parameters:
badgeRepository- the badge repositoryactivityRepository- the activity repositorynotificationService- the notification service for badge-earned events
-
-
Method Details
-
saveBadge
Saves a badge entity after associating it with the given sport. Initialises the earned-by list if it is null.- Parameters:
badge- the badge entity to savesport- the sport to associate with the badge, ornullfor universal badges- Throws:
IllegalArgumentException- if the badge is null
-
findAll
Returns all badges in the database.- Returns:
- a list of all badges
-
findById
Finds a badge by its unique identifier.- Parameters:
id- the badge identifier- Returns:
- the matching
Badge - Throws:
IllegalArgumentException- if no badge is found
-
deleteById
public void deleteById(int id) Deletes the badge with the given identifier.- Parameters:
id- the badge identifier to delete- Throws:
IllegalArgumentException- if no badge is found with the given identifier
-
getEarnedBadges
Returns all badges earned by the specified athlete.- Parameters:
athleteId- the unique identifier of the athlete- Returns:
- a list of badges earned by the athlete
-
getUnearnedBadges
Returns all badges not yet earned by the specified athlete.- Parameters:
athleteId- the unique identifier of the athlete- Returns:
- a list of badges the athlete has not yet earned
-
getRecentBadges
Returns the N most recently earned badges for the specified athlete. Since earned-at timestamps are not tracked, the last N from the list are returned.- Parameters:
athleteId- the unique identifier of the athletelimit- the maximum number of badges to return- Returns:
- a list of up to
limitrecent badges
-
checkAndAwardBadges
Checks and awards badges after an activity is created. Evaluates both sport-specific badges and universal badges (with no sport filter).- Parameters:
activity- the newly created activity that may trigger badge awards
-