| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Admin; | |
| 4 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 6 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import io.swagger.v3.oas.annotations.Parameter; | |
| 9 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 10 | import java.util.List; | |
| 11 | import java.util.stream.StreamSupport; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.beans.factory.annotation.Value; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 17 | import org.springframework.web.bind.annotation.GetMapping; | |
| 18 | import org.springframework.web.bind.annotation.PostMapping; | |
| 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 | /** This is a REST controller for Admin */ | |
| 24 | @Tag(name = "Admin") | |
| 25 | @RequestMapping("/api/admin") | |
| 26 | @RestController | |
| 27 | @Slf4j | |
| 28 | public class AdminsController extends ApiController { | |
| 29 | @Autowired AdminRepository adminRepository; | |
| 30 | ||
| 31 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 32 | List<String> adminEmails; | |
| 33 | ||
| 34 | public static record AdminDTO(String email, boolean isInAdminEmails) { | |
| 35 | public AdminDTO(Admin admin, List<String> adminEmails) { | |
| 36 | this(admin.getEmail(), adminEmails.contains(admin.getEmail())); | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Create a new admin | |
| 42 | * | |
| 43 | * @param adminEmail the email in typical email format | |
| 44 | * @return the saved admin | |
| 45 | */ | |
| 46 | @Operation(summary = "Create a new admin") | |
| 47 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 48 | @PostMapping("/post") | |
| 49 | public Admin postAdmin(@Parameter(name = "email") @RequestParam String email) { | |
| 50 | String convertedEmail = CanonicalFormConverter.convertToValidEmail(email); | |
| 51 | Admin admin = new Admin(convertedEmail); | |
| 52 | Admin savedAdmin = adminRepository.save(admin); | |
| 53 |
1
1. postAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::postAdmin → KILLED |
return savedAdmin; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * List all admins | |
| 58 | * | |
| 59 | * @return an iterable of Admin | |
| 60 | */ | |
| 61 | @Operation(summary = "List all admins") | |
| 62 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 63 | @GetMapping("/all") | |
| 64 | public Iterable<AdminDTO> allAdmins() { | |
| 65 | Iterable<Admin> admins = adminRepository.findAll(); | |
| 66 | List<AdminDTO> adminDTOs = | |
| 67 | StreamSupport.stream(admins.spliterator(), false) | |
| 68 |
1
1. lambda$allAdmins$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::lambda$allAdmins$0 → KILLED |
.map(admin -> new AdminDTO(admin, adminEmails)) |
| 69 | .toList(); | |
| 70 | ||
| 71 |
1
1. allAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/AdminsController::allAdmins → KILLED |
return adminDTOs; |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Delete an Admin | |
| 76 | * | |
| 77 | * @param email the email of the admin to delete | |
| 78 | * @return a message indicating the admin was deleted | |
| 79 | */ | |
| 80 | @Operation(summary = "Delete an Admin") | |
| 81 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 82 | @DeleteMapping("/delete") | |
| 83 | public Object deleteAdmin(@Parameter(name = "email") @RequestParam String email) { | |
| 84 | Admin admin = | |
| 85 | adminRepository | |
| 86 | .findByEmail(email) | |
| 87 |
1
1. lambda$deleteAdmin$1 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminsController::lambda$deleteAdmin$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Admin.class, email)); |
| 88 |
1
1. deleteAdmin : negated conditional → KILLED |
if (adminEmails.contains(email)) { |
| 89 | throw new UnsupportedOperationException( | |
| 90 | "Forbidden to delete an admin from ADMIN_EMAILS list"); | |
| 91 | } | |
| 92 |
1
1. deleteAdmin : removed call to edu/ucsb/cs156/frontiers/repositories/AdminRepository::delete → KILLED |
adminRepository.delete(admin); |
| 93 |
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)); |
| 94 | } | |
| 95 | } | |
Mutations | ||
| 53 |
1.1 |
|
| 68 |
1.1 |
|
| 71 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |
|
| 92 |
1.1 |
|
| 93 |
1.1 |