diff --git a/api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java b/api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java index 0848f24134..c82ab3fc3d 100755 --- a/api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java +++ b/api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java @@ -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; @@ -263,4 +260,29 @@ public Flux getAllPetTypes() { .retrieve() .bodyToFlux(PetTypeResponseDTO.class); } + public Mono getPetTypeByPetTypeId(String petTypeId) { + return webClientBuilder.build().get() + .uri(customersServiceUrl + "/owners/petTypes/" + petTypeId) + .retrieve() + .bodyToMono(PetTypeResponseDTO.class); + } + + public Mono deletePetType(final String petTypeId) { + return webClientBuilder.build().delete() + .uri(customersServiceUrl +"/owners/petTypes/"+ petTypeId) + .retrieve() + .bodyToMono(PetTypeResponseDTO.class); + } + + public Mono updatePetType(String petTypeId, Mono petTypeRequestDTO) { + return petTypeRequestDTO.flatMap(requestDTO -> + webClientBuilder.build() + .put() + .uri(customersServiceUrl + "/owners/petTypes/" + petTypeId) + .body(BodyInserters.fromValue(requestDTO)) + .retrieve() + .bodyToMono(PetTypeResponseDTO.class) + ); + } + } diff --git a/api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java b/api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java index d876e8588c..68fc527953 100644 --- a/api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java +++ b/api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java @@ -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; @@ -975,4 +972,42 @@ public Flux getAllPetTypes() { .map(addVisitsToOwner(n)) );*/ } + + @IsUserSpecific(idToMatch = {"petTypeId"}, bypassRoles = {Roles.ALL}) + @GetMapping(value = "owners/petTypes/{petTypeId}") + public Mono> 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> deletePetTypeByPetTypeId(final @PathVariable String petTypeId){ + return customersServiceClient.deletePetType(petTypeId).then(Mono.just(ResponseEntity.noContent().build())) + .defaultIfEmpty(ResponseEntity.notFound().build()); + } + + @IsUserSpecific(idToMatch = {"petTypeId"}) + @PutMapping("owners/petTypes/{petTypeId}") + public Mono> updatePetType( + @PathVariable String petTypeId, + @RequestBody Mono petTypeRequestMono) { + return petTypeRequestMono.flatMap(petTypeRequestDTO -> + customersServiceClient.updatePetType(petTypeId, Mono.just(petTypeRequestDTO)) + .map(updatedOwner -> ResponseEntity.ok().body(updatedOwner)) + .defaultIfEmpty(ResponseEntity.notFound().build()) + ); + } + + + + + + + } \ No newline at end of file diff --git a/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java b/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java index 24958a9c8a..a801af3b73 100644 --- a/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java +++ b/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeService.java @@ -1,7 +1,9 @@ package com.petclinic.customersservice.business; import com.petclinic.customersservice.data.PetType; +import com.petclinic.customersservice.presentationlayer.OwnerRequestDTO; 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; @@ -14,4 +16,13 @@ public interface PetTypeService { //Flux getAllPetTypes(); Flux getAllPetTypes(); + Mono getPetTypeByPetTypeId(String petTypeId); + + Mono updatePetType(Mono petTypeRequestDTO, String petTypeId); + + Mono deletePetTypeByPetTypeId(String petTypeId); + + + + } diff --git a/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java b/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java index 8660d00f19..a43bf7b147 100644 --- a/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java +++ b/customers-service-reactive/src/main/java/com/petclinic/customersservice/business/PetTypeServiceImpl.java @@ -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; +import com.petclinic.customersservice.presentationlayer.PetTypeRequestDTO; import com.petclinic.customersservice.presentationlayer.PetTypeResponseDTO; import com.petclinic.customersservice.util.EntityDTOUtil; import lombok.extern.slf4j.Slf4j; @@ -30,6 +32,42 @@ public Flux getAllPetTypes() { .map(EntityDTOUtil::toPetTypeResponseDTO); } + @Override + public Mono 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 updatePetType(Mono 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 deletePetTypeByPetTypeId(String petTypeId) { + return petTypeRepo.deleteById(petTypeId); + } + + /* + @Override + Mono deletePetTypeByPetTypeId(String petTypeId){ + + return petTypeRepo.deleteById(petTypeId); + + } + + */ + @Override public Mono getPetTypeById(Integer Id) { diff --git a/customers-service-reactive/src/main/java/com/petclinic/customersservice/data/PetTypeRepo.java b/customers-service-reactive/src/main/java/com/petclinic/customersservice/data/PetTypeRepo.java index 830778029b..0a6b2462b0 100644 --- a/customers-service-reactive/src/main/java/com/petclinic/customersservice/data/PetTypeRepo.java +++ b/customers-service-reactive/src/main/java/com/petclinic/customersservice/data/PetTypeRepo.java @@ -8,4 +8,9 @@ public interface PetTypeRepo extends ReactiveMongoRepository { Mono findPetTypeById(Integer Id); + Mono findOPetTypeById(String petTypeId); + + Mono deleteById(String petTypeId); + + } diff --git a/customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java b/customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java index 67aadd5d9c..04e5d49f0a 100644 --- a/customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java +++ b/customers-service-reactive/src/main/java/com/petclinic/customersservice/presentationlayer/PetTypeController.java @@ -2,13 +2,15 @@ import com.petclinic.customersservice.business.PetTypeService; import com.petclinic.customersservice.data.PetType; +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 @@ -23,4 +25,31 @@ public class PetTypeController { public Flux getAllPetTypes() { return petTypeService.getAllPetTypes(); } + + @PutMapping("/{petTypeId}") + public Mono> updatePetType( + @RequestBody Mono petTypeRequestDTO, + @PathVariable String petTypeId) { + + return petTypeService.updatePetType(petTypeRequestDTO, petTypeId) + .map(updatedPetType -> ResponseEntity.ok().body(updatedPetType)) + .defaultIfEmpty(ResponseEntity.notFound().build()); + } + + + @GetMapping("/{petTypeId}") + public Mono> getPetTypeByPetTypeId(@PathVariable String petTypeId) { + return petTypeService.getPetTypeByPetTypeId(petTypeId) + .map(petTypeResponseDTO -> ResponseEntity.status(HttpStatus.OK).body(petTypeResponseDTO)) + .defaultIfEmpty(ResponseEntity.notFound().build()); + } + + @DeleteMapping("/{petTypeId}") + public Mono DeletePetTypeByPetTypeId(@PathVariable String petTypeId) { + return petTypeService.deletePetTypeByPetTypeId(petTypeId); + } + + + + } diff --git a/customers-service-reactive/src/test/java/com/petclinic/customersservice/presentationlayer/PetTypeControllerIntegrationTest.java b/customers-service-reactive/src/test/java/com/petclinic/customersservice/presentationlayer/PetTypeControllerIntegrationTest.java index 90e7116080..caff17a857 100644 --- a/customers-service-reactive/src/test/java/com/petclinic/customersservice/presentationlayer/PetTypeControllerIntegrationTest.java +++ b/customers-service-reactive/src/test/java/com/petclinic/customersservice/presentationlayer/PetTypeControllerIntegrationTest.java @@ -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; @@ -29,6 +30,9 @@ class PetTypeControllerIntegrationTest { PetType petTypeEntity2 = buildPetType2(); + String PETTYPE_ID = petTypeEntity2.getId(); + String PUBLIC_PETTYPE_ID = petTypeEntity2.getPetTypeId(); + /* @Test @@ -73,6 +77,65 @@ void getAllPetTypes_shouldSucceed() { } + @Test + void deletePetTypeByPetTypeId() { + petTypeRepo.save(petTypeEntity2); + Publisher 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 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 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(); }