| 1 | package edu.ucsb.cs156.organic.controllers; | |
| 2 | import edu.ucsb.cs156.organic.errors.EntityNotFoundException; | |
| 3 | ||
| 4 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | ||
| 7 | import org.springframework.beans.factory.annotation.Autowired; | |
| 8 | import org.springframework.http.ResponseEntity; | |
| 9 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 10 | import org.springframework.web.bind.annotation.GetMapping; | |
| 11 | import org.springframework.web.bind.annotation.PostMapping; | |
| 12 | ||
| 13 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 14 | import org.springframework.web.bind.annotation.RequestParam; | |
| 15 | import org.springframework.web.bind.annotation.RestController; | |
| 16 | ||
| 17 | import edu.ucsb.cs156.organic.entities.User; | |
| 18 | import edu.ucsb.cs156.organic.repositories.UserRepository; | |
| 19 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 20 | import io.swagger.v3.oas.annotations.Operation; | |
| 21 | import io.swagger.v3.oas.annotations.Parameter; | |
| 22 | ||
| 23 | @Tag(name = "User information (admin only)") | |
| 24 | @RequestMapping("/api/admin/users") | |
| 25 | @RestController | |
| 26 | public class UsersController extends ApiController { | |
| 27 |     @Autowired | |
| 28 |     UserRepository userRepository; | |
| 29 | ||
| 30 |     @Autowired | |
| 31 |     ObjectMapper mapper; | |
| 32 | ||
| 33 |     @Operation(summary = "Get a list of all users") | |
| 34 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 35 |     @GetMapping("") | |
| 36 |     public ResponseEntity<String> users() | |
| 37 |             throws JsonProcessingException { | |
| 38 |         Iterable<User> users = userRepository.findAll(); | |
| 39 |         String body = mapper.writeValueAsString(users); | |
| 40 | 
1
1. users : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::users → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 41 |     } | |
| 42 | ||
| 43 |     @Operation(summary= "Toggle a user's instructor status") | |
| 44 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 45 |     @PostMapping("/toggleInstructor") | |
| 46 |     public Object postUsersToggleInstructor( | |
| 47 |             @Parameter(name="githubId") @RequestParam Integer githubId) | |
| 48 |             { | |
| 49 | ||
| 50 |         User user = userRepository.findByGithubId(githubId) | |
| 51 | 
1
1. lambda$postUsersToggleInstructor$0 : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::lambda$postUsersToggleInstructor$0 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(User.class, githubId)); | 
| 52 | ||
| 53 | 
2
1. postUsersToggleInstructor : negated conditional → KILLED 2. postUsersToggleInstructor : removed call to edu/ucsb/cs156/organic/entities/User::setInstructor → KILLED  | 
        user.setInstructor(!user.isInstructor()); | 
| 54 | ||
| 55 |         userRepository.save(user); | |
| 56 | 
1
1. postUsersToggleInstructor : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::postUsersToggleInstructor → KILLED | 
        return genericMessage("User with githubId %s has toggled instructor status to %s".formatted(githubId, user.isInstructor())); | 
| 57 |     } | |
| 58 | ||
| 59 |     @Operation(summary = "Toggle the admin field") | |
| 60 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 |     @PostMapping("/toggleAdmin") | |
| 62 |     public Object toggleAdmin( @Parameter(name = "githubId", description = "Integer, githubId number of user to toggle their admin field", example = "1", required = true) @RequestParam Integer githubId){ | |
| 63 |         User user = userRepository.findByGithubId(githubId) | |
| 64 | 
1
1. lambda$toggleAdmin$1 : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::lambda$toggleAdmin$1 → KILLED | 
        .orElseThrow(() -> new EntityNotFoundException(User.class, githubId)); | 
| 65 | ||
| 66 | 
2
1. toggleAdmin : negated conditional → KILLED 2. toggleAdmin : removed call to edu/ucsb/cs156/organic/entities/User::setAdmin → KILLED  | 
        user.setAdmin(!user.isAdmin()); | 
| 67 |         userRepository.save(user); | |
| 68 | 
1
1. toggleAdmin : replaced return value with null for edu/ucsb/cs156/organic/controllers/UsersController::toggleAdmin → KILLED | 
        return genericMessage("User with githubId %s has toggled admin status to %s".formatted(githubId, user.isAdmin())); | 
| 69 |     } | |
| 70 | } | |
Mutations | ||
| 40 | 
 
 1.1  | 
|
| 51 | 
 
 1.1  | 
|
| 53 | 
 
 1.1 2.2  | 
|
| 56 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 66 | 
 
 1.1 2.2  | 
|
| 68 | 
 
 1.1  |