205 lines
8.8 KiB
Java
205 lines
8.8 KiB
Java
package fr.istic.taa.jaxrs.rest;
|
|
|
|
import fr.istic.taa.jaxrs.DAO.SessionDAO;
|
|
import fr.istic.taa.jaxrs.DAO.UtilisateurDAO;
|
|
import fr.istic.taa.jaxrs.DTO.SessionDTO;
|
|
import fr.istic.taa.jaxrs.DTO.UtilisateurDTO;
|
|
import fr.istic.taa.jaxrs.Mapper.SessionMapper;
|
|
import fr.istic.taa.jaxrs.Mapper.UtilisateurMapper;
|
|
import fr.istic.taa.jaxrs.metier.Question;
|
|
import fr.istic.taa.jaxrs.metier.Session;
|
|
import fr.istic.taa.jaxrs.metier.Utilisateur;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
import io.swagger.v3.oas.annotations.media.Content;
|
|
import io.swagger.v3.oas.annotations.media.Schema;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
import jakarta.ws.rs.*;
|
|
import jakarta.ws.rs.core.Response;
|
|
|
|
import java.util.List;
|
|
|
|
@Path("utilisateur")
|
|
@Consumes({"application/json", "application/xml"})
|
|
@Produces({"application/json", "application/xml"})
|
|
public class UtilisateurResource {
|
|
private final UtilisateurDAO utilisateurDAO = new UtilisateurDAO();
|
|
private final UtilisateurMapper mapper = UtilisateurMapper.INSTANCE;
|
|
|
|
@GET
|
|
@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 mapper.toDTOs(utilisateurs);
|
|
}
|
|
|
|
//https://stackoverflow.com/questions/9269040/which-http-response-code-for-this-email-is-already-registered
|
|
@POST
|
|
@Path("/register")
|
|
@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);
|
|
|
|
//VERIFACTION S'IL EXISTE DANS BDD
|
|
if (existing != null) {
|
|
return Response.status(Response.Status.CONFLICT).entity("Email est déjà registré").build();
|
|
}
|
|
|
|
Utilisateur utilisateur = mapper.toEntity(dto);
|
|
utilisateurDAO.create(utilisateur);
|
|
|
|
return Response.status(Response.Status.CREATED).entity("Registration succès").build();
|
|
}
|
|
|
|
@POST
|
|
@Path("/login")
|
|
@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) {
|
|
return Response.status(Response.Status.NOT_FOUND).entity("Email n'existe pas").build();
|
|
} else if (!utilisateur.getPassword().equals(dto.getPassword())) {
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity("Mauvais mdp").build();
|
|
}
|
|
UtilisateurDTO result = mapper.toDTO(utilisateur);
|
|
|
|
return Response.status(Response.Status.OK).entity(result).build();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{id}")
|
|
@Operation(summary = "Get user by ID",
|
|
tags = {"Utilisateurs"},
|
|
description = "Get User by ID",
|
|
responses = {
|
|
@ApiResponse(description = "Utilisateur", content = @Content(
|
|
schema = @Schema(implementation = Utilisateur.class)
|
|
)),
|
|
@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
|
|
@ApiResponse(responseCode = "404", description = "User not found")
|
|
})
|
|
public Response getUtilisateur(@Parameter(
|
|
description = "ID of user that needs to be fetched",
|
|
schema = @Schema(
|
|
type = "integer",
|
|
format = "int32",
|
|
description = "param ID of user that needs to be fetched"
|
|
),
|
|
required = true)
|
|
@PathParam("id") Integer id) {
|
|
Utilisateur utilisateur = utilisateurDAO.findById(id);
|
|
if (utilisateur == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
UtilisateurDTO result = mapper.toDTO(utilisateur);
|
|
return Response.ok(result).build();
|
|
}
|
|
|
|
@PUT
|
|
@Path("/{user_id}/add_session/{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);
|
|
Utilisateur utilisateur = utilisateurDAO.findById(user_id);
|
|
|
|
if (existingSession == null || utilisateur == null) {
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
}
|
|
//If user already joined
|
|
if (utilisateur.getSession().contains(existingSession)) {
|
|
return Response.status(Response.Status.CONFLICT).build();
|
|
}
|
|
utilisateurDAO.addToSession(user_id, session_id);
|
|
utilisateurDAO.update(utilisateur);
|
|
|
|
// We update it so have to return new DTO
|
|
UtilisateurDTO dto = mapper.toDTO(utilisateur);
|
|
|
|
return Response.status(Response.Status.OK).entity(dto).build();
|
|
}
|
|
|
|
@GET
|
|
@Path("{user_id}/session")
|
|
@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();
|
|
}
|
|
List<Session> sess = utilisateur.getSession();
|
|
List<SessionDTO> dtos = SessionMapper.INSTANCE.toDTOs(sess);
|
|
return Response.status(Response.Status.OK).entity(dtos).build();
|
|
}
|
|
@DELETE
|
|
@Path("{user_id}/delete")
|
|
@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();
|
|
}
|
|
utilisateurDAO.delete(existing);
|
|
return Response.status(Response.Status.OK).build();
|
|
}
|
|
}
|