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