| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.Report; | |
| 4 | import edu.ucsb.cs156.happiercows.entities.ReportLine; | |
| 5 | import edu.ucsb.cs156.happiercows.helpers.ReportCSVHelper; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.ReportLineRepository; | |
| 8 | import edu.ucsb.cs156.happiercows.repositories.ReportRepository; | |
| 9 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | import io.swagger.v3.oas.annotations.Operation; | |
| 12 | import io.swagger.v3.oas.annotations.Parameter; | |
| 13 | ||
| 14 | import org.springframework.data.domain.Sort.Order; | |
| 15 | import org.springframework.http.HttpHeaders; | |
| 16 | import org.springframework.http.MediaType; | |
| 17 | import org.springframework.http.ResponseEntity; | |
| 18 | ||
| 19 | import java.io.ByteArrayInputStream; | |
| 20 | import java.io.IOException; | |
| 21 | import java.util.List; | |
| 22 | import java.util.Optional; | |
| 23 | ||
| 24 | import org.springframework.beans.factory.annotation.Autowired; | |
| 25 | import org.springframework.core.io.InputStreamResource; | |
| 26 | import org.springframework.core.io.Resource; | |
| 27 | import org.springframework.data.domain.Sort; | |
| 28 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 29 | import org.springframework.web.bind.annotation.GetMapping; | |
| 30 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 31 | import org.springframework.web.bind.annotation.RequestParam; | |
| 32 | import org.springframework.web.bind.annotation.RestController; | |
| 33 | ||
| 34 | @Tag(name = "Reports") | |
| 35 | @RequestMapping("/api/reports") | |
| 36 | @RestController | |
| 37 | public class ReportsController extends ApiController { | |
| 38 | ||
| 39 |     @Autowired | |
| 40 |     CommonsRepository commonsRepository; | |
| 41 | ||
| 42 |     @Autowired | |
| 43 |     UserCommonsRepository userCommonsRepository; | |
| 44 | ||
| 45 |     @Autowired | |
| 46 |     ReportRepository reportRepository; | |
| 47 | ||
| 48 |     @Autowired | |
| 49 |     ReportLineRepository reportLineRepository; | |
| 50 | ||
| 51 |     @Operation(summary = "Get all report headers") | |
| 52 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 53 |     @GetMapping("") | |
| 54 |     public Iterable<Report> allReports() { | |
| 55 |         Iterable<Report> reports = reportRepository.findAll( | |
| 56 |                 Sort.by(List.of( | |
| 57 |                         new Order(Sort.Direction.ASC, "commonsId"), | |
| 58 |                         new Order(Sort.Direction.DESC, "id")))); | |
| 59 | 
1
1. allReports : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReports → KILLED | 
        return reports; | 
| 60 |     } | |
| 61 | ||
| 62 |     @Operation(summary = "Get report headers for a given report") | |
| 63 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 64 |     @GetMapping("/byReportId") | |
| 65 |     public Optional<Report> findByReportId( | |
| 66 |             @Parameter(name = "reportId") @RequestParam Long reportId) { | |
| 67 |         Optional<Report> reports = reportRepository.findById(reportId); | |
| 68 | 
1
1. findByReportId : replaced return value with Optional.empty for edu/ucsb/cs156/happiercows/controllers/ReportsController::findByReportId → KILLED | 
        return reports; | 
| 69 |     } | |
| 70 | ||
| 71 |     @Operation(summary = "Get report headers for a given user commons") | |
| 72 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 73 |     @GetMapping("/headers") | |
| 74 |     public Iterable<Report> allReportsByCommonsId( | |
| 75 |             @Parameter(name = "commonsId") @RequestParam Long commonsId) { | |
| 76 |         Iterable<Report> reports = reportRepository.findAllByCommonsId(commonsId); | |
| 77 | 
1
1. allReportsByCommonsId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allReportsByCommonsId → KILLED | 
        return reports; | 
| 78 |     } | |
| 79 | ||
| 80 |     @Operation(summary = "Get report lines for a report id") | |
| 81 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 82 |     @GetMapping("/lines") | |
| 83 |     public Iterable<ReportLine> allLinesByReportId( | |
| 84 |             @Parameter(name = "reportId") @RequestParam Long reportId) { | |
| 85 |         Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId); | |
| 86 | 
1
1. allLinesByReportId : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::allLinesByReportId → KILLED | 
        return reportLines; | 
| 87 |     } | |
| 88 | ||
| 89 |     @Operation(summary = "Get report lines for a report id and user commons id") | |
| 90 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 91 |     @GetMapping("/download") | |
| 92 |     public ResponseEntity<Resource> getLinesCSV( | |
| 93 |             @Parameter(name = "reportId") @RequestParam Long reportId) throws IOException { | |
| 94 | ||
| 95 |         Iterable<ReportLine> reportLines = reportLineRepository.findAllByReportId(reportId); | |
| 96 | ||
| 97 |         String filename = String.format("report%05d.csv",reportId); | |
| 98 | ||
| 99 |         ByteArrayInputStream bais = ReportCSVHelper.toCSV(reportLines); | |
| 100 |         InputStreamResource isr = new InputStreamResource(bais); | |
| 101 | ||
| 102 | 
1
1. getLinesCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/ReportsController::getLinesCSV → KILLED | 
        return ResponseEntity.ok() | 
| 103 |                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename) | |
| 104 |                 .contentType(MediaType.parseMediaType("application/csv")) | |
| 105 |                 .body(isr); | |
| 106 |     } | |
| 107 | ||
| 108 | } | |
Mutations | ||
| 59 | 
 
 1.1  | 
|
| 68 | 
 
 1.1  | 
|
| 77 | 
 
 1.1  | 
|
| 86 | 
 
 1.1  | 
|
| 102 | 
 
 1.1  |