Class BadgeService

java.lang.Object
fr.utc.miage.sporttrack.service.event.BadgeService

@Service public class BadgeService extends Object
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 Details

    • BadgeService

      public BadgeService(BadgeRepository badgeRepository, ActivityRepository activityRepository)
      Constructs a BadgeService without notification support.
      Parameters:
      badgeRepository - the badge repository
      activityRepository - the activity repository
    • BadgeService

      @Autowired public BadgeService(BadgeRepository badgeRepository, ActivityRepository activityRepository, NotificationService notificationService)
      Constructs a BadgeService with full notification support.
      Parameters:
      badgeRepository - the badge repository
      activityRepository - the activity repository
      notificationService - the notification service for badge-earned events
  • Method Details

    • saveBadge

      public void saveBadge(Badge badge, Sport sport)
      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 save
      sport - the sport to associate with the badge, or null for universal badges
      Throws:
      IllegalArgumentException - if the badge is null
    • findAll

      public List<Badge> findAll()
      Returns all badges in the database.
      Returns:
      a list of all badges
    • findById

      public Badge findById(int id)
      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

      public List<Badge> getEarnedBadges(Integer athleteId)
      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

      public List<Badge> getUnearnedBadges(Integer athleteId)
      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

      public List<Badge> getRecentBadges(Integer athleteId, int limit)
      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 athlete
      limit - the maximum number of badges to return
      Returns:
      a list of up to limit recent badges
    • checkAndAwardBadges

      public void checkAndAwardBadges(Activity activity)
      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