Class SportService
java.lang.Object
fr.utc.miage.sporttrack.service.activity.SportService
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 Summary
ConstructorsConstructorDescriptionSportService(SportRepository repository) Constructs a newSportServicewith the given repository. -
Method Summary
Modifier and TypeMethodDescriptioncreateSport(String name, String description, double caloriesPerHour, SportType type) Creates and persists a new sport with the provided attributes.voiddisableSport(int id) Disables a sport by setting its active flag tofalse.voidenableSport(int id) Enables a sport by setting its active flag totrue.findAll()Returns all sports, both active and inactive.Returns only active sports.findById(int id) Finds a sport by its unique identifier.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.updateSport(int id, String name, String description, double caloriesPerHour, SportType type) Updates an existing sport with the provided attributes.
-
Constructor Details
-
SportService
Constructs a newSportServicewith the given repository.- Parameters:
repository- the sport repository for data access
-
-
Method Details
-
findAll
Returns all sports, both active and inactive.- Returns:
- a list of all sports
-
findAllActive
Returns only active sports.- Returns:
- a list of sports where the active flag is
true
-
findById
Finds a sport by its unique identifier.- Parameters:
id- the sport identifier- Returns:
- an
Optionalcontaining the sport if found, empty otherwise
-
createSport
Creates and persists a new sport with the provided attributes.- Parameters:
name- the display name of the sportdescription- a textual description of the sportcaloriesPerHour- the average calories burned per hourtype- theSportTypedefining 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 updatename- the new display namedescription- the new textual descriptioncaloriesPerHour- the new average calories burned per hourtype- the newSportType- 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 tofalse.- 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 totrue.- Parameters:
id- the identifier of the sport to enable- Throws:
IllegalArgumentException- if no sport is found with the given identifier
-
safeSportName
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
-