| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import java.util.Arrays; | |
| 4 | ||
| 5 | import org.springframework.beans.factory.annotation.Autowired; | |
| 6 | import org.springframework.beans.factory.annotation.Value; | |
| 7 | import org.springframework.data.domain.Sort; | |
| 8 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 9 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 10 | import org.springframework.web.bind.annotation.GetMapping; | |
| 11 | import org.springframework.web.bind.annotation.PostMapping; | |
| 12 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 13 | import org.springframework.web.bind.annotation.RequestParam; | |
| 14 | import org.springframework.web.bind.annotation.RestController; | |
| 15 | ||
| 16 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 17 | ||
| 18 | import edu.ucsb.cs156.frontiers.entities.Admin; | |
| 19 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 20 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 21 | import io.swagger.v3.oas.annotations.Operation; | |
| 22 | import io.swagger.v3.oas.annotations.Parameter; | |
| 23 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 24 | import lombok.extern.slf4j.Slf4j; | |
| 25 | ||
| 26 | @Tag(name = "Admins") | |
| 27 | @RequestMapping("/api/admin/admins") | |
| 28 | @RestController | |
| 29 | @Slf4j | |
| 30 | public class AdminController extends ApiController{ | |
| 31 | @Autowired | |
| 32 | AdminRepository adminRepository; | |
| 33 | ||
| 34 | @Value("${app.admin.emails}") | |
| 35 | private String[] protectedAdminEmails; | |
| 36 | ||
| 37 | @Operation(summary= "List all Admins") | |
| 38 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 39 | @GetMapping("/all") | |
| 40 | public Iterable<Admin> allAdmin() { | |
| 41 |
1
1. allAdmin : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/AdminController::allAdmin → KILLED |
return adminRepository.findAll(Sort.by("email").ascending()); |
| 42 | } | |
| 43 | ||
| 44 | @Operation(summary= "Create a new admin") | |
| 45 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 46 | @PostMapping("/post") | |
| 47 | public Admin postAdmin( | |
| 48 | @Parameter(name="email") @RequestParam String email) | |
| 49 | throws JsonProcessingException { | |
| 50 | ||
| 51 | Admin admin = new Admin(); | |
| 52 |
1
1. postAdmin : removed call to edu/ucsb/cs156/frontiers/entities/Admin::setEmail → KILLED |
admin.setEmail(email); |
| 53 | ||
| 54 | Admin savedAdmin = adminRepository.save(admin); | |
| 55 | ||
| 56 |
1
1. postAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminController::postAdmin → KILLED |
return savedAdmin; |
| 57 | } | |
| 58 | ||
| 59 | @Operation(summary= "Delete an Admin") | |
| 60 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 61 | @DeleteMapping("") | |
| 62 | public Object deleteAdmin(@Parameter(name="email") @RequestParam String email) { | |
| 63 | ||
| 64 |
1
1. deleteAdmin : negated conditional → KILLED |
if (Arrays.asList(protectedAdminEmails).contains(email)) { |
| 65 | throw new UnsupportedOperationException("Cannot delete protected admin: " + email); | |
| 66 | } | |
| 67 | | |
| 68 | Admin admin = adminRepository.findById(email) | |
| 69 |
1
1. lambda$deleteAdmin$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminController::lambda$deleteAdmin$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Admin.class, email)); |
| 70 | ||
| 71 |
1
1. deleteAdmin : removed call to edu/ucsb/cs156/frontiers/repositories/AdminRepository::delete → KILLED |
adminRepository.delete(admin); |
| 72 |
1
1. deleteAdmin : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/AdminController::deleteAdmin → KILLED |
return genericMessage("Admin with email is deleted".formatted(email)); |
| 73 | } | |
| 74 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 52 |
1.1 |
|
| 56 |
1.1 |
|
| 64 |
1.1 |
|
| 69 |
1.1 |
|
| 71 |
1.1 |
|
| 72 |
1.1 |