| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | ||
| 7 | import edu.ucsb.cs156.frontiers.entities.Admin; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 9 | ||
| 10 | import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | import org.springframework.http.ResponseEntity; | |
| 12 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 13 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 14 | import org.springframework.web.bind.annotation.GetMapping; | |
| 15 | import org.springframework.web.bind.annotation.PostMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestParam; | |
| 18 | import org.springframework.web.bind.annotation.RestController; | |
| 19 | import org.springframework.beans.factory.annotation.Value; | |
| 20 | ||
| 21 | ||
| 22 | import io.swagger.v3.oas.annotations.Operation; | |
| 23 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 24 | import io.swagger.v3.oas.annotations.Parameter; | |
| 25 | import lombok.extern.slf4j.Slf4j; | |
| 26 | ||
| 27 | import java.util.List; | |
| 28 | ||
| 29 | ||
| 30 | @Tag(name = "Admins") | |
| 31 | @RequestMapping("/api/admins") | |
| 32 | @RestController | |
| 33 | @Slf4j | |
| 34 | ||
| 35 | public class AdminsController extends ApiController{ | |
| 36 | @Autowired | |
| 37 | AdminRepository adminRepository; | |
| 38 | ||
| 39 | /** | |
| 40 | * This method creates a new admin. Accessible only to users with the role "ROLE_ADMIN". | |
| 41 | * @param email email of the the admin | |
| 42 | * @return the new admin | |
| 43 | */ | |
| 44 | @Operation(summary= "Add a new Admin") | |
| 45 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 46 | @PostMapping("/post") | |
| 47 | public Admin postAdmin( | |
| 48 | @Parameter(name="email") @RequestParam String email | |
| 49 | ) | |
| 50 | { | |
| 51 | ||
| 52 | Admin admin = new Admin(email); | |
| 53 | ||
| 54 | Admin savedAdmin = adminRepository.save(admin); | |
| 55 | ||
| 56 |
1
1. postAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::postAdmin → KILLED |
return savedAdmin; |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * THis method returns a list of all admins. | |
| 61 | * @return a list of all admins | |
| 62 | */ | |
| 63 | @Operation(summary= "List all Admins") | |
| 64 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 65 | @GetMapping("/all") | |
| 66 | public Iterable<Admin> allAdmins() { | |
| 67 | Iterable<Admin> admins = adminRepository.findAll(); | |
| 68 |
1
1. allAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/AdminsController::allAdmins → KILLED |
return admins; |
| 69 | } | |
| 70 | ||
| 71 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 72 | private List<String> adminEmails; | |
| 73 | ||
| 74 | /** | |
| 75 | * Delete an admin. Accessible only to users with the role "ROLE_ADMIN". | |
| 76 | * @param email email of the admin | |
| 77 | * @return a message indiciating the organization was deleted | |
| 78 | */ | |
| 79 | @Operation(summary= "Delete an Admin") | |
| 80 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 81 | @DeleteMapping("") | |
| 82 | public Object deleteAdmin( | |
| 83 | @Parameter(name="email") @RequestParam String email) { | |
| 84 | Admin admin = adminRepository.findById(email) | |
| 85 |
1
1. lambda$deleteAdmin$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::lambda$deleteAdmin$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Admin.class, email)); |
| 86 | ||
| 87 |
1
1. deleteAdmin : negated conditional → KILLED |
if (adminEmails.contains(email)) { |
| 88 | throw new UnsupportedOperationException("Can not delete an admin from ADMIN_EMAILS list"); | |
| 89 | } | |
| 90 | ||
| 91 |
1
1. deleteAdmin : removed call to edu/ucsb/cs156/frontiers/repositories/AdminRepository::delete → KILLED |
adminRepository.delete(admin); |
| 92 |
1
1. deleteAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::deleteAdmin → KILLED |
return genericMessage("Admin with id %s deleted".formatted(email)); |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 56 |
1.1 |
|
| 68 |
1.1 |
|
| 85 |
1.1 |
|
| 87 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |