Finished and tested
This commit is contained in:
@@ -27,14 +27,31 @@ public class UtilisateurResource {
|
||||
private final UtilisateurMapper mapper = UtilisateurMapper.INSTANCE;
|
||||
|
||||
@GET
|
||||
public List<Utilisateur> listUtilisateur() {
|
||||
@Operation(summary = "List all users", tags = {"Utilisateurs"},
|
||||
description = "Get a list of all users in BDD",
|
||||
responses = {@ApiResponse(description = "List of users", content = @Content(
|
||||
schema = @Schema(implementation = Utilisateur.class)
|
||||
))
|
||||
}
|
||||
)
|
||||
public List<UtilisateurDTO> listUtilisateur() {
|
||||
List<Utilisateur> utilisateurs = utilisateurDAO.findAll();
|
||||
return utilisateurs;
|
||||
return mapper.toDTOs(utilisateurs);
|
||||
}
|
||||
|
||||
//https://stackoverflow.com/questions/9269040/which-http-response-code-for-this-email-is-already-registered
|
||||
@POST
|
||||
@Path("/register")
|
||||
public Response registerUtilisateur(UtilisateurDTO dto) {
|
||||
@Operation(summary = "Register a new user",
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Registers a new user.",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "201", description = "Registration succès"),
|
||||
@ApiResponse(responseCode = "409", description = "Email est déjà registré")
|
||||
}
|
||||
)
|
||||
public Response registerUtilisateur(
|
||||
@Parameter(description = "User details for registration", required = true) UtilisateurDTO dto) {
|
||||
String email_verification = dto.getEmail();
|
||||
Utilisateur existing = utilisateurDAO.findByEmail(email_verification);
|
||||
|
||||
@@ -51,7 +68,19 @@ public class UtilisateurResource {
|
||||
|
||||
@POST
|
||||
@Path("/login")
|
||||
public Response loginUtilisateur(UtilisateurDTO dto) {
|
||||
@Operation(summary = "Log in a user",
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Authenticates a user and returns the user's details upon success.",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200", description = "Successful login", content = @Content(
|
||||
schema = @Schema(implementation = UtilisateurDTO.class)
|
||||
)),
|
||||
@ApiResponse(responseCode = "404", description = "Email n'existe pas"),
|
||||
@ApiResponse(responseCode = "401", description = "Mauvais mdp")
|
||||
}
|
||||
)
|
||||
public Response loginUtilisateur(
|
||||
@Parameter(description = "User credentials (email and password)", required = true) UtilisateurDTO dto) {
|
||||
Utilisateur utilisateur = utilisateurDAO.findByEmail(dto.getEmail());
|
||||
|
||||
if (utilisateur == null) {
|
||||
@@ -67,7 +96,7 @@ public class UtilisateurResource {
|
||||
@GET
|
||||
@Path("/{id}")
|
||||
@Operation(summary = "Get user by ID",
|
||||
tags = {"utilisateur"},
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Get User by ID",
|
||||
responses = {
|
||||
@ApiResponse(description = "Utilisateur", content = @Content(
|
||||
@@ -95,7 +124,20 @@ public class UtilisateurResource {
|
||||
|
||||
@PUT
|
||||
@Path("/{user_id}/add_session/{session_id}")
|
||||
public Response addSession(@PathParam("user_id") Integer user_id, @PathParam("session_id") Integer session_id) {
|
||||
@Operation(summary = "Add a session to a user",
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Add a session to a user existed in BDD",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200", description = "Session added successfully", content = @Content(
|
||||
schema = @Schema(implementation = UtilisateurDTO.class) // Returns updated user DTO
|
||||
)),
|
||||
@ApiResponse(responseCode = "404", description = "User or Session not found"),
|
||||
@ApiResponse(responseCode = "409", description = "User is already in the session")
|
||||
}
|
||||
)
|
||||
public Response addSession(
|
||||
@Parameter(description = "ID of the user") @PathParam("user_id") Integer user_id,
|
||||
@Parameter(description = "ID of the session to add") @PathParam("session_id") Integer session_id) {
|
||||
SessionDAO sessionDAO = new SessionDAO();
|
||||
|
||||
Session existingSession = sessionDAO.findById(session_id);
|
||||
@@ -119,7 +161,19 @@ public class UtilisateurResource {
|
||||
|
||||
@GET
|
||||
@Path("{user_id}/session")
|
||||
public Response listSession(@PathParam("user_id") Integer user_id) {
|
||||
@Operation(summary = "List of all sessions of a user",
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Return a response of all sessions of a user",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200", description = "List of user's sessions", content = @Content(
|
||||
schema = @Schema(implementation = SessionDTO.class)
|
||||
)),
|
||||
@ApiResponse(responseCode = "404", description = "User not found")
|
||||
}
|
||||
)
|
||||
public Response listSession(
|
||||
@Parameter(description = "ID of the user") @PathParam("user_id") Integer user_id) {
|
||||
|
||||
Utilisateur utilisateur = utilisateurDAO.findById(user_id);
|
||||
if (utilisateur == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
@@ -128,10 +182,18 @@ public class UtilisateurResource {
|
||||
List<SessionDTO> dtos = SessionMapper.INSTANCE.toDTOs(sess);
|
||||
return Response.status(Response.Status.OK).entity(dtos).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{user_id}/delete")
|
||||
public Response deleteUtilisateur(@PathParam("user_id") Integer user_id) {
|
||||
@Operation(summary = "Delete a user",
|
||||
tags = {"Utilisateurs"},
|
||||
description = "Delete a user with ID",
|
||||
responses = {
|
||||
@ApiResponse(responseCode = "200", description = "User deleted"),
|
||||
@ApiResponse(responseCode = "404", description = "User not found")
|
||||
}
|
||||
)
|
||||
public Response deleteUtilisateur(
|
||||
@Parameter(description = "ID of the user to delete") @PathParam("user_id") Integer user_id) {
|
||||
Utilisateur existing = utilisateurDAO.findById(user_id);
|
||||
if (existing == null) {
|
||||
return Response.status(Response.Status.NOT_FOUND).build();
|
||||
|
||||
Reference in New Issue
Block a user