Skip to content

Commit

Permalink
Merge pull request #129 from arey/bugfix/125-refactoring-pettype
Browse files Browse the repository at this point in the history
Remove the readOnly property to PetType #125
  • Loading branch information
arey authored Jul 28, 2024
2 parents 9a80274 + fb1a9b2 commit d01b91b
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface PetTypeMapper {
PetType toPetType(PetTypeFieldsDto petTypeFieldsDto);

PetTypeDto toPetTypeDto(PetType petType);
PetTypeFieldsDto toPetTypeFieldsDto(PetType petType);

List<PetTypeDto> toPetTypeDtos(Collection<PetType> petTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.springframework.samples.petclinic.mapper.VisitMapper;
import org.springframework.samples.petclinic.model.Owner;
import org.springframework.samples.petclinic.model.Pet;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.model.Visit;
import org.springframework.samples.petclinic.rest.api.OwnersApi;
import org.springframework.samples.petclinic.rest.dto.*;
Expand Down Expand Up @@ -140,8 +139,6 @@ public ResponseEntity<PetDto> addPetToOwner(Integer ownerId, PetFieldsDto petFie
Owner owner = new Owner();
owner.setId(ownerId);
pet.setOwner(owner);
PetType petType = this.clinicService.findPetTypeByName(pet.getType().getName());
pet.setType(petType);
this.clinicService.savePet(pet);
PetDto petDto = petMapper.toPetDto(pet);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pets/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.rest.api.PettypesApi;
import org.springframework.samples.petclinic.rest.dto.PetTypeDto;
import org.springframework.samples.petclinic.rest.dto.PetTypeFieldsDto;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
Expand All @@ -32,7 +33,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin(exposedHeaders = "errors, content-type")
Expand Down Expand Up @@ -70,16 +70,12 @@ public ResponseEntity<PetTypeDto> getPetType(Integer petTypeId) {

@PreAuthorize("hasRole(@roles.VET_ADMIN)")
@Override
public ResponseEntity<PetTypeDto> addPetType(PetTypeDto petTypeDto) {
public ResponseEntity<PetTypeDto> addPetType(PetTypeFieldsDto petTypeFieldsDto) {
HttpHeaders headers = new HttpHeaders();
if (Objects.nonNull(petTypeDto.getId()) && !petTypeDto.getId().equals(0)) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
} else {
final PetType type = petTypeMapper.toPetType(petTypeDto);
this.clinicService.savePetType(type);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pettypes/{id}").buildAndExpand(type.getId()).toUri());
return new ResponseEntity<>(petTypeMapper.toPetTypeDto(type), headers, HttpStatus.CREATED);
}
final PetType type = petTypeMapper.toPetType(petTypeFieldsDto);
this.clinicService.savePetType(type);
headers.setLocation(UriComponentsBuilder.newInstance().path("/api/pettypes/{id}").buildAndExpand(type.getId()).toUri());
return new ResponseEntity<>(petTypeMapper.toPetTypeDto(type), headers, HttpStatus.CREATED);
}

@PreAuthorize("hasRole(@roles.VET_ADMIN)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,4 @@ public interface ClinicService {
void deleteSpecialty(Specialty specialty) throws DataAccessException;

List<Specialty> findSpecialtiesByNameIn(Set<String> names) throws DataAccessException;

PetType findPetTypeByName(String name) throws DataAccessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,4 @@ public List<Specialty> findSpecialtiesByNameIn(Set<String> names){
}
return specialties;
}

@Override
@Transactional(readOnly = true)
public PetType findPetTypeByName(String name){
PetType petType;
try {
petType = petTypeRepository.findByName(name);
} catch (ObjectRetrievalFailureException|EmptyResultDataAccessException e) {
// just ignore not found exceptions for Jdbc/Jpa realization
return null;
}
return petType;
}
}
3 changes: 1 addition & 2 deletions src/main/resources/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/PetType'
$ref: '#/components/schemas/PetTypeFields'
required: true
responses:
200:
Expand Down Expand Up @@ -2170,7 +2170,6 @@ components:
format: int32
minimum: 0
example: 1
readOnly: true
required:
- id
User:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.springframework.samples.petclinic.mapper.PetTypeMapper;
import org.springframework.samples.petclinic.model.PetType;
import org.springframework.samples.petclinic.rest.advice.ExceptionControllerAdvice;
import org.springframework.samples.petclinic.rest.controller.PetTypeRestController;
import org.springframework.samples.petclinic.rest.dto.PetTypeDto;
import org.springframework.samples.petclinic.service.ClinicService;
import org.springframework.samples.petclinic.service.clinicService.ApplicationTestConfig;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -72,7 +70,7 @@ void initPetTypes(){
this.mockMvc = MockMvcBuilders.standaloneSetup(petTypeRestController)
.setControllerAdvice(new ExceptionControllerAdvice())
.build();
petTypes = new ArrayList<PetType>();
petTypes = new ArrayList<>();

PetType petType = new PetType();
petType.setId(1);
Expand Down Expand Up @@ -176,7 +174,7 @@ void testCreatePetTypeSuccess() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(null);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType));
String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeFieldsDto(newPetType));
this.mockMvc.perform(post("/api/pettypes/")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());
Expand All @@ -194,17 +192,7 @@ void testCreatePetTypeError() throws Exception {
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}
@Test
@WithMockUser(roles="VET_ADMIN")
void testCreatePetTypeErrorWithId() throws Exception {
PetType newPetType = petTypes.get(0);
newPetType.setId(1);
ObjectMapper mapper = new ObjectMapper();
String newPetTypeAsJSON = mapper.writeValueAsString(petTypeMapper.toPetTypeDto(newPetType));
this.mockMvc.perform(post("/api/pettypes/")
.content(newPetTypeAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isBadRequest());
}

@Test
@WithMockUser(roles="VET_ADMIN")
void testUpdatePetTypeSuccess() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,4 @@ void shouldFindSpecialtiesByNameIn() {
&& actual.getId().equals(expected.getId()))).isTrue();
}
}

@Test
@Transactional
void shouldFindPetTypeByName(){
PetType petType = this.clinicService.findPetTypeByName("cat");
assertThat(petType.getId()).isEqualTo(1);
}
}

0 comments on commit d01b91b

Please sign in to comment.