| 1 | package edu.ucsb.cs156.gauchoride.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import edu.ucsb.cs156.gauchoride.entities.User; | |
| 7 | import edu.ucsb.cs156.gauchoride.repositories.UserRepository; | |
| 8 | ||
| 9 | import edu.ucsb.cs156.gauchoride.errors.EntityNotFoundException; | |
| 10 | ||
| 11 | ||
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.http.ResponseEntity; | |
| 14 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 15 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 16 | import org.springframework.web.bind.annotation.GetMapping; | |
| 17 | import org.springframework.web.bind.annotation.PostMapping; | |
| 18 | import org.springframework.web.bind.annotation.PathVariable; | |
| 19 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestParam; | |
| 21 | import org.springframework.web.bind.annotation.RestController; | |
| 22 | ||
| 23 | import io.swagger.annotations.Api; | |
| 24 | import io.swagger.annotations.ApiOperation; | |
| 25 | import io.swagger.annotations.ApiParam; | |
| 26 | ||
| 27 | ||
| 28 | @Api(description = "User information (admin only)") | |
| 29 | @RequestMapping("/api/admin/users") | |
| 30 | @RestController | |
| 31 | public class UsersController extends ApiController { | |
| 32 |     @Autowired | |
| 33 |     UserRepository userRepository; | |
| 34 | ||
| 35 |     @Autowired | |
| 36 |     ObjectMapper mapper; | |
| 37 | ||
| 38 |     @ApiOperation(value = "Get a list of all users") | |
| 39 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 40 |     @GetMapping("") | |
| 41 |     public ResponseEntity<String> users() | |
| 42 |             throws JsonProcessingException { | |
| 43 |         Iterable<User> users = userRepository.findAll(); | |
| 44 |         String body = mapper.writeValueAsString(users); | |
| 45 | 
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 46 |     } | |
| 47 | ||
| 48 |     @ApiOperation(value = "Get user by id") | |
| 49 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 50 |     @GetMapping("/get") | |
| 51 |     public User users( | |
| 52 |             @ApiParam("id") @RequestParam Long id) | |
| 53 |             throws JsonProcessingException { | |
| 54 |         User user = userRepository.findById(id) | |
| 55 | 
1
1. lambda$users$0 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$users$0 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 56 | 
1
1. users : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::users → KILLED | 
        return user; | 
| 57 |     } | |
| 58 | ||
| 59 |     @ApiOperation(value = "Delete a user (admin)") | |
| 60 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 |     @DeleteMapping("/delete") | |
| 62 |     public Object deleteUser_Admin( | |
| 63 |             @ApiParam("id") @RequestParam Long id) { | |
| 64 |               User user = userRepository.findById(id) | |
| 65 | 
1
1. lambda$deleteUser_Admin$1 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$deleteUser_Admin$1 → KILLED | 
          .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 66 | ||
| 67 | 
1
1. deleteUser_Admin : removed call to edu/ucsb/cs156/gauchoride/repositories/UserRepository::delete → KILLED | 
          userRepository.delete(user); | 
| 68 | ||
| 69 | 
1
1. deleteUser_Admin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::deleteUser_Admin → KILLED | 
        return genericMessage("User with id %s deleted".formatted(id)); | 
| 70 |     } | |
| 71 | ||
| 72 |      | |
| 73 |     @ApiOperation(value = "Toggle the admin field") | |
| 74 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 75 |     @PostMapping("/toggleAdmin") | |
| 76 |     public Object toggleAdmin( @ApiParam("id") @RequestParam Long id){ | |
| 77 |         User user = userRepository.findById(id) | |
| 78 | 
1
1. lambda$toggleAdmin$2 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleAdmin$2 → KILLED | 
        .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 79 |         boolean oldAdminStatus = user.getAdmin(); | |
| 80 | 
2
1. toggleAdmin : negated conditional → KILLED 2. toggleAdmin : removed call to edu/ucsb/cs156/gauchoride/entities/User::setAdmin → KILLED  | 
        user.setAdmin(!user.getAdmin()); | 
| 81 |         userRepository.save(user); | |
| 82 | 
1
1. toggleAdmin : negated conditional → KILLED | 
        String status = oldAdminStatus ? "true to false" : "false to true"; | 
| 83 | 
1
1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleAdmin → KILLED | 
        return genericMessage("User with id %s has toggled admin status from %s".formatted(id, status)); | 
| 84 |     } | |
| 85 | ||
| 86 |        | |
| 87 |     @ApiOperation(value = "Toggle the Driver field") | |
| 88 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 89 |     @PostMapping("/toggleDriver") | |
| 90 |     public Object toggleDriver( @ApiParam("id") @RequestParam Long id){ | |
| 91 |         User user = userRepository.findById(id) | |
| 92 | 
1
1. lambda$toggleDriver$3 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleDriver$3 → KILLED | 
        .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 93 |         boolean oldDriverStatus = user.getDriver(); | |
| 94 | 
2
1. toggleDriver : negated conditional → KILLED 2. toggleDriver : removed call to edu/ucsb/cs156/gauchoride/entities/User::setDriver → KILLED  | 
        user.setDriver(!user.getDriver()); | 
| 95 |         userRepository.save(user); | |
| 96 | 
1
1. toggleDriver : negated conditional → KILLED | 
        String status = oldDriverStatus ? "true to false" : "false to true"; | 
| 97 | 
1
1. toggleDriver : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleDriver → KILLED | 
        return genericMessage("User with id %s has toggled driver status from %s".formatted(id, status)); | 
| 98 |     } | |
| 99 | ||
| 100 |     @ApiOperation(value = "Toggle the Rider field") | |
| 101 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 102 |     @PostMapping("/toggleRider") | |
| 103 |     public Object toggleRider( @ApiParam("id") @RequestParam Long id){ | |
| 104 |         User user = userRepository.findById(id) | |
| 105 | 
1
1. lambda$toggleRider$4 : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::lambda$toggleRider$4 → KILLED | 
        .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 106 |         boolean oldRiderStatus = user.getRider(); | |
| 107 | 
2
1. toggleRider : negated conditional → KILLED 2. toggleRider : removed call to edu/ucsb/cs156/gauchoride/entities/User::setRider → KILLED  | 
        user.setRider(!user.getRider()); | 
| 108 |         userRepository.save(user); | |
| 109 | 
1
1. toggleRider : negated conditional → KILLED | 
        String status = oldRiderStatus ? "true to false" : "false to true"; | 
| 110 | 
1
1. toggleRider : replaced return value with null for edu/ucsb/cs156/gauchoride/controllers/UsersController::toggleRider → KILLED | 
        return genericMessage("User with id %s has toggled rider status from %s".formatted(id, status)); | 
| 111 |     } | |
| 112 | ||
| 113 | ||
| 114 | } | |
Mutations | ||
| 45 | 
 
 1.1  | 
|
| 55 | 
 
 1.1  | 
|
| 56 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 69 | 
 
 1.1  | 
|
| 78 | 
 
 1.1  | 
|
| 80 | 
 
 1.1 2.2  | 
|
| 82 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 92 | 
 
 1.1  | 
|
| 94 | 
 
 1.1 2.2  | 
|
| 96 | 
 
 1.1  | 
|
| 97 | 
 
 1.1  | 
|
| 105 | 
 
 1.1  | 
|
| 107 | 
 
 1.1 2.2  | 
|
| 109 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  |