package io.gmss.fiscad.controllers.decoupage; import io.gmss.fiscad.entities.decoupage.Secteur; import io.gmss.fiscad.exceptions.*; import io.gmss.fiscad.interfaces.decoupage.SecteurService; import io.gmss.fiscad.paylaods.ApiResponse; import io.gmss.fiscad.paylaods.request.crudweb.SecteurPaylaodWeb; import io.gmss.fiscad.paylaods.request.synchronisation.SecteurPayload; import io.swagger.v3.oas.annotations.Operation; 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/secteur", produces = MediaType.APPLICATION_JSON_VALUE) @SecurityRequirement(name = "bearer") @Tag(name = "Secteur") //@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPERVISEUR')") public class SecteurController { private final SecteurService secteurService; private static final Logger logger = LoggerFactory.getLogger(SecteurController.class); public SecteurController(SecteurService secteurService) { this.secteurService = secteurService; } @Operation( summary = "Créer un secteur", description = "Permet de Créer un secteur" ) @PostMapping("/create") @PreAuthorize("hasAuthority('CREATE_SECTEUR')") public ResponseEntity createSecteur(@RequestBody @Valid @Validated SecteurPaylaodWeb secteurPaylaodWeb) { try { secteurPaylaodWeb = secteurService.createSecteur(secteurPaylaodWeb); return new ResponseEntity<>( new ApiResponse<>(true, secteurPaylaodWeb, "Secteur 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); } } @Operation( summary = "mettre à jour un secteur", description = "Permet de modifier un secteur" ) @PutMapping("/update/{id}") @PreAuthorize("hasAuthority('UPDATE_SECTEUR')") public ResponseEntity updateSecteur(@PathVariable Long id, @RequestBody SecteurPaylaodWeb secteurPayloadWeb) { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.updateSecteur(id, secteurPayloadWeb), "Secteur 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); } } @Operation( summary = "supprimer un secteur", description = "Permet de supprimer un secteur" ) @DeleteMapping("/delete/{id}") @PreAuthorize("hasAuthority('DELETE_SECTEUR')") public ResponseEntity deleteSecteurr(@PathVariable Long id) { try { secteurService.deleteSecteur(id); return new ResponseEntity<>( new ApiResponse<>(true, "Secteur supprimé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); } } @Operation( summary = "recuperer tous les secteurs", description = "Permet de récuperer l'ensemble des secteurs" ) @GetMapping("/all") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getAllSecteurList() { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurList(), "Liste des secteurs 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); } } @Operation( summary = "recuperer tous les secteurs avec pagination", description = "Permet de récuperer l'ensemble des secteurs avec pagination" ) @GetMapping("/all-paged") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getAllSecteurPaged(@RequestParam int pageNo, @RequestParam int pageSize) { try { Pageable pageable = PageRequest.of(pageNo, pageSize); return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurList(pageable), "Liste des secteurs 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); } } @Operation( summary = "recuperer un secteurs par son ID ", description = "Permet de récuperer le secteur ayant l'ID fournie en path" ) @GetMapping("/id/{id}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurById(@PathVariable Long id) { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurById(id), "Secteur trouvé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); } } @Operation( summary = "recuperer tous les secteurs d'une structure", description = "Permet de récuperer l'ensemble des secteurs de la structure dont l'ID est fourni en path" ) @GetMapping("/by-structure-id/{structureId}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurByStructureId(@PathVariable Long structureId) { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurListByStructureId(structureId), "Secteur trouvé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); } } @Operation( summary = "recuperer tous les secteurs d'une structure", description = "Permet de récuperer l'ensemble des secteurs de la structure dont l'ID est fourni en path avec pagination" ) @GetMapping("/page/by-structure-id/{structureId}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurByStructureIdPage(@PathVariable Long structureId, @RequestParam int pageNo, @RequestParam int pageSize) { try { Pageable pageable = PageRequest.of(pageNo, pageSize); return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurListByStructureId(structureId,pageable), "Secteur trouvé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); } } @Operation( summary = "recuperer tous les secteurs d'une section", description = "Permet de récuperer l'ensemble des secteurs de la section dont l'ID est fourni en path" ) @GetMapping("/by-section-id/{sectionId}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurBySectionId(@PathVariable Long sectionId) { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurListBySectionId(sectionId), "Secteur trouvé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); } } @Operation( summary = "recuperer tous les secteurs d'une section avec pagination", description = "Permet de récuperer l'ensemble des secteurs de la section dont l'ID est fourni en path avec pagination" ) @GetMapping("/page/by-section-id/{sectionId}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurBySectionIdPage(@PathVariable Long sectionId, @RequestParam int pageNo, @RequestParam int pageSize) { try { Pageable pageable = PageRequest.of(pageNo, pageSize); return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getSecteurListBySectionId(sectionId,pageable), "Secteur trouvé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); } } @Operation( summary = "recuperer tous les secteurs d'un departement", description = "Permet de récuperer l'ensemble des secteurs du departement dont l'ID est fourni en path" ) @GetMapping("/by-departement-id/{departementId}") @PreAuthorize("hasAuthority('READ_SECTEUR')") public ResponseEntity getSecteurByDepartementId(@PathVariable Long departementId) { try { return new ResponseEntity<>( new ApiResponse<>(true, secteurService.getListSecteurByDepartementId(departementId), "Secteur trouvé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); } } }