| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.CommonStats; | |
| 4 | import edu.ucsb.cs156.happiercows.helpers.CommonStatsCSVHelper; | |
| 5 | import edu.ucsb.cs156.happiercows.repositories.CommonsRepository; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.CommonStatsRepository; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.UserCommonsRepository; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import io.swagger.v3.oas.annotations.Operation; | |
| 10 | import io.swagger.v3.oas.annotations.Parameter; | |
| 11 | ||
| 12 | import org.springframework.http.HttpHeaders; | |
| 13 | import org.springframework.http.MediaType; | |
| 14 | import org.springframework.http.ResponseEntity; | |
| 15 | ||
| 16 | import java.io.ByteArrayInputStream; | |
| 17 | import java.io.IOException; | |
| 18 | ||
| 19 | import org.springframework.beans.factory.annotation.Autowired; | |
| 20 | import org.springframework.core.io.InputStreamResource; | |
| 21 | import org.springframework.core.io.Resource; | |
| 22 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 23 | import org.springframework.web.bind.annotation.GetMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 25 | import org.springframework.web.bind.annotation.RequestParam; | |
| 26 | import org.springframework.web.bind.annotation.RestController; | |
| 27 | ||
| 28 | @Tag(name = "CommonStats") | |
| 29 | @RequestMapping("/api/commonstats") | |
| 30 | @RestController | |
| 31 | public class CommonStatsController { | |
| 32 | ||
| 33 |     @Autowired | |
| 34 |     CommonsRepository commonsRepository; | |
| 35 | ||
| 36 |     @Autowired | |
| 37 |     UserCommonsRepository userCommonsRepository; | |
| 38 | ||
| 39 |     @Autowired | |
| 40 |     CommonStatsRepository commonStatsRepository; | |
| 41 | ||
| 42 |     @Operation(summary = "Get all common stats") | |
| 43 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 44 |     @GetMapping("") | |
| 45 |     public Iterable<CommonStats> allCommonStats() { | |
| 46 | 
1
1. allCommonStats : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonStatsController::allCommonStats → KILLED | 
        return commonStatsRepository.findAll(); | 
| 47 |     } | |
| 48 | ||
| 49 |     @Operation(summary = "Get all stats for a commons") | |
| 50 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 51 |     @GetMapping("/commons") | |
| 52 |     public Iterable<CommonStats> allCommonStatsForCommons( | |
| 53 |             @Parameter(name = "commonsId") @RequestParam Long commonsId) { | |
| 54 | 
1
1. allCommonStatsForCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonStatsController::allCommonStatsForCommons → KILLED | 
        return commonStatsRepository.findAllByCommonsId(commonsId); | 
| 55 |     } | |
| 56 | ||
| 57 |     @Operation(summary = "Get all stats for a commons as csv") | |
| 58 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 |     @GetMapping(value = "/download") | |
| 60 |     public ResponseEntity<Resource> getCSV( | |
| 61 |             @Parameter(name = "commonsId") @RequestParam Long commonsId) throws IOException { | |
| 62 | ||
| 63 |         Iterable<CommonStats> commonStats = commonStatsRepository.findAllByCommonsId(commonsId); | |
| 64 |                  | |
| 65 |         String filename = String.format("stats%05d.csv",commonsId); | |
| 66 | ||
| 67 |         ByteArrayInputStream bais = CommonStatsCSVHelper.toCSV(commonStats); | |
| 68 |         InputStreamResource isr = new InputStreamResource(bais); | |
| 69 | ||
| 70 | 
1
1. getCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonStatsController::getCSV → KILLED | 
        return ResponseEntity.ok() | 
| 71 |                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename) | |
| 72 |                 .contentType(MediaType.parseMediaType("application/csv")).body(isr); | |
| 73 |     } | |
| 74 | ||
| 75 |     @Operation(summary = "Get all stats for all commons as csv") | |
| 76 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 77 |     @GetMapping(value = "/downloadAll") | |
| 78 |     public ResponseEntity<Resource> getAllCSV() throws IOException { | |
| 79 | ||
| 80 |         Iterable<CommonStats> commonStats = commonStatsRepository.findAll(); | |
| 81 |                  | |
| 82 |         String filename = String.format("CommonStats.csv"); | |
| 83 | ||
| 84 |         ByteArrayInputStream bais = CommonStatsCSVHelper.toCSV(commonStats); | |
| 85 |         InputStreamResource isr = new InputStreamResource(bais); | |
| 86 | ||
| 87 | 
1
1. getAllCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/CommonStatsController::getAllCSV → KILLED | 
        return ResponseEntity.ok() | 
| 88 |                 .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename) | |
| 89 |                 .contentType(MediaType.parseMediaType("application/csv")).body(isr); | |
| 90 |     } | |
| 91 |      | |
| 92 | } | |
Mutations | ||
| 46 | 
 
 1.1  | 
|
| 54 | 
 
 1.1  | 
|
| 70 | 
 
 1.1  | 
|
| 87 | 
 
 1.1  |