Skip to content

Commit

Permalink
refactor: handle exceptions in controller
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Dec 6, 2023
1 parent f18db93 commit 6275ce1
Show file tree
Hide file tree
Showing 34 changed files with 276 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;
Expand Down Expand Up @@ -122,8 +123,8 @@ public ResponseEntity<?> getCampaign(@PathVariable("id") String id) {
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> putCampaign(@PathVariable("id") String id, @RequestBody @Valid CampaignDto campaignDto) {
if (StringUtils.isBlank(campaignDto.getId()) || !campaignDto.getId().equalsIgnoreCase(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and idCampaign don't match");
if ( !campaignDto.getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and idCampaign don't match");
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.LOCATION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Owner;
import fr.insee.survey.datacollectionmanagement.metadata.dto.OwnerDto;
import fr.insee.survey.datacollectionmanagement.metadata.service.OwnerService;
Expand All @@ -11,6 +12,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
Expand All @@ -19,6 +21,7 @@
import org.springframework.http.HttpStatus;
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.servlet.support.ServletUriComponentsBuilder;

Expand All @@ -32,6 +35,7 @@
+ "|| @AuthorizeMethodDecider.isAdmin() ")
@Tag(name = "3 - Metadata", description = "Enpoints to create, update, delete and find entities in metadata domain")
@RequiredArgsConstructor
@Validated
public class OwnerController {

private final ModelMapper modelmapper;
Expand Down Expand Up @@ -73,9 +77,9 @@ public ResponseEntity<?> getOwner(@PathVariable("id") String id) {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = OwnerDto.class))),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> putOwner(@PathVariable("id") String id, @RequestBody OwnerDto ownerDto) {
public ResponseEntity<?> putOwner(@PathVariable("id") String id, @RequestBody @Valid OwnerDto ownerDto) {
if (!ownerDto.getId().equals(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and owner id don't match");
throw new NotMatchException("id and owner id don't match");
}

HttpHeaders responseHeaders = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.dto.PartitioningDto;
Expand Down Expand Up @@ -94,8 +95,8 @@ public ResponseEntity<?> getPartitioning(@PathVariable("id") String id) {
})
public ResponseEntity<?> putPartitioning(@PathVariable("id") String id,
@RequestBody PartitioningDto partitioningDto) {
if (StringUtils.isBlank(partitioningDto.getId()) || !partitioningDto.getId().equalsIgnoreCase(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and idPartitioning don't match");
if (!partitioningDto.getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and owner id don't match");
}
Partitioning partitioning;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Owner;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
Expand All @@ -20,6 +21,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -30,6 +32,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

Expand All @@ -43,6 +46,7 @@
@Tag(name = "3 - Metadata", description = "Enpoints to create, update, delete and find entities in metadata domain")
@Slf4j
@RequiredArgsConstructor
@Validated
public class SourceController {

private final SourceService sourceService;
Expand Down Expand Up @@ -93,9 +97,10 @@ public ResponseEntity<?> getSource(@PathVariable("id") String id) {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = SourceCompleteDto.class))),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> putSource(@PathVariable("id") String id, @RequestBody SourceCompleteDto SourceCompleteDto) {
if (StringUtils.isBlank(SourceCompleteDto.getId()) || !SourceCompleteDto.getId().equalsIgnoreCase(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and source id don't match");
public ResponseEntity<?> putSource(@PathVariable("id") String id, @RequestBody @Valid SourceCompleteDto SourceCompleteDto) {
if ( !SourceCompleteDto.getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and source id don't match");

}

Source source;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Support;
import fr.insee.survey.datacollectionmanagement.metadata.dto.SupportDto;
import fr.insee.survey.datacollectionmanagement.metadata.service.SupportService;
Expand All @@ -11,6 +12,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
Expand All @@ -19,6 +21,7 @@
import org.springframework.http.HttpStatus;
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.servlet.support.ServletUriComponentsBuilder;

Expand All @@ -32,6 +35,7 @@
+ "|| @AuthorizeMethodDecider.isAdmin() ")
@Tag(name = "3 - Metadata", description = "Enpoints to create, update, delete and find entities in metadata domain")
@RequiredArgsConstructor
@Validated
public class SupportController {

private final ModelMapper modelmapper;
Expand Down Expand Up @@ -73,9 +77,9 @@ public ResponseEntity<?> getSupport(@PathVariable("id") String id) {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = SupportDto.class))),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> putSupport(@PathVariable("id") String id, @RequestBody SupportDto supportDto) {
public ResponseEntity<?> putSupport(@PathVariable("id") String id, @RequestBody @Valid SupportDto supportDto) {
if (!supportDto.getId().equals(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and support id don't match");
throw new NotMatchException("id and support id don't match");
}

HttpHeaders responseHeaders = new HttpHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Campaign;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Partitioning;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Source;
Expand All @@ -18,6 +19,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -79,14 +81,10 @@ public ResponseEntity<?> getSurveysBySource(@PathVariable("id") String id) {
@ApiResponse(responseCode = "404", description = "Not found"),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> getSurvey(@PathVariable("id") String id) {
public ResponseEntity<SurveyDto> getSurvey(@PathVariable("id") String id) {
Survey survey = surveyService.findById(StringUtils.upperCase(id));
try {
return ResponseEntity.ok().body(convertToDto(survey));
return ResponseEntity.ok().body(convertToDto(survey));

} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Error");
}

}

Expand All @@ -97,9 +95,11 @@ public ResponseEntity<?> getSurvey(@PathVariable("id") String id) {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(schema = @Schema(implementation = SurveyDto.class))),
@ApiResponse(responseCode = "400", description = "Bad request")
})
public ResponseEntity<?> putSurvey(@PathVariable("id") String id, @RequestBody SurveyDto surveyDto) {
if (StringUtils.isBlank(surveyDto.getId()) || !surveyDto.getId().equalsIgnoreCase(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and idSurvey don't match");
public ResponseEntity<SurveyDto> putSurvey(@PathVariable("id") String id, @RequestBody @Valid SurveyDto surveyDto) {
if (!surveyDto.getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and idSurvey don't match");


}
Survey survey;
HttpHeaders responseHeaders = new HttpHeaders();
Expand Down Expand Up @@ -131,7 +131,7 @@ public ResponseEntity<?> putSurvey(@PathVariable("id") String id, @RequestBody S
@ApiResponse(responseCode = "400", description = "Bad Request")
})
@Transactional
public ResponseEntity<?> deleteSurvey(@PathVariable("id") String id) {
public ResponseEntity<String> deleteSurvey(@PathVariable("id") String id) {
Survey survey = surveyService.findById(id);

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OwnerDto {

@NotBlank
private String id;
private String label;
private String ministry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import java.util.Date;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

import java.util.Date;

@Data
public class PartitioningDto {

@NotBlank
private String id;
private String campaignId;
private String label;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import fr.insee.survey.datacollectionmanagement.metadata.util.PeriodicityEnum;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SourceCompleteDto {

@NotBlank
private String id;
private String longWording;
private String shortWording;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SupportDto {

@NotBlank
private String id;
private String label;
private String phoneNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package fr.insee.survey.datacollectionmanagement.metadata.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;

@Getter
@Setter
public class SurveyDto {

@NotBlank
private String id;
@NonNull
@NotBlank
private String sourceId;
private Integer year;
private Integer sampleSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.insee.survey.datacollectionmanagement.contact.service.AddressService;
import fr.insee.survey.datacollectionmanagement.contact.service.ContactService;
import fr.insee.survey.datacollectionmanagement.exception.NotFoundException;
import fr.insee.survey.datacollectionmanagement.exception.NotMatchException;
import fr.insee.survey.datacollectionmanagement.metadata.domain.*;
import fr.insee.survey.datacollectionmanagement.metadata.dto.*;
import fr.insee.survey.datacollectionmanagement.metadata.service.*;
Expand All @@ -33,6 +34,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -42,6 +44,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

Expand All @@ -54,6 +57,7 @@
@Slf4j
@Tag(name = "6 - Webclients", description = "Enpoints for webclients")
@RequiredArgsConstructor
@Validated
public class WebclientController {

private final QuestioningService questioningService;
Expand Down Expand Up @@ -118,12 +122,11 @@ public ResponseEntity<?> putQuestioning(@RequestBody QuestioningWebclientDto que
su = convertToEntity(questioningWebclientDto.getSurveyUnit());

// Create su if not exists or update
try{
try {
SurveyUnit optSuBase = surveyUnitService.findbyId(idSu);
su.setQuestionings(optSuBase.getQuestionings());

}
catch (NotFoundException e){
} catch (NotFoundException e) {
log.warn("survey unit {} does not exist - Creation of the survey unit",
idSu);
su.setQuestionings(new HashSet<>());
Expand Down Expand Up @@ -299,12 +302,12 @@ public ResponseEntity<?> getMetadata(@PathVariable("id") String id) {
})
@Transactional
public ResponseEntity<?> putMetadata(@PathVariable("id") String id,
@RequestBody MetadataDto metadataDto) {
@RequestBody @Valid MetadataDto metadataDto) {
if (!metadataDto.getPartitioningDto().getId().equalsIgnoreCase(id)) {
throw new NotMatchException("id and idPartitioning don't match");
}
try {
if (StringUtils.isBlank(metadataDto.getPartitioningDto().getId())
|| !metadataDto.getPartitioningDto().getId().equalsIgnoreCase(id)) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("id and idPartitioning don't match");
}

MetadataDto metadataReturn = new MetadataDto();

HttpHeaders responseHeaders = new HttpHeaders();
Expand Down
Loading

0 comments on commit 6275ce1

Please sign in to comment.