Skip to content

Commit

Permalink
Improve DTO mapping and pet creation (#172)
Browse files Browse the repository at this point in the history
* map owner id when mapping from PetDto to Pet
* map pet id when mapping from VisitDto to Visit
* when creating new pet, return CREATED and its location
* align openapi spec with implementation to return 201 instead of 200
  • Loading branch information
misto authored Nov 9, 2024
1 parent 44c8495 commit e2e11ec
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface PetMapper {

Collection<Pet> toPets(Collection<PetDto> pets);

@Mapping(source = "ownerId", target = "owner.id")
Pet toPet(PetDto petDto);

Pet toPet(PetFieldsDto petFieldsDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
@Mapper(uses = PetMapper.class)
public interface VisitMapper {
@Mapping(source = "petId", target = "pet.id")
Visit toVisit(VisitDto visitDto);

Visit toVisit(VisitFieldsDto visitFieldsDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.samples.petclinic.rest.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.samples.petclinic.mapper.PetMapper;
Expand All @@ -27,6 +28,7 @@
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -98,7 +100,10 @@ public ResponseEntity<PetDto> deletePet(Integer petId) {
@PreAuthorize("hasRole(@roles.OWNER_ADMIN)")
@Override
public ResponseEntity<PetDto> addPet(PetDto petDto) {
this.clinicService.savePet(petMapper.toPet(petDto));
return new ResponseEntity<>(petDto, HttpStatus.OK);
HttpHeaders headers = new HttpHeaders();
Pet pet = petMapper.toPet(petDto);
this.clinicService.savePet(pet);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}").buildAndExpand(pet.getId()).toUri());
return new ResponseEntity<>(petMapper.toPetDto(pet), headers, HttpStatus.CREATED);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ paths:
$ref: '#/components/schemas/Pet'
required: true
responses:
200:
201:
description: Pet type created successfully.
headers:
ETag:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.mapper.PetMapper;
import org.springframework.samples.petclinic.model.Pet;
Expand Down Expand Up @@ -229,7 +230,8 @@ void testAddPetSuccess() throws Exception {
given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0)));
this.mockMvc.perform(post("/api/pets")
.content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isOk());
.andExpect(status().isCreated())
.andExpect(header().string(HttpHeaders.LOCATION, "/api/pets/3"));
}

@Test
Expand Down

0 comments on commit e2e11ec

Please sign in to comment.