| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 4 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 5 | import edu.ucsb.cs156.dining.services.CurrentUserService; | |
| 6 | import java.util.Map; | |
| 7 | import lombok.extern.slf4j.Slf4j; | |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | import org.springframework.http.HttpStatus; | |
| 10 | import org.springframework.http.ResponseEntity; | |
| 11 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 12 | import org.springframework.web.bind.annotation.ResponseStatus; | |
| 13 | ||
| 14 | /** This is an abstract class that provides common functionality for all API controllers. */ | |
| 15 | @Slf4j | |
| 16 | public abstract class ApiController { | |
| 17 | @Autowired private CurrentUserService currentUserService; | |
| 18 | ||
| 19 | /** | |
| 20 | * This method returns the current user. | |
| 21 | * | |
| 22 | * @return the current user | |
| 23 | */ | |
| 24 | protected CurrentUser getCurrentUser() { | |
| 25 |
1
1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/dining/controllers/ApiController::getCurrentUser → KILLED |
return currentUserService.getCurrentUser(); |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * This method returns a generic message. | |
| 30 | * | |
| 31 | * @param message the message | |
| 32 | * @return a map with the message | |
| 33 | */ | |
| 34 | protected Object genericMessage(String message) { | |
| 35 |
1
1. genericMessage : replaced return value with null for edu/ucsb/cs156/dining/controllers/ApiController::genericMessage → KILLED |
return Map.of("message", message); |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * This method handles the EntityNotFoundException. | |
| 40 | * | |
| 41 | * @param e the exception | |
| 42 | * @return a map with the type and message of the exception | |
| 43 | */ | |
| 44 | @ExceptionHandler({EntityNotFoundException.class}) | |
| 45 | @ResponseStatus(HttpStatus.NOT_FOUND) | |
| 46 | public Object handleGenericException(Throwable e) { | |
| 47 |
1
1. handleGenericException : replaced return value with null for edu/ucsb/cs156/dining/controllers/ApiController::handleGenericException → KILLED |
return Map.of( |
| 48 | "type", e.getClass().getSimpleName(), | |
| 49 | "message", e.getMessage()); | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * This method handles the UnsupportedOperationException. This maps to a 403/Forbidden. | |
| 54 | * | |
| 55 | * @param e the exception | |
| 56 | * @return a map with the type and message of the exception | |
| 57 | */ | |
| 58 | @ExceptionHandler(UnsupportedOperationException.class) | |
| 59 | public ResponseEntity<Map<String, String>> handleUnsupportedOperation( | |
| 60 | UnsupportedOperationException e) { | |
| 61 |
1
1. handleUnsupportedOperation : replaced return value with null for edu/ucsb/cs156/dining/controllers/ApiController::handleUnsupportedOperation → KILLED |
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(Map.of("message", e.getMessage())); |
| 62 | } | |
| 63 | } | |
Mutations | ||
| 25 |
1.1 |
|
| 35 |
1.1 |
|
| 47 |
1.1 |
|
| 61 |
1.1 |