All checks were successful
CI - Build & Test (develop) / build-and-test (pull_request) Successful in 36s
183 lines
9.5 KiB
Java
Executable File
183 lines
9.5 KiB
Java
Executable File
package io.gmss.fiscad.controllers.user;
|
|
|
|
|
|
import io.gmss.fiscad.entities.user.Profile;
|
|
import io.gmss.fiscad.exceptions.*;
|
|
import io.gmss.fiscad.interfaces.user.ProfileService;
|
|
import io.gmss.fiscad.paylaods.ApiResponse;
|
|
import io.gmss.fiscad.paylaods.request.crudweb.ProfilePaylaodWeb;
|
|
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.validation.annotation.Validated;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
|
|
|
@RestController
|
|
@RequestMapping(value = "api/profil", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
@SecurityRequirement(name = "bearer")
|
|
@Tag(name = "Profile")
|
|
@CrossOrigin(origins = "*")
|
|
public class ProfileController {
|
|
|
|
private final ProfileService profileService;
|
|
private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);
|
|
|
|
public ProfileController(ProfileService profileService) {
|
|
this.profileService = profileService;
|
|
}
|
|
|
|
|
|
@PostMapping("/create")
|
|
public ResponseEntity<?> createProfile(@RequestBody @Valid @Validated ProfilePaylaodWeb profilePaylaodWeb ) {
|
|
try {
|
|
Profile profile = profileService.createProfile(profilePaylaodWeb);
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, profile, "Profile created successully."),
|
|
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<?> updateProfile(@PathVariable Long id, @RequestBody ProfilePaylaodWeb profilePaylaodWeb) {
|
|
try {
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, profileService.updateProfile(id, profilePaylaodWeb), "Profile updated successully."),
|
|
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<?> deleteProfile(@PathVariable Long id) {
|
|
try {
|
|
profileService.deleteProfile(id);
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, "Profile deleted successully"),
|
|
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<?> getAll() {
|
|
try {
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, profileService.getProfileList(), "Liste des profiles."),
|
|
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<?> getById(@PathVariable Long id) {
|
|
try {
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, profileService.getProfileById(id), "Profile 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);
|
|
}
|
|
}
|
|
|
|
@GetMapping("/all-paged")
|
|
public ResponseEntity<?> getAllPaged(@RequestParam int pageNo, @RequestParam int pageSize) {
|
|
try {
|
|
Pageable pageable = PageRequest.of(pageNo, pageSize);
|
|
|
|
return new ResponseEntity<>(
|
|
new ApiResponse<>(true, profileService.getProfileList(pageable), "Liste des profiles."),
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|