Skip to content

Commit

Permalink
Fix Vet creation from the POST /vets API
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Dec 29, 2021
1 parent 8655ad4 commit 2db3bcb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.mapstruct.Mapper;
import org.springframework.samples.petclinic.rest.dto.VetDto;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.rest.dto.VetFieldsDto;

import java.util.Collection;

Expand All @@ -13,6 +14,8 @@
public interface VetMapper {
Vet toVet(VetDto vetDto);

Vet toVet(VetFieldsDto vetFieldsDto);

VetDto toVetDto(Vet vet);

Collection<VetDto> toVetDtos(Collection<Vet> vets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.samples.petclinic.mapper.VetMapper;
import org.springframework.samples.petclinic.model.Specialty;
import org.springframework.samples.petclinic.model.Vet;
import org.springframework.samples.petclinic.rest.dto.VetFieldsDto;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
Expand Down Expand Up @@ -77,18 +78,18 @@ public ResponseEntity<VetDto> getVet(@PathVariable("vetId") int vetId) {

@PreAuthorize("hasRole(@roles.VET_ADMIN)")
@RequestMapping(value = "/vets", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<VetDto> addVet(@RequestBody @Valid VetDto vetDto, BindingResult bindingResult, UriComponentsBuilder ucBuilder) {
public ResponseEntity<VetDto> addVet(@RequestBody @Valid VetFieldsDto vetFieldsDto, BindingResult bindingResult, UriComponentsBuilder ucBuilder) {
BindingErrorsResponse errors = new BindingErrorsResponse();
HttpHeaders headers = new HttpHeaders();
if (bindingResult.hasErrors() || (vetDto == null)) {
if (bindingResult.hasErrors() || (vetFieldsDto == null)) {
errors.addAllErrors(bindingResult);
headers.add("errors", errors.toJSON());
return new ResponseEntity<VetDto>(headers, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST);
}
Vet vet = vetMapper.toVet(vetDto);
Vet vet = vetMapper.toVet(vetFieldsDto);
this.clinicService.saveVet(vet);
headers.setLocation(ucBuilder.path("/api/vets/{id}").buildAndExpand(vet.getId()).toUri());
return new ResponseEntity<VetDto>(vetMapper.toVetDto(vet), headers, HttpStatus.CREATED);
return new ResponseEntity<>(vetMapper.toVetDto(vet), headers, HttpStatus.CREATED);
}

@PreAuthorize("hasRole(@roles.VET_ADMIN)")
Expand Down
33 changes: 22 additions & 11 deletions src/main/resources/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,11 @@ components:
- id
- type
- visits
Vet:
title: Vet
description: A veterinarian.
VetFields:
title: VetFields
description: Editable fields of a veterinarian.
type: object
properties:
id:
title: ID
description: The ID of the vet.
type: integer
format: int32
minimum: 0
example: 1
readOnly: true
firstName:
title: First name
description: The first name of the vet.
Expand All @@ -768,6 +760,25 @@ components:
type: array
items:
$ref: '#/components/schemas/Specialty'
required:
- firstName
- lastName
- specialties
Vet:
title: Vet
description: A veterinarian.
allOf:
- $ref: '#/components/schemas/VetFields'
- type: object
properties:
id:
title: ID
description: The ID of the vet.
type: integer
format: int32
minimum: 0
example: 1
readOnly: true
required:
- id
- firstName
Expand Down

0 comments on commit 2db3bcb

Please sign in to comment.