Skip to content

Commit

Permalink
add UUID as id, and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lor-cab committed Oct 11, 2023
1 parent 7b55dca commit 2810013
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class UpdateGrantMandatoryQuestionDto {
private String orgType;

@NotBlank(message = "You must enter an answer")
@Pattern(regexp = "^[0-9]+[.][0-9]{1,2}$",message = "Funding amount must only numbers, and decimal(max 2) will need to be split with a dot")
@Pattern(regexp = "^[0-9]+[.][0-9]{1,2}$",message = "Funding amount must only contain numbers, and decimal(max 2) will need to be split with a dot")
private String fundingAmount;

@NotBlank(message = "You must select an answer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
public class GrantMandatoryQuestions extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue
@Column(name = "id")
private Integer id;
private UUID id;

@ManyToOne
@JoinColumn(name = "grant_scheme_id")
Expand All @@ -57,10 +57,10 @@ public class GrantMandatoryQuestions extends BaseEntity {
@Column(name = "name")
private String name;

@Column(name = "address_line1")
@Column(name = "address_line_1")
private String addressLine1;

@Column(name = "address_line2")
@Column(name = "address_line_2")
private String addressLine2;

@Column(name = "city")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import gov.cabinetoffice.gap.applybackend.model.GrantApplicant;
import gov.cabinetoffice.gap.applybackend.model.GrantMandatoryQuestions;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface GrantMandatoryQuestionRepository extends JpaRepository<GrantMandatoryQuestions, Integer> {
List<GrantMandatoryQuestions> findByGrantSchemeIdAndCreatedBy(Integer schemeId, GrantApplicant applicant);
public interface GrantMandatoryQuestionRepository extends JpaRepository<GrantMandatoryQuestions, UUID> {
List<GrantMandatoryQuestions> findByGrantSchemeAndCreatedBy(GrantScheme schemeId, GrantApplicant applicant);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

import static java.util.Optional.ofNullable;

Expand All @@ -19,11 +20,8 @@
@Slf4j
public class GrantMandatoryQuestionService {
private final GrantMandatoryQuestionRepository grantMandatoryQuestionRepository;
private final GrantApplicantService grantApplicantService;
private final GrantSchemeService grantSchemeService;


public GrantMandatoryQuestions getGrantMandatoryQuestionById(Integer id, String applicantSub) {
public GrantMandatoryQuestions getGrantMandatoryQuestionById(UUID id, String applicantSub) {
final Optional<GrantMandatoryQuestions> grantMandatoryQuestion = ofNullable(grantMandatoryQuestionRepository.findById(id)
.orElseThrow(() -> new NotFoundException(String.format("No Mandatory Question with ID %s was found", id))));
if(!grantMandatoryQuestion.get().getCreatedBy().getUserId().equals(applicantSub)){
Expand All @@ -32,13 +30,11 @@ public GrantMandatoryQuestions getGrantMandatoryQuestionById(Integer id, String
return grantMandatoryQuestion.get();
}

public GrantMandatoryQuestions createMandatoryQuestion(Integer schemeId, String applicantSub){
final GrantApplicant applicant = grantApplicantService.getApplicantById(applicantSub);
final GrantScheme scheme = grantSchemeService.getSchemeById(schemeId);
public GrantMandatoryQuestions createMandatoryQuestion(GrantScheme scheme, GrantApplicant applicant){

if(doesMandatoryQuestionAlreadyExist(schemeId, applicant)){
log.debug("Mandatory question for scheme {}, and applicant {} already exist", schemeId, applicantSub);
return grantMandatoryQuestionRepository.findByGrantSchemeIdAndCreatedBy(schemeId, applicant).get(0);
if(doesMandatoryQuestionAlreadyExist(scheme, applicant)){
log.debug("Mandatory question for scheme {}, and applicant {} already exist", scheme.getId(), applicant.getId());
return grantMandatoryQuestionRepository.findByGrantSchemeAndCreatedBy(scheme, applicant).get(0);
};

final GrantMandatoryQuestions grantMandatoryQuestions = GrantMandatoryQuestions.builder()
Expand All @@ -49,8 +45,8 @@ public GrantMandatoryQuestions createMandatoryQuestion(Integer schemeId, String
return grantMandatoryQuestionRepository.save(grantMandatoryQuestions);
}

private boolean doesMandatoryQuestionAlreadyExist(Integer schemeId, GrantApplicant applicant) {
return !grantMandatoryQuestionRepository.findByGrantSchemeIdAndCreatedBy(schemeId, applicant).isEmpty();
private boolean doesMandatoryQuestionAlreadyExist(GrantScheme scheme, GrantApplicant applicant) {
return !grantMandatoryQuestionRepository.findByGrantSchemeAndCreatedBy(scheme, applicant).isEmpty();
}

public GrantMandatoryQuestions updateMandatoryQuestion(GrantMandatoryQuestions grantMandatoryQuestions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import gov.cabinetoffice.gap.applybackend.dto.api.GetGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.dto.api.JwtPayload;
import gov.cabinetoffice.gap.applybackend.dto.api.MandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.dto.api.UpdateGrantMandatoryQuestionDto;
import gov.cabinetoffice.gap.applybackend.model.GrantApplicant;
import gov.cabinetoffice.gap.applybackend.model.GrantMandatoryQuestions;
import gov.cabinetoffice.gap.applybackend.model.GrantScheme;
import gov.cabinetoffice.gap.applybackend.service.GrantApplicantService;
import gov.cabinetoffice.gap.applybackend.service.GrantMandatoryQuestionService;
import gov.cabinetoffice.gap.applybackend.service.GrantSchemeService;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -24,23 +29,30 @@
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.UUID;

@RequiredArgsConstructor
@RestController
@RequestMapping("/grant-mandatory-questions")
@Slf4j
public class GrantMandatoryQuestionsController {
private final GrantMandatoryQuestionService grantMandatoryQuestionService;
private final GrantApplicantService grantApplicantService;
private final GrantSchemeService grantSchemeService;
//change to mapstruct
private final ModelMapper modelMapper;

@PostMapping()
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Grant Mandatory question created", content = @Content(mediaType = "application/json", schema = @Schema(implementation = GrantMandatoryQuestions.class))),
@ApiResponse(responseCode = "404", description = "No Grant Mandatory question found", content = @Content(mediaType = "application/json")),
})
public ResponseEntity<Integer> createMandatoryQuestion(@RequestParam final Integer schemeId) {
public ResponseEntity<UUID> createMandatoryQuestion(@RequestParam final Integer schemeId) {
final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(schemeId, jwtPayload.getSub());
final GrantApplicant applicant = grantApplicantService.getApplicantById(jwtPayload.getSub());
final GrantScheme scheme = grantSchemeService.getSchemeById(schemeId);
final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.createMandatoryQuestion(scheme, applicant);

return ResponseEntity.ok(grantMandatoryQuestions.getId());
}

Expand All @@ -50,11 +62,13 @@ public ResponseEntity<Integer> createMandatoryQuestion(@RequestParam final Integ
@ApiResponse(responseCode = "403", description = "User cannot access this mandatory question", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "404", description = "No Grant Mandatory question found", content = @Content(mediaType = "application/json")),
})
public ResponseEntity<GetGrantMandatoryQuestionDto> getGrantMandatoryQuestionsById(@PathVariable final Integer grantMandatoryQuestionId) {
public ResponseEntity<GetGrantMandatoryQuestionDto> getGrantMandatoryQuestionsById(@PathVariable final UUID mandatoryQuestionId) {
final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.getGrantMandatoryQuestionById(grantMandatoryQuestionId, jwtPayload.getSub());
final GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.getGrantMandatoryQuestionById(mandatoryQuestionId, jwtPayload.getSub());
final GetGrantMandatoryQuestionDto getGrantMandatoryQuestionDto = GetGrantMandatoryQuestionDto.builder().build();

modelMapper.map(grantMandatoryQuestions, getGrantMandatoryQuestionDto);

return ResponseEntity.ok(getGrantMandatoryQuestionDto);
}

Expand All @@ -64,8 +78,8 @@ public ResponseEntity<GetGrantMandatoryQuestionDto> getGrantMandatoryQuestionsBy
@ApiResponse(responseCode = "403", description = "User cannot access this mandatory question", content = @Content(mediaType = "application/json")),
@ApiResponse(responseCode = "404", description = "No Grant Mandatory question found", content = @Content(mediaType = "application/json")),
})
public ResponseEntity<String> updateMandatoryQuestion(@PathVariable Integer mandatoryQuestionId,
@RequestBody @Valid UpdateGrantMandatoryQuestionDto mandatoryQuestionDto) {
public ResponseEntity<String> updateMandatoryQuestion(@PathVariable final UUID mandatoryQuestionId,
@RequestBody @Valid final UpdateGrantMandatoryQuestionDto mandatoryQuestionDto) {
final JwtPayload jwtPayload = (JwtPayload) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
GrantMandatoryQuestions grantMandatoryQuestions = grantMandatoryQuestionService.getGrantMandatoryQuestionById(mandatoryQuestionId, jwtPayload.getSub());
modelMapper.map(mandatoryQuestionDto, grantMandatoryQuestions);
Expand Down

0 comments on commit 2810013

Please sign in to comment.