package io.gmss.fiscad.controllers.decoupage; import io.gmss.fiscad.entities.decoupage.Departement; import io.gmss.fiscad.exceptions.*; import io.gmss.fiscad.interfaces.decoupage.DepartementService; import io.gmss.fiscad.paylaods.ApiResponse; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.HttpClientErrorException; @RestController @RequestMapping(value = "api/departement", produces = MediaType.APPLICATION_JSON_VALUE) @SecurityRequirement(name = "bearer") @Tag(name = "Département") @CrossOrigin(origins = "*") //@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPERVISEUR')") public class DepartementController { private final DepartementService departementService; private static final Logger logger = LoggerFactory.getLogger(DepartementController.class); public DepartementController(DepartementService departementService) { this.departementService = departementService; } @PostMapping("/create") public ResponseEntity createDepartement(@RequestBody @Valid @Validated Departement departement) { try { departement = departementService.createDepartement(departement); return new ResponseEntity<>( new ApiResponse<>(true, departement, "Departement créé avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } @PutMapping("/update/{id}") public ResponseEntity updateDepartement(@PathVariable Long id, @RequestBody Departement departement) { try { return new ResponseEntity<>( new ApiResponse<>(true, departementService.updateDepartement(id, departement), "Département mis à jour avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } @DeleteMapping("/delete/{id}") public ResponseEntity deleteDepartementr(@PathVariable Long id) { try { departementService.deleteDepartement(id); return new ResponseEntity<>( new ApiResponse<>(true, "Département supprimé avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } @GetMapping("/all") public ResponseEntity getAllDepartementList() { try { return new ResponseEntity<>( new ApiResponse<>(true, departementService.getDepartementList(), "Liste des département chargée avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } @GetMapping("/all-paged") public ResponseEntity getAllDepartementPaged(@RequestParam int pageNo, @RequestParam int pageSize) { try { Pageable pageable = PageRequest.of(pageNo, pageSize); return new ResponseEntity<>( new ApiResponse<>(true, departementService.getDepartementList(pageable), "Liste des département chargée avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } @GetMapping("/id/{id}") public ResponseEntity getDepartementById(@PathVariable Long id) { try { return new ResponseEntity<>( new ApiResponse<>(true, departementService.getDepartementById(id), "Département trouvé avec succès."), HttpStatus.OK ); } catch (HttpClientErrorException.MethodNotAllowed e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Method POST/GET is required."), HttpStatus.OK); } catch (NotFoundException | BadRequestException | MyFileNotFoundException | ResourceNotFoundException | FileStorageException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, e.getMessage()), HttpStatus.OK); } catch (NullPointerException e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "Null value has been detected {" + e.getMessage() + "}."), HttpStatus.OK); } catch (Exception e) { logger.error(e.getLocalizedMessage()); return new ResponseEntity<>(new ApiResponse(false, null, "An error has been occur and the content is {" + e.getMessage() + "}."), HttpStatus.OK); } } }