Class SportService

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

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

Provides business logic for creating, updating, enabling, disabling, and querying sports. Input validation is performed before persistence.

Author:
SportTrack Team
  • Constructor Details

    • SportService

      public SportService(SportRepository repository)
      Constructs a new SportService with the given repository.
      Parameters:
      repository - the sport repository for data access
  • Method Details

    • findAll

      public List<Sport> findAll()
      Returns all sports, both active and inactive.
      Returns:
      a list of all sports
    • findAllActive

      public List<Sport> findAllActive()
      Returns only active sports.
      Returns:
      a list of sports where the active flag is true
    • findById

      public Optional<Sport> findById(int id)
      Finds a sport by its unique identifier.
      Parameters:
      id - the sport identifier
      Returns:
      an Optional containing the sport if found, empty otherwise
    • createSport

      public Sport createSport(String name, String description, double caloriesPerHour, SportType type)
      Creates and persists a new sport with the provided attributes.
      Parameters:
      name - the display name of the sport
      description - a textual description of the sport
      caloriesPerHour - the average calories burned per hour
      type - the SportType defining the measurement method
      Returns:
      the newly created and persisted Sport
      Throws:
      IllegalArgumentException - if the name is empty or calories per hour is not positive
    • updateSport

      public Sport updateSport(int id, String name, String description, double caloriesPerHour, SportType type)
      Updates an existing sport with the provided attributes.
      Parameters:
      id - the identifier of the sport to update
      name - the new display name
      description - the new textual description
      caloriesPerHour - the new average calories burned per hour
      type - the new SportType
      Returns:
      the updated and persisted Sport
      Throws:
      IllegalArgumentException - if the sport is not found, the name is empty, or calories per hour is not positive
    • disableSport

      public void disableSport(int id)
      Disables a sport by setting its active flag to false.
      Parameters:
      id - the identifier of the sport to disable
      Throws:
      IllegalArgumentException - if no sport is found with the given identifier
    • enableSport

      public void enableSport(int id)
      Enables a sport by setting its active flag to true.
      Parameters:
      id - the identifier of the sport to enable
      Throws:
      IllegalArgumentException - if no sport is found with the given identifier
    • safeSportName

      public String safeSportName(Sport sport)
      Returns a safe display name for the given sport, defaulting to "Autre" if the sport or its name is null or blank.
      Parameters:
      sport - the sport whose name should be resolved
      Returns:
      the sport name, or "Autre" as a fallback