Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Vet creation from the POST /vets API #71

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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