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