diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java index 7e0c18c83e4be3edf893a9cde05b44ce62d8ccba..3bb2b6488fc3fb7e99016c1417a92c59bd1e5e3c 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/OwnerRestController.java @@ -130,7 +130,7 @@ public class OwnerRestController implements OwnersApi { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @Override - public ResponseEntity<PetDto> addPet(Integer ownerId, PetFieldsDto petFieldsDto) { + public ResponseEntity<PetDto> addPetToOwner(Integer ownerId, PetFieldsDto petFieldsDto) { HttpHeaders headers = new HttpHeaders(); Pet pet = petMapper.toPet(petFieldsDto); Owner owner = new Owner(); diff --git a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java index abb16c5834cd82a6257857a36a2567ce0e08672e..5d75adaf979182dd4439deb0d7b90599ea8402ba 100644 --- a/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java +++ b/src/main/java/org/springframework/samples/petclinic/rest/controller/PetRestController.java @@ -16,11 +16,13 @@ package org.springframework.samples.petclinic.rest.controller; +import io.swagger.annotations.ApiParam; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.samples.petclinic.mapper.PetMapper; import org.springframework.samples.petclinic.model.Pet; +import org.springframework.samples.petclinic.rest.api.PetsApi; import org.springframework.samples.petclinic.rest.dto.PetDto; import org.springframework.samples.petclinic.rest.dto.PetTypeDto; import org.springframework.samples.petclinic.service.ClinicService; @@ -30,7 +32,11 @@ import org.springframework.web.bind.annotation.*; import javax.transaction.Transactional; import javax.validation.Valid; +import javax.validation.constraints.Min; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.List; /** * @author Vitaliy Fedoriv @@ -39,7 +45,7 @@ import java.util.Collection; @RestController @CrossOrigin(exposedHeaders = "errors, content-type") @RequestMapping("api/pets") -public class PetRestController { +public class PetRestController implements PetsApi { private final ClinicService clinicService; @@ -52,7 +58,8 @@ public class PetRestController { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "/{petId}", method = RequestMethod.GET, produces = "application/json") - public ResponseEntity<PetDto> getPet(@PathVariable("petId") int petId) { + @Override + public ResponseEntity<PetDto> getPet(@Min(0)@ApiParam(value = "The ID of the pet.",required=true) @PathVariable("petId") Integer petId){ PetDto pet = petMapper.toPetDto(this.clinicService.findPetById(petId)); if (pet == null) { return new ResponseEntity<PetDto>(HttpStatus.NOT_FOUND); @@ -62,12 +69,13 @@ public class PetRestController { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "", method = RequestMethod.GET, produces = "application/json") - public ResponseEntity<Collection<PetDto>> getPets() { - Collection<PetDto> pets = petMapper.toPetsDto(this.clinicService.findAllPets()); + @Override + public ResponseEntity<List<PetDto>> listPets() { + List<PetDto> pets = new ArrayList<>(petMapper.toPetsDto(this.clinicService.findAllPets())); if (pets.isEmpty()) { - return new ResponseEntity<Collection<PetDto>>(HttpStatus.NOT_FOUND); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - return new ResponseEntity<Collection<PetDto>>(pets, HttpStatus.OK); + return new ResponseEntity<>(pets, HttpStatus.OK); } @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @@ -78,21 +86,16 @@ public class PetRestController { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "/{petId}", method = RequestMethod.PUT, produces = "application/json") - public ResponseEntity<PetDto> updatePet(@PathVariable("petId") int petId, @RequestBody @Valid PetDto pet, BindingResult bindingResult) { + @Override + public ResponseEntity<PetDto> updatePet(@Min(0)@ApiParam(value = "The ID of the pet.",required=true) @PathVariable("petId") Integer petId,@ApiParam(value = "The pet" ,required=true ) @Valid @RequestBody PetDto petDto) { BindingErrorsResponse errors = new BindingErrorsResponse(); - HttpHeaders headers = new HttpHeaders(); - if (bindingResult.hasErrors() || (pet == null)) { - errors.addAllErrors(bindingResult); - headers.add("errors", errors.toJSON()); - return new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST); - } Pet currentPet = this.clinicService.findPetById(petId); if (currentPet == null) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - currentPet.setBirthDate(pet.getBirthDate()); - currentPet.setName(pet.getName()); - currentPet.setType(petMapper.toPetType(pet.getType())); + currentPet.setBirthDate(petDto.getBirthDate()); + currentPet.setName(petDto.getName()); + currentPet.setType(petMapper.toPetType(petDto.getType())); this.clinicService.savePet(currentPet); return new ResponseEntity<>(petMapper.toPetDto(currentPet), HttpStatus.NO_CONTENT); } @@ -100,13 +103,14 @@ public class PetRestController { @PreAuthorize("hasRole(@roles.OWNER_ADMIN)") @RequestMapping(value = "/{petId}", method = RequestMethod.DELETE, produces = "application/json") @Transactional - public ResponseEntity<Void> deletePet(@PathVariable("petId") int petId) { + @Override + public ResponseEntity<PetDto> deletePet(@Min(0)@ApiParam(value = "The ID of the pet.",required=true) @PathVariable("petId") Integer petId) { Pet pet = this.clinicService.findPetById(petId); if (pet == null) { - return new ResponseEntity<Void>(HttpStatus.NOT_FOUND); + return new ResponseEntity<>(HttpStatus.NOT_FOUND); } this.clinicService.deletePet(pet); - return new ResponseEntity<Void>(HttpStatus.NO_CONTENT); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } diff --git a/src/main/resources/openapi.yml b/src/main/resources/openapi.yml index e1cb0310c30e8613f587954789122c17b1429917..a5a5cf1729777893f92cff417c99613b5babbe48 100755 --- a/src/main/resources/openapi.yml +++ b/src/main/resources/openapi.yml @@ -232,8 +232,8 @@ paths: post: tags: - pet - operationId: addPet - summary: Adds a pet + operationId: addPetToOwner + summary: Adds a pet to an owner description: Records the details of a new pet. parameters: - name: ownerId @@ -281,7 +281,7 @@ paths: get: tags: - pet - operationId: getPet + operationId: getOwnersPet summary: Get a pet by ID description: Returns the pet or a 404 error. parameters: @@ -343,7 +343,8 @@ paths: put: tags: - pet - operationId: updatePet + operationId: updateOwnersPet + summary: Update a pet's details description: Updates the pet record with the specified details. parameters: @@ -454,7 +455,7 @@ paths: /pettypes: get: tags: - - pet + - pettypes operationId: listPetTypes summary: Lists pet types description: Returns an array of pet types. @@ -702,6 +703,258 @@ paths: application/json: schema: $ref: '#/components/schemas/RestError' + + /pets: + get: + tags: + - pet + operationId: listPets + summary: Lists pet + description: Returns an array of pet . + responses: + 200: + description: Pet types found and returned. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + content: + application/json: + schema: + type: array + items: + $ref: '#/components/schemas/Pet' + 304: + description: Not modified. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + 500: + description: Server error. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + post: + tags: + - petw + operationId: addPet + summary: Create a pet + description: Creates a pet . + requestBody: + description: The pet + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + required: true + responses: + 200: + description: Pet type created successfully. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + 304: + description: Not modified. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + 400: + description: Bad request. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 404: + description: Owner not found. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 500: + description: Server error. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + /pets/{petId}: + get: + tags: + - pet + operationId: getPet + summary: Get a pet by ID + description: Returns the pet or a 404 error. + parameters: + - name: petId + in: path + description: The ID of the pet. + required: true + schema: + type: integer + format: int32 + minimum: 0 + example: 1 + responses: + 200: + description: Pet details found and returned. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + 304: + description: Not modified. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + 400: + description: Bad request. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 404: + description: Owner not found. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 500: + description: Server error. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + put: + tags: + - pet + operationId: updatePet + summary: Update a pet by ID + description: Returns the pet or a 404 error. + parameters: + - name: petId + in: path + description: The ID of the pet. + required: true + schema: + type: integer + format: int32 + minimum: 0 + example: 1 + requestBody: + description: The pet + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + required: true + responses: + 200: + description: Pet details found and returned. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + 304: + description: Not modified. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + 400: + description: Bad request. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 404: + description: Owner not found. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 500: + description: Server error. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + delete: + tags: + - pet + operationId: deletePet + summary: Delete a pet by ID + description: Returns the pet or a 404 error. + parameters: + - name: petId + in: path + description: The ID of the pet. + required: true + schema: + type: integer + format: int32 + minimum: 0 + example: 1 + responses: + 200: + description: Pet details found and returned. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/Pet' + 304: + description: Not modified. + headers: + ETag: + description: An ID for this version of the response. + schema: + type: string + 400: + description: Bad request. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 404: + description: Owner not found. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' + 500: + description: Server error. + content: + application/json: + schema: + $ref: '#/components/schemas/RestError' /vets: get: tags: diff --git a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java index 35ad6b8454b664074e842aceec34b467ff408e4e..7a17ed72a293e90d5acedf8007a9dc4a3594d82b 100644 --- a/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java +++ b/src/test/java/org/springframework/samples/petclinic/rest/controller/PetRestControllerTests.java @@ -121,7 +121,7 @@ class PetRestControllerTests { @WithMockUser(roles = "OWNER_ADMIN") void testGetPetNotFound() throws Exception { given(petMapper.toPetDto(this.clinicService.findPetById(-1))).willReturn(null); - this.mockMvc.perform(get("/api/pets/-1") + this.mockMvc.perform(get("/api/pets/999") .accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } @@ -216,7 +216,7 @@ class PetRestControllerTests { mapper.registerModule(new JavaTimeModule()); String newPetAsJSON = mapper.writeValueAsString(newPet); given(this.clinicService.findPetById(-1)).willReturn(null); - this.mockMvc.perform(delete("/api/pets/-1") + this.mockMvc.perform(delete("/api/pets/999") .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE)) .andExpect(status().isNotFound()); }