Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Front-end perms and back-end content for exceptions of owners #597

Merged
merged 4 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;

Check warning on line 7 in api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import org.springframework.http.server.reactive.ServerHttpRequest;`
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;

Check warning on line 9 in api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import org.springframework.web.bind.MethodArgumentNotValidException;`
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -16,6 +17,8 @@
import java.util.HashMap;
import java.util.Map;

import static org.springframework.http.HttpStatus.NOT_FOUND;

Check warning on line 20 in api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import static org.springframework.http.HttpStatus.NOT_FOUND;`


/**
* Created by IntelliJ IDEA.
Expand Down Expand Up @@ -129,4 +132,13 @@
.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)

Check notice on line 137 in api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (6 lines)
// public ResponseEntity<HttpErrorInfo> handleNotFoundException(NotFoundException ex) {
// return ResponseEntity.status(NOT_FOUND)
// .body(new HttpErrorInfo(NOT_FOUND.value(),ex.getMessage()));
//
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
status = HttpStatus.valueOf((Integer.parseInt(ex.getMessage().substring(0, 3))));
log.debug("Exception type: {}", ex.getClass().getSimpleName());
log.debug("Exception message: {}", ex.getMessage());
log.debug("Status found in 3 digits : "+status.toString());

Check warning on line 28 in api-gateway/src/main/java/com/petclinic/bffapigateway/config/GlobalServletExceptionHandler.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unnecessary call to 'toString()'

Unnecessary `toString()` call
}
catch (Exception e){

Expand All @@ -48,7 +48,11 @@
}
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import com.petclinic.bffapigateway.dtos.CustomerDTOs.OwnerResponseDTO;
import com.petclinic.bffapigateway.dtos.Pets.*;
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;

Check warning on line 14 in api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unused import

Unused import `import org.springframework.web.bind.annotation.RequestParam;`
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
Expand Down Expand Up @@ -41,6 +44,16 @@
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);
}

Expand All @@ -59,7 +72,7 @@
builder.queryParam("size",size);

// Add query parameters conditionally if they are not null or empty
if (ownerId != null && !ownerId.isEmpty()) {

Check notice on line 75 in api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Duplicated code fragment

Duplicated code
builder.queryParam("ownerId", ownerId);
}
if (firstName != null && !firstName.isEmpty()) {
Expand Down Expand Up @@ -150,7 +163,7 @@
.bodyValue(model)
.retrieve()
.bodyToMono(OwnerResponseDTO.class);
// return webClientBuilder.build()

Check notice on line 166 in api-gateway/src/main/java/com/petclinic/bffapigateway/domainclientlayer/CustomersServiceClient.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (8 lines)
// .post()
// .uri(customersServiceUrl + "/owners")
// .accept(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -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); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* @author Maciej Szarlinski
* @author Christine Gerard
* Copied from https://github.com/spring-petclinic/spring-petclinic-microservices

Check warning on line 37 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Link specified as plain text

Link specified as plain text
* Modified to remove circuitbreaker
*/

Expand Down Expand Up @@ -158,7 +158,7 @@
.defaultIfEmpty(ResponseEntity.badRequest().build());
}

/*@GetMapping(value = "owners/{ownerId}/pets")

Check notice on line 161 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (4 lines)
public Flux<PetResponseDTO> getAllPetsFromOwnerId(@PathVariable String ownerId){
return customersServiceClient.getAllPets(ownerId);
}*/
Expand Down Expand Up @@ -284,7 +284,7 @@
.defaultIfEmpty(ResponseEntity.notFound().build());
}
// @PutMapping(value = "owners/*/pets/{petId}/visits/{visitId}", consumes = "application/json", produces = "application/json")
/*

Check notice on line 287 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (33 lines)
Mono<ResponseEntity<VisitDetails>> updateVisit(@RequestBody VisitDetails visit, @PathVariable String petId, @PathVariable String visitId) {
visit.setPetId(petId);
visit.setVisitId(visitId);
Expand Down Expand Up @@ -327,7 +327,7 @@
.defaultIfEmpty(ResponseEntity.badRequest().build());
}

// @PutMapping(value = "owners/*/pets/{petId}/visits/{visitId}", consumes = "application/json", produces = "application/json")

Check notice on line 330 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (7 lines)
// Mono<ResponseEntity<VisitDetails>> updateVisit(@RequestBody VisitDetails visit, @PathVariable String petId, @PathVariable String visitId) {
// visit.setPetId(petId);
// visit.setVisitId(visitId);
Expand All @@ -336,7 +336,7 @@
// }
/* End of Visit Methods */

/**

Check warning on line 339 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Dangling Javadoc comment

Dangling Javadoc comment
* End of Visit Methods
**/

Expand Down Expand Up @@ -500,7 +500,7 @@

@IsUserSpecific(idToMatch = {"vetId"})
@GetMapping("/vets/vetBillId/{vetId}")
public Mono<ResponseEntity<VetResponseDTO>> getVetByVetBillId(@PathVariable String vetBillId) {

Check warning on line 503 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Mismatch in @PathVariable declarations and usages

Cannot resolve path variable 'vetBillId' in request mapping

Check notice on line 503 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Mismatch in @PathVariable declarations and usages

Path variable 'vetId' is not consumed
return vetsServiceClient.getVetByVetBillId(VetsEntityDtoUtil.verifyId(vetBillId))
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
Expand Down Expand Up @@ -539,10 +539,10 @@
.defaultIfEmpty(ResponseEntity.notFound().build());
}

/**

Check warning on line 542 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Dangling Javadoc comment

Dangling Javadoc comment
* End of Vet Methods
**/
//

Check notice on line 545 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (16 lines)
// @DeleteMapping(value = "users/{userId}")
// public Mono<UserDetails> deleteUser(@RequestHeader(AUTHORIZATION) String auth, final @PathVariable long userId) {
// return authServiceClient.deleteUser(auth, userId);
Expand Down Expand Up @@ -632,7 +632,7 @@
}


// @PostMapping(value = "owners",

Check notice on line 635 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (6 lines)
// consumes = "application/json",
// produces = "application/json")
// public Mono<OwnerResponseDTO> createOwner(@RequestBody OwnerResponseDTO model){
Expand All @@ -646,12 +646,12 @@
.defaultIfEmpty(ResponseEntity.badRequest().build());
}

/*@GetMapping(value = "owners/photo/{ownerId}")

Check notice on line 649 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (4 lines)
public Mono<PhotoDetails> getOwnerPhoto(@PathVariable int ownerId) {
return customersServiceClient.getOwnerPhoto(ownerId);
}*/

// @PostMapping(value = "owners/{ownerId}/pet/photo/{petId}")

Check notice on line 654 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (19 lines)
// public Mono<String> setPetPhoto(@PathVariable String ownerId, @RequestBody PhotoDetails photoDetails, @PathVariable String petId) {
// return customersServiceClient.setPetPhoto(ownerId, photoDetails, petId);
// }
Expand All @@ -675,7 +675,7 @@



/*

Check notice on line 678 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Commented out code

Commented out code (9 lines)

// Endpoint to update an owner
@PutMapping("owners/{ownerId}")
Expand Down Expand Up @@ -703,12 +703,6 @@
}








@SecuredEndpoint(allowedRoles = {Roles.ADMIN})
@DeleteMapping(value = "owners/{ownerId}")
public Mono<ResponseEntity<OwnerResponseDTO>> deleteOwner(@PathVariable String ownerId){
Expand All @@ -716,7 +710,7 @@
.defaultIfEmpty(ResponseEntity.notFound().build());
}

/**

Check warning on line 713 in api-gateway/src/main/java/com/petclinic/bffapigateway/presentationlayer/BFFApiGatewayController.java

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Dangling Javadoc comment

Dangling Javadoc comment
* End of Owner Methods
**/

Expand Down
25 changes: 21 additions & 4 deletions api-gateway/src/main/resources/static/scripts/fragments/nav.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<script>

function purgeUser() {
localStorage.removeItem("username")
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);

function logoutUser() {
logoutUser.$inject = ['$http'];
var $http = angular.injector(['ng']).get('$http');
Expand All @@ -20,6 +32,7 @@
localStorage.removeItem("roles")
window.location.href = '/#!/login';
}); }

</script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
Expand All @@ -42,16 +55,22 @@
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="nav-link" ui-sref="owners">Owners</a>

<a class="nav-link" ui-sref="ownerDetails({ ownerId: UUID })" ng-if = "roles == 'OWNER'" >Edit Account</a>

<a class="nav-link" ui-sref="ownerDetails({ ownerId: UUID })">Edit Account</a>
<a class="nav-link" ui-sref="petTypes">Pet Types</a>

</div>
</li>
<li class="nav-item">
<li class="nav-item" ng-if = "roles !== 'OWNER'">
<a class="nav-link" ui-sref="bills">Bills</a>
</li>
<li class="nav-item">

<li class="nav-item" ng-if = "roles !== 'OWNER'">
<a class="nav-link" ui-sref="visitList">Visits</a>
</li>

<li class="nav-item">
<a class="nav-link" ui-sref="inventories">Inventory</a>
</li>
Expand All @@ -75,7 +94,5 @@
<a class="nav-link" ui-sref="login">Login</a>
</li>
</ul>


</div>
</nav>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ angular.module('ownerDetails', ['ui.router'])
// params: {ownerId: null, petId: null},
// template: '<owner-details></owner-details>'
// }

}]);


Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ 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);
});
}

function getTopThreeVetsWithHighestRating(vet){
$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);

});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ <h2>Veterinarians</h2>
</form>


<a ng-if = "roles !== 'OWNER'" ui-sref="vetsNew" style="float: right;margin-bottom: 1%">
<button ng-if = "roles !== 'OWNER'" class="add-vet-button btn btn-success">Add Vet</button>
</a>


<div style="margin-bottom: 1%; margin-top: 1%">
<label for="filterOption">Filter</label>
Expand All @@ -41,7 +45,7 @@ <h2>Veterinarians</h2>
<th class="vet_speciality">Specialties</th>
<th class="getAverageRating">Average Rating</th>
<th class="getCountOfRatings">Number of Ratings</th>
<th>Delete</th>
<th ng-if = "roles !== 'OWNER'" >Delete</th>
</tr>
</thead>

Expand All @@ -56,14 +60,9 @@ <h2>Veterinarians</h2>
<td class="vet_speciality"><span ng-repeat="specialty in vet.specialties">{{specialty.name + ' '}}</span></td>
<td class="vet-rating v{{vet.vetId}} centered-count"><span>{{vet.rating}}</span></td>
<td class="vet-rating v{{vet.vetId}} centered-count"><span>{{vet.count}}</span></td>
<td><a class="btn btn-danger" href="javascript:void(0)" ng-click="deleteVet(vet.vetId)">Delete Vet</a></td>
<td ng-if = "roles !== 'OWNER'" ><a class="btn btn-danger" href="javascript:void(0)" ng-click="deleteVet(vet.vetId)" >Delete Vet</a></td>
</tr>






<div class="modal m{{vet.vetId}} modalOff" ng-repeat="vet in $ctrl.vetList" >
<span class="modal_image"><img ng-if="vet.image != null" src="data:image/png;base64,{{vet.image}}"/><img
ng-if="vet.image == null" src="images/vet_default.jpg"/></span>
Expand Down
Loading