From e2f0a66a438114a177f387d6f0c8f5d8497178c4 Mon Sep 17 00:00:00 2001 From: AmirHamzahMirza <83139031+AmirHamzahMirza@users.noreply.github.com> Date: Tue, 24 Oct 2023 08:51:48 -0400 Subject: [PATCH] Front-end perms and back-end content for exceptions of owners Nothing to really discuss besides what is already defined --- .../config/GlobalExceptionHandler.java | 12 ++++++++++++ .../config/GlobalServletExceptionHandler.java | 6 +++++- .../CustomersServiceClient.java | 13 +++++++++++++ .../exceptions/NotFoundException.java | 12 ++++++++++++ .../BFFApiGatewayController.java | 8 -------- .../resources/static/scripts/fragments/nav.html | 16 ++++++++++------ .../scripts/owner-details/owner-details.js | 1 - .../scripts/vet-list/vet-list.controller.js | 4 ++-- .../scripts/vet-list/vet-list.template.html | 13 ++++--------- 9 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 api-gateway/src/main/java/com/petclinic/bffapigateway/exceptions/NotFoundException.java diff --git a/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java b/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java index f0fadd8987..05871cfa8b 100644 --- a/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java +++ b/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -16,6 +17,8 @@ import java.util.HashMap; import java.util.Map; +import static org.springframework.http.HttpStatus.NOT_FOUND; + /** * Created by IntelliJ IDEA. @@ -129,4 +132,13 @@ public ResponseEntity handleForbiddenAccessException(ForbiddenAcc .body(new HttpErrorInfo(HttpStatus.FORBIDDEN.value(),ex.getMessage())); } + //I have no idea why but this method kills docker and specifically + //the api gateway as a service crashes on docker if you have this uncommented +// @ExceptionHandler(value = NotFoundException.class) +// public ResponseEntity handleNotFoundException(NotFoundException ex) { +// return ResponseEntity.status(NOT_FOUND) +// .body(new HttpErrorInfo(NOT_FOUND.value(),ex.getMessage())); +// +// } + } \ No newline at end of file diff --git a/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalServletExceptionHandler.java b/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalServletExceptionHandler.java index d66e2f449d..44c369e5ca 100644 --- a/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalServletExceptionHandler.java +++ b/api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalServletExceptionHandler.java @@ -48,7 +48,11 @@ else if(exClass.equals(NullPointerException.class)){ } else if(exClass.equals(ForbiddenAccessException.class)){ status = HttpStatus.FORBIDDEN; - } // Handle any other exception types here + + } + else if(exClass.equals(NotFoundException.class)){ + status = HttpStatus.NOT_FOUND; + }// Handle any other exception types here else { log.error("Exception not handled: {}", exClass.getSimpleName()); status = HttpStatus.UNPROCESSABLE_ENTITY; 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 a3fe3b25d5..fbac937795 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 @@ -6,8 +6,11 @@ import com.petclinic.bffapigateway.dtos.Pets.PetResponseDTO; import com.petclinic.bffapigateway.dtos.Pets.PetType; import com.petclinic.bffapigateway.dtos.Vets.PhotoDetails; +import com.petclinic.bffapigateway.exceptions.NotFoundException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestParam; @@ -42,6 +45,16 @@ public Mono getOwner(final String ownerId) { return webClientBuilder.build().get() .uri(customersServiceUrl + "/owners/" + ownerId) .retrieve() + .onStatus(HttpStatusCode::is4xxClientError, error->{ + HttpStatusCode statusCode = error.statusCode(); + + if (statusCode.equals(HttpStatus.NOT_FOUND)) + return Mono.error(new NotFoundException("Course id not found:" + ownerId)); + return Mono.error(new IllegalArgumentException("Something went wrong(owner Client)")); + }) + .onStatus(HttpStatusCode::is5xxServerError, error-> { + return Mono.error(new IllegalArgumentException("Something went wrong(owner Client")); + }) .bodyToMono(OwnerResponseDTO.class); } diff --git a/api-gateway/src/main/java/com/petclinic/bffapigateway/exceptions/NotFoundException.java b/api-gateway/src/main/java/com/petclinic/bffapigateway/exceptions/NotFoundException.java new file mode 100644 index 0000000000..a48df23d50 --- /dev/null +++ b/api-gateway/src/main/java/com/petclinic/bffapigateway/exceptions/NotFoundException.java @@ -0,0 +1,12 @@ +package com.petclinic.bffapigateway.exceptions; + +public class NotFoundException extends RuntimeException{ + + public NotFoundException() {} + + public NotFoundException(String message) { super(message); } + + public NotFoundException(Throwable cause) { super(cause); } + + public NotFoundException(String message, Throwable cause) { super(message, cause); } +} \ No newline at end of file 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 36aa4395d3..53ab3fea1e 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 @@ -551,14 +551,6 @@ public Mono> updateOwner( ); } - - - - - - - - @DeleteMapping(value = "owners/{ownerId}") public Mono> deleteOwner(@PathVariable String ownerId){ return customersServiceClient.deleteOwner(ownerId).then(Mono.just(ResponseEntity.noContent().build())) diff --git a/api-gateway/src/main/resources/static/scripts/fragments/nav.html b/api-gateway/src/main/resources/static/scripts/fragments/nav.html index e71072719a..5c2b674f03 100644 --- a/api-gateway/src/main/resources/static/scripts/fragments/nav.html +++ b/api-gateway/src/main/resources/static/scripts/fragments/nav.html @@ -4,7 +4,11 @@ localStorage.removeItem("email") localStorage.removeItem("UUID") localStorage.removeItem("roles") -} + } + + //access the roles title and make sure it exists + const userRoles = localStorage.getItem('roles') + console.log(userRoles); diff --git a/api-gateway/src/main/resources/static/scripts/owner-details/owner-details.js b/api-gateway/src/main/resources/static/scripts/owner-details/owner-details.js index 4b90325713..b69a0a090a 100644 --- a/api-gateway/src/main/resources/static/scripts/owner-details/owner-details.js +++ b/api-gateway/src/main/resources/static/scripts/owner-details/owner-details.js @@ -15,7 +15,6 @@ angular.module('ownerDetails', ['ui.router']) // params: {ownerId: null, petId: null}, // template: '' // } - }]); diff --git a/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.controller.js b/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.controller.js index 865d7c164e..c689d672f4 100755 --- a/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.controller.js +++ b/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.controller.js @@ -52,7 +52,7 @@ angular.module('vetList') $http.get('api/gateway/vets/' + vet.vetId + "/ratings/average").then(function (resp) { console.log(resp.data); vet.showRating = true; - vet.rating = parseFloat(resp.data.toFixed(1)); + vet.rating = parseFloat(resp.data).toFixed(1); }); } @@ -60,7 +60,7 @@ angular.module('vetList') $http.get('api/gateway/vets/topVets').then(function (resp) { console.log(resp.data); vet.showRating=true; - vet.rating = parseFloat(resp.data.toFixed(1)); + vet.rating = parseFloat(resp.data).toFixed(1); }); diff --git a/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.template.html b/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.template.html index 6f29ea5aa7..43cd168da1 100644 --- a/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.template.html +++ b/api-gateway/src/main/resources/static/scripts/vet-list/vet-list.template.html @@ -20,8 +20,8 @@

Veterinarians

- - + +
@@ -44,7 +44,7 @@

Veterinarians

Specialties Average Rating Number of Ratings - Delete + Delete @@ -59,14 +59,9 @@

Veterinarians

{{specialty.name + ' '}} {{vet.rating}} {{vet.count}} - Delete Vet + Delete Vet - - - - -