| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import net.bytebuddy.implementation.bytecode.Throw; | |
| 4 | import org.springframework.beans.factory.annotation.Autowired; | |
| 5 | ||
| 6 | import lombok.extern.slf4j.Slf4j; | |
| 7 | import org.springframework.http.HttpStatus; | |
| 8 | import org.springframework.web.bind.annotation.ExceptionHandler; | |
| 9 | import org.springframework.web.bind.annotation.ResponseStatus; | |
| 10 | ||
| 11 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 12 | import edu.ucsb.cs156.gauchoride.models.CurrentUser; | |
| 13 | import edu.ucsb.cs156.gauchoride.services.CurrentUserService; | |
| 14 | ||
| 15 | import java.nio.file.AccessDeniedException; | |
| 16 | import java.util.Map; | |
| 17 | ||
| 18 | /** | |
| 19 |  * Base class for all API controllers. | |
| 20 |  *  | |
| 21 |  * Provides a method to get the current user as well as common methods for | |
| 22 |  * error handling. | |
| 23 |  */ | |
| 24 | ||
| 25 | @Slf4j | |
| 26 | public abstract class ApiController { | |
| 27 |   @Autowired | |
| 28 |   private CurrentUserService currentUserService; | |
| 29 | ||
| 30 |   /** | |
| 31 |    * Get the current user | |
| 32 |    *  | |
| 33 |    * @return the current user | |
| 34 |    */ | |
| 35 | ||
| 36 |   protected CurrentUser getCurrentUser() { | |
| 37 | 
1
1. getCurrentUser : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ApiController::getCurrentUser → KILLED | 
    return currentUserService.getCurrentUser(); | 
| 38 |   } | |
| 39 | ||
| 40 |   /** | |
| 41 |    * This creates a plain old java object that can be returned as a JSON response | |
| 42 |    *  | |
| 43 |    * @return a Map object with a single key/value pair: "message" => message | |
| 44 |    */ | |
| 45 | ||
| 46 |   protected Object genericMessage(String message) { | |
| 47 | 
1
1. genericMessage : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ApiController::genericMessage → KILLED | 
    return Map.of("message", message); | 
| 48 |   } | |
| 49 | ||
| 50 |   /** | |
| 51 |    * This catches any EntityNotFoundExceptions and returns a 404 (NOT_FOUND) | |
| 52 |    * response | |
| 53 |    *  | |
| 54 |    * @return a Map object that can be returned as a JSON response | |
| 55 |    */ | |
| 56 |   @ExceptionHandler({ EntityNotFoundException.class }) | |
| 57 |   @ResponseStatus(HttpStatus.NOT_FOUND) | |
| 58 |   public Object handleGenericException(Throwable e) { | |
| 59 | 
1
1. handleGenericException : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/ApiController::handleGenericException → KILLED | 
    return Map.of( | 
| 60 |         "type", e.getClass().getSimpleName(), | |
| 61 |         "message", e.getMessage()); | |
| 62 |   } | |
| 63 | } | |
Mutations | ||
| 37 | 
 
 1.1  | 
|
| 47 | 
 
 1.1  | 
|
| 59 | 
 
 1.1  |