| 1 | package edu.ucsb.cs156.dining.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | ||
| 6 | import edu.ucsb.cs156.dining.statuses.ModerationStatus; | |
| 7 | import org.springframework.beans.factory.annotation.Autowired; | |
| 8 | import org.springframework.http.ResponseEntity; | |
| 9 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 10 | ||
| 11 | import edu.ucsb.cs156.dining.entities.User; | |
| 12 | import edu.ucsb.cs156.dining.repositories.UserRepository; | |
| 13 | import io.swagger.v3.oas.annotations.Operation; | |
| 14 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 15 | ||
| 16 | ||
| 17 | import edu.ucsb.cs156.dining.models.CurrentUser; | |
| 18 | import edu.ucsb.cs156.dining.entities.User; | |
| 19 | import edu.ucsb.cs156.dining.errors.EntityNotFoundException; | |
| 20 | import org.springframework.web.bind.annotation.*; | |
| 21 | import org.springframework.http.HttpStatus; | |
| 22 | import org.springframework.web.server.ResponseStatusException; | |
| 23 | import java.util.List; | |
| 24 | ||
| 25 | import java.time.LocalDate; | |
| 26 | ||
| 27 | /** | |
| 28 |  * This is a REST controller for getting information about the users. | |
| 29 |  *  | |
| 30 |  * These endpoints are only accessible to users with the role "ROLE_ADMIN". | |
| 31 |  */ | |
| 32 | ||
| 33 | @Tag(name="User information (admin only)") | |
| 34 | @RequestMapping("/api") | |
| 35 | @RestController | |
| 36 | public class UsersController extends ApiController { | |
| 37 |     @Autowired | |
| 38 |     UserRepository userRepository; | |
| 39 | ||
| 40 |     @Autowired | |
| 41 |     ObjectMapper mapper; | |
| 42 | ||
| 43 |     /** | |
| 44 |      * This method returns a list of all users.  Accessible only to users with the role "ROLE_ADMIN". | |
| 45 |      * @return a list of all users | |
| 46 |      * @throws JsonProcessingException if there is an error processing the JSON | |
| 47 |      */ | |
| 48 |     @Operation(summary= "Get a list of all users") | |
| 49 |     @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_MODERATOR')") | |
| 50 |     @GetMapping("/admin/users") | |
| 51 |     public ResponseEntity<String> users() | |
| 52 |             throws JsonProcessingException { | |
| 53 | ||
| 54 | ||
| 55 |         Iterable<User> users = userRepository.findAll(); | |
| 56 |         String body = mapper.writeValueAsString(users); | |
| 57 | 
1
1. users : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::users → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 58 |     } | |
| 59 | ||
| 60 |     /** | |
| 61 |      * This method returns list of all users with a proposed alias. | |
| 62 |      * @return a list of users with a proposed alias | |
| 63 |      * @throws JsonProcessingException if there is an error processing the JSON | |
| 64 |      */ | |
| 65 |     @Operation(summary = "Get a list of all users with a proposed alias") | |
| 66 |     @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_MODERATOR')") | |
| 67 |     @GetMapping("/admin/usersWithProposedAlias") | |
| 68 |     public ResponseEntity<String> getUsersWithProposedAlias() | |
| 69 |             throws JsonProcessingException { | |
| 70 |         Iterable<User> users = userRepository.findByProposedAliasNotNull(); | |
| 71 |         String body = mapper.writeValueAsString(users); | |
| 72 | 
1
1. getUsersWithProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::getUsersWithProposedAlias → KILLED | 
        return ResponseEntity.ok().body(body); | 
| 73 |     } | |
| 74 | ||
| 75 |     /** | |
| 76 |      * This method allows the user to update their alias. | |
| 77 |      * @param proposedAlias the new alias | |
| 78 |      * @return the updated user | |
| 79 |      */ | |
| 80 |     @Operation(summary = "Update proposed alias of the current user") | |
| 81 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 82 |     @PostMapping("/currentUser/updateAlias") | |
| 83 |     public ResponseEntity<User> updateProposedAlias(@RequestParam String proposedAlias) { | |
| 84 |         CurrentUser currentUser = super.getCurrentUser(); | |
| 85 |         User user = currentUser.getUser(); | |
| 86 | ||
| 87 | 
1
1. updateProposedAlias : negated conditional → KILLED | 
        if (userRepository.findByAlias(proposedAlias).isPresent()) { | 
| 88 |             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Alias already in use."); | |
| 89 |         } | |
| 90 |      | |
| 91 | 
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED | 
        user.setProposedAlias(proposedAlias); | 
| 92 | 
1
1. updateProposedAlias : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED | 
        user.setStatus(ModerationStatus.AWAITING_REVIEW); | 
| 93 |         User savedUser = userRepository.save(user); | |
| 94 |      | |
| 95 | 
1
1. updateProposedAlias : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateProposedAlias → KILLED | 
        return ResponseEntity.ok(savedUser); | 
| 96 |     } | |
| 97 |      | |
| 98 |     /** | |
| 99 |      * This method allows an admin to update the moderation status of a user's alias. | |
| 100 |      * @param id the id of the user to update | |
| 101 |      * @param approved the new moderation status  | |
| 102 |      * @return the updated user | |
| 103 |      */ | |
| 104 |     @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_MODERATOR')") | |
| 105 |     @PutMapping("/currentUser/updateAliasModeration") | |
| 106 |     public User updateAliasModeration( | |
| 107 |             @RequestParam long id,  | |
| 108 |             @RequestParam Boolean approved) { | |
| 109 |          | |
| 110 |         User user = userRepository.findById(id) | |
| 111 | 
1
1. lambda$updateAliasModeration$0 : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::lambda$updateAliasModeration$0 → KILLED | 
            .orElseThrow(() -> new EntityNotFoundException(User.class, id)); | 
| 112 |          | |
| 113 | ||
| 114 | 
1
1. updateAliasModeration : negated conditional → KILLED | 
        if (approved) { | 
| 115 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setAlias → KILLED | 
            user.setAlias(user.getProposedAlias());   | 
| 116 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED | 
            user.setStatus(ModerationStatus.APPROVED); | 
| 117 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setDateApproved → KILLED | 
            user.setDateApproved(LocalDate.now()); | 
| 118 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED | 
            user.setProposedAlias(null); | 
| 119 |         } else { | |
| 120 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setStatus → KILLED | 
            user.setStatus(ModerationStatus.REJECTED); | 
| 121 | 
1
1. updateAliasModeration : removed call to edu/ucsb/cs156/dining/entities/User::setProposedAlias → KILLED | 
            user.setProposedAlias(null); | 
| 122 |         } | |
| 123 |          | |
| 124 |         userRepository.save(user); | |
| 125 | ||
| 126 | 
1
1. updateAliasModeration : replaced return value with null for edu/ucsb/cs156/dining/controllers/UsersController::updateAliasModeration → KILLED | 
        return user; | 
| 127 |     } | |
| 128 | } | |
Mutations | ||
| 57 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 87 | 
 
 1.1  | 
|
| 91 | 
 
 1.1  | 
|
| 92 | 
 
 1.1  | 
|
| 95 | 
 
 1.1  | 
|
| 111 | 
 
 1.1  | 
|
| 114 | 
 
 1.1  | 
|
| 115 | 
 
 1.1  | 
|
| 116 | 
 
 1.1  | 
|
| 117 | 
 
 1.1  | 
|
| 118 | 
 
 1.1  | 
|
| 120 | 
 
 1.1  | 
|
| 121 | 
 
 1.1  | 
|
| 126 | 
 
 1.1  |