Skip to content

Commit

Permalink
feat/CSTM-CPC-1006-Complete-petTypes-Backend1 (#566)
Browse files Browse the repository at this point in the history
added backend methods for petTypes

JIRA: link to jira ticket

https://champlainsaintlambert.atlassian.net/jira/software/c/projects/CPC/boards/14/backlog?atlOrigin=eyJpIjoiODFlNDliODI5YzJhNDM5NWFlYjk1YWY0NzY3MDI3MzMiLCJwIjoiaiJ9
## Context:
This change was made so that users can implement basic crud functions in
the front end.
## Changes
Added the ability to Delete, update and getById methods for the
back-end.
What are the various changes and what other modules do those changes
effect.
This can be bullet point or sentence format.
## Before and After UI (Required for UI-impacting PRs)
If this is a change to the UI, include before and after screenshots to
show the differences.
If this is a new UI feature, include screenshots to show reviewers what
it looks like.
## Dev notes (Optional)
- Specific technical changes that should be noted
## Linked pull requests (Optional)
- pull request link
  • Loading branch information
Saimirz authored Oct 23, 2023
1 parent eae50eb commit 5f3da92
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.petclinic.bffapigateway.dtos.CustomerDTOs.OwnerRequestDTO;
import com.petclinic.bffapigateway.dtos.CustomerDTOs.OwnerResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetRequestDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetType;
import com.petclinic.bffapigateway.dtos.Pets.PetTypeResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.*;
import com.petclinic.bffapigateway.dtos.Vets.PhotoDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -263,4 +260,29 @@ public Flux<PetTypeResponseDTO> getAllPetTypes() {
.retrieve()
.bodyToFlux(PetTypeResponseDTO.class);
}
public Mono<PetTypeResponseDTO> getPetTypeByPetTypeId(String petTypeId) {
return webClientBuilder.build().get()
.uri(customersServiceUrl + "/owners/petTypes/" + petTypeId)
.retrieve()
.bodyToMono(PetTypeResponseDTO.class);
}

public Mono<PetTypeResponseDTO> deletePetType(final String petTypeId) {
return webClientBuilder.build().delete()
.uri(customersServiceUrl +"/owners/petTypes/"+ petTypeId)
.retrieve()
.bodyToMono(PetTypeResponseDTO.class);
}

public Mono<PetTypeResponseDTO> updatePetType(String petTypeId, Mono<PetTypeRequestDTO> petTypeRequestDTO) {
return petTypeRequestDTO.flatMap(requestDTO ->
webClientBuilder.build()
.put()
.uri(customersServiceUrl + "/owners/petTypes/" + petTypeId)
.body(BodyInserters.fromValue(requestDTO))
.retrieve()
.bodyToMono(PetTypeResponseDTO.class)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import com.petclinic.bffapigateway.dtos.CustomerDTOs.OwnerRequestDTO;
import com.petclinic.bffapigateway.dtos.Inventory.*;
import com.petclinic.bffapigateway.dtos.CustomerDTOs.OwnerResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetRequestDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.PetType;
import com.petclinic.bffapigateway.dtos.Pets.PetTypeResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.*;
import com.petclinic.bffapigateway.dtos.Vets.*;
import com.petclinic.bffapigateway.dtos.Visits.VisitRequestDTO;
import com.petclinic.bffapigateway.utils.Security.Annotations.IsUserSpecific;
Expand Down Expand Up @@ -975,4 +972,42 @@ public Flux<PetTypeResponseDTO> getAllPetTypes() {
.map(addVisitsToOwner(n))
);*/
}

@IsUserSpecific(idToMatch = {"petTypeId"}, bypassRoles = {Roles.ALL})
@GetMapping(value = "owners/petTypes/{petTypeId}")
public Mono<ResponseEntity<PetTypeResponseDTO>> getPetTypeById(final @PathVariable String petTypeId) {
return customersServiceClient.getPetTypeByPetTypeId(petTypeId)
.map(petTypeResponseDTO -> ResponseEntity.status(HttpStatus.OK).body(petTypeResponseDTO))
.defaultIfEmpty(ResponseEntity.notFound().build());

/*.flatMap(owner ->
visitsServiceClient.getVisitsForPets(owner.getPetIds())
.map(addVisitsToOwner(owner))
);*/
}
@IsUserSpecific(idToMatch = {"petTypeId"}, bypassRoles = {Roles.ADMIN})
@DeleteMapping(value = "owners/petTypes/{petTypeId}")
public Mono<ResponseEntity<PetTypeResponseDTO>> deletePetTypeByPetTypeId(final @PathVariable String petTypeId){
return customersServiceClient.deletePetType(petTypeId).then(Mono.just(ResponseEntity.noContent().<PetTypeResponseDTO>build()))
.defaultIfEmpty(ResponseEntity.notFound().build());
}

@IsUserSpecific(idToMatch = {"petTypeId"})
@PutMapping("owners/petTypes/{petTypeId}")
public Mono<ResponseEntity<PetTypeResponseDTO>> updatePetType(
@PathVariable String petTypeId,
@RequestBody Mono<PetTypeRequestDTO> petTypeRequestMono) {
return petTypeRequestMono.flatMap(petTypeRequestDTO ->
customersServiceClient.updatePetType(petTypeId, Mono.just(petTypeRequestDTO))
.map(updatedOwner -> ResponseEntity.ok().body(updatedOwner))
.defaultIfEmpty(ResponseEntity.notFound().build())
);
}







}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.petclinic.customersservice.business;

import com.petclinic.customersservice.data.PetType;
import com.petclinic.customersservice.presentationlayer.OwnerRequestDTO;

Check warning on line 4 in customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import com.petclinic.customersservice.presentationlayer.OwnerRequestDTO;`
import com.petclinic.customersservice.presentationlayer.OwnerResponseDTO;

Check warning on line 5 in customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import com.petclinic.customersservice.presentationlayer.OwnerResponseDTO;`
import com.petclinic.customersservice.presentationlayer.PetTypeRequestDTO;
import com.petclinic.customersservice.presentationlayer.PetTypeResponseDTO;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
Expand All @@ -14,4 +16,13 @@ public interface PetTypeService {
//Flux<PetType> getAllPetTypes();

Flux<PetTypeResponseDTO> getAllPetTypes();
Mono<PetTypeResponseDTO> getPetTypeByPetTypeId(String petTypeId);

Mono<PetTypeResponseDTO> updatePetType(Mono<PetTypeRequestDTO> petTypeRequestDTO, String petTypeId);

Mono<Void> deletePetTypeByPetTypeId(String petTypeId);




}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.petclinic.customersservice.business;

import com.petclinic.customersservice.customersExceptions.exceptions.NotFoundException;
import com.petclinic.customersservice.data.PetType;
import com.petclinic.customersservice.data.PetTypeRepo;
import com.petclinic.customersservice.presentationlayer.OwnerResponseDTO;

Check warning on line 6 in customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import com.petclinic.customersservice.presentationlayer.OwnerResponseDTO;`
import com.petclinic.customersservice.presentationlayer.PetTypeRequestDTO;
import com.petclinic.customersservice.presentationlayer.PetTypeResponseDTO;
import com.petclinic.customersservice.util.EntityDTOUtil;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -30,6 +32,42 @@ public Flux<PetTypeResponseDTO> getAllPetTypes() {
.map(EntityDTOUtil::toPetTypeResponseDTO);
}

@Override
public Mono<PetTypeResponseDTO> getPetTypeByPetTypeId(String petTypeId) {

return petTypeRepo.findOPetTypeById(petTypeId)
.switchIfEmpty(Mono.error(new NotFoundException("Pet Type not found with id : " + petTypeId)))
.map(EntityDTOUtil::toPetTypeResponseDTO);

}

@Override
public Mono<PetTypeResponseDTO> updatePetType(Mono<PetTypeRequestDTO> petTypeRequestDTO, String petTypeId) {
return petTypeRepo.findOPetTypeById(petTypeId)
.flatMap(existingPetType -> petTypeRequestDTO.map(requestDTO -> {
existingPetType.setName(requestDTO.getName());
existingPetType.setPetTypeDescription(requestDTO.getPetTypeDescription());
return existingPetType;
} ))
.flatMap(petTypeRepo::save)
.map(EntityDTOUtil::toPetTypeResponseDTO);
}

@Override
public Mono<Void> deletePetTypeByPetTypeId(String petTypeId) {
return petTypeRepo.deleteById(petTypeId);
}

/*

Check notice on line 61 in customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (6 lines)
@Override
Mono<Void> deletePetTypeByPetTypeId(String petTypeId){
return petTypeRepo.deleteById(petTypeId);
}
*/


@Override
public Mono<PetType> getPetTypeById(Integer Id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
public interface PetTypeRepo extends ReactiveMongoRepository<PetType, Integer> {
Mono<PetType> findPetTypeById(Integer Id);

Check warning on line 9 in customers-service-reactive/src/main/java/com/petclinic/customersservice/data/PetTypeRepo.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Spring Data repository method parameters errors

Expected parameter types: **String**

Mono<PetType> findOPetTypeById(String petTypeId);

Mono<Void> deleteById(String petTypeId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.petclinic.customersservice.business.PetTypeService;
import com.petclinic.customersservice.data.PetType;

Check warning on line 4 in customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import com.petclinic.customersservice.data.PetType;`
import com.petclinic.customersservice.util.EntityDTOUtil;

Check warning on line 5 in customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import com.petclinic.customersservice.util.EntityDTOUtil;`
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequiredArgsConstructor
Expand All @@ -23,4 +25,31 @@ public class PetTypeController {
public Flux<PetTypeResponseDTO> getAllPetTypes() {
return petTypeService.getAllPetTypes();
}

@PutMapping("/{petTypeId}")
public Mono<ResponseEntity<PetTypeResponseDTO>> updatePetType(
@RequestBody Mono<PetTypeRequestDTO> petTypeRequestDTO,
@PathVariable String petTypeId) {

return petTypeService.updatePetType(petTypeRequestDTO, petTypeId)
.map(updatedPetType -> ResponseEntity.ok().body(updatedPetType))
.defaultIfEmpty(ResponseEntity.notFound().build());
}


@GetMapping("/{petTypeId}")
public Mono<ResponseEntity<PetTypeResponseDTO>> getPetTypeByPetTypeId(@PathVariable String petTypeId) {
return petTypeService.getPetTypeByPetTypeId(petTypeId)
.map(petTypeResponseDTO -> ResponseEntity.status(HttpStatus.OK).body(petTypeResponseDTO))
.defaultIfEmpty(ResponseEntity.notFound().build());
}

@DeleteMapping("/{petTypeId}")
public Mono<Void> DeletePetTypeByPetTypeId(@PathVariable String petTypeId) {
return petTypeService.deletePetTypeByPetTypeId(petTypeId);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.nio.charset.StandardCharsets;
Expand All @@ -29,6 +30,9 @@ class PetTypeControllerIntegrationTest {

PetType petTypeEntity2 = buildPetType2();

String PETTYPE_ID = petTypeEntity2.getId();
String PUBLIC_PETTYPE_ID = petTypeEntity2.getPetTypeId();


/*
@Test
Expand Down Expand Up @@ -73,6 +77,65 @@ void getAllPetTypes_shouldSucceed() {
}


@Test
void deletePetTypeByPetTypeId() {
petTypeRepo.save(petTypeEntity2);
Publisher<Void> setup = petTypeRepo.deleteById(PUBLIC_PETTYPE_ID);
StepVerifier.create(setup).expectNextCount(0).verifyComplete();
webTestClient.delete().uri("/owners/petTypes/" + PUBLIC_PETTYPE_ID)
.accept(MediaType.APPLICATION_JSON)
.exchange().expectStatus().isOk().expectBody();

}


/*
@Test
void updatePetType() {
Publisher<PetType> setup = petTypeRepo.deleteAll().thenMany(petTypeRepo.save(petTypeEntity2));
StepVerifier.create(setup).expectNextCount(1).verifyComplete();
webTestClient.put().uri("/owners/petTypes/" + PUBLIC_PETTYPE_ID)
.body(Mono.just(petTypeEntity2), PetType.class)
.accept(MediaType.APPLICATION_JSON)
.exchange().expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody()
.jsonPath("$.petTypeId").isEqualTo(petTypeEntity2.getPetTypeId())
.jsonPath("$.name").isEqualTo(petTypeEntity2.getName())
.jsonPath("$.petTypeDescription").isEqualTo(petTypeEntity2.getPetTypeDescription());
}
@Test
void getOwnerByOwnerId() {
Publisher<PetType> setup = petTypeRepo.deleteAll().thenMany(petTypeRepo.save(petTypeEntity2));
StepVerifier.create(setup).expectNextCount(1).verifyComplete();
webTestClient.get().uri("/owners/petTypes/" + PUBLIC_PETTYPE_ID)
.accept(MediaType.APPLICATION_JSON)
.exchange().expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON)
.expectBody(PetTypeResponseDTO.class)
.value(petTypeResponseDTO -> {
assertNotNull(petTypeResponseDTO);
assertEquals(petTypeResponseDTO.getPetTypeId(),petTypeEntity2.getPetTypeId());
assertEquals(petTypeResponseDTO.getName(),petTypeEntity2.getName());
assertEquals(petTypeResponseDTO.getPetTypeDescription(),petTypeEntity2.getPetTypeDescription());
});
}
*/







private PetType buildPetType() {
return PetType.builder().id("10").name("TestType").build();
}
Expand Down

0 comments on commit 5f3da92

Please sign in to comment.