| 1 | package edu.ucsb.cs.scaffold.controller; | |
| 2 | ||
| 3 | import edu.ucsb.cs.scaffold.entity.Admin; | |
| 4 | import edu.ucsb.cs.scaffold.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs.scaffold.repository.AdminRepository; | |
| 6 | import edu.ucsb.cs.scaffold.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 | @Tag(name = "Admin") | |
| 24 | @RequestMapping("/api/admin") | |
| 25 | @RestController | |
| 26 | @Slf4j | |
| 27 | public class AdminsController extends ApiController { | |
| 28 | ||
| 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 | @Operation(summary = "Create a new admin") | |
| 41 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 42 | @PostMapping("/post") | |
| 43 | public Admin postAdmin(@Parameter(name = "email") @RequestParam String email) { | |
| 44 | String convertedEmail = CanonicalFormConverter.convertToValidEmail(email).strip(); | |
| 45 | Admin admin = new Admin(convertedEmail); | |
| 46 |
1
1. postAdmin : replaced return value with null for edu/ucsb/cs/scaffold/controller/AdminsController::postAdmin → KILLED |
return adminRepository.save(admin); |
| 47 | } | |
| 48 | ||
| 49 | @Operation(summary = "List all admins") | |
| 50 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 51 | @GetMapping("/all") | |
| 52 | public Iterable<AdminDTO> allAdmins() { | |
| 53 | Iterable<Admin> admins = adminRepository.findAll(); | |
| 54 |
1
1. allAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs/scaffold/controller/AdminsController::allAdmins → KILLED |
return StreamSupport.stream(admins.spliterator(), false) |
| 55 |
1
1. lambda$allAdmins$0 : replaced return value with null for edu/ucsb/cs/scaffold/controller/AdminsController::lambda$allAdmins$0 → KILLED |
.map(admin -> new AdminDTO(admin, adminEmails)) |
| 56 | .toList(); | |
| 57 | } | |
| 58 | ||
| 59 | @Operation(summary = "Delete an Admin") | |
| 60 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 | @DeleteMapping("/delete") | |
| 62 | public Object deleteAdmin(@Parameter(name = "email") @RequestParam String email) { | |
| 63 | Admin admin = | |
| 64 | adminRepository | |
| 65 | .findByEmail(email) | |
| 66 |
1
1. lambda$deleteAdmin$1 : replaced return value with null for edu/ucsb/cs/scaffold/controller/AdminsController::lambda$deleteAdmin$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Admin.class, email)); |
| 67 |
1
1. deleteAdmin : negated conditional → KILLED |
if (adminEmails.contains(email)) { |
| 68 | throw new UnsupportedOperationException( | |
| 69 | "Forbidden to delete an admin from ADMIN_EMAILS list"); | |
| 70 | } | |
| 71 |
1
1. deleteAdmin : removed call to edu/ucsb/cs/scaffold/repository/AdminRepository::delete → KILLED |
adminRepository.delete(admin); |
| 72 |
1
1. deleteAdmin : replaced return value with null for edu/ucsb/cs/scaffold/controller/AdminsController::deleteAdmin → KILLED |
return genericMessage("Admin with id %s deleted".formatted(email)); |
| 73 | } | |
| 74 | } | |
Mutations | ||
| 46 |
1.1 |
|
| 54 |
1.1 |
|
| 55 |
1.1 |
|
| 66 |
1.1 |
|
| 67 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |