| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.happiercows.entities.GameStats; | |
| 4 | import edu.ucsb.cs156.happiercows.helpers.GameStatsCSVHelper; | |
| 5 | import edu.ucsb.cs156.happiercows.repositories.GameRepository; | |
| 6 | import edu.ucsb.cs156.happiercows.repositories.GameStatsRepository; | |
| 7 | import edu.ucsb.cs156.happiercows.repositories.FarmerRepository; | |
| 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 = "GameStats") | |
| 29 | @RequestMapping("/api/gamestats") | |
| 30 | @RestController | |
| 31 | public class GameStatsController { | |
| 32 | ||
| 33 | @Autowired | |
| 34 | GameRepository gameRepository; | |
| 35 | ||
| 36 | @Autowired | |
| 37 | FarmerRepository farmerRepository; | |
| 38 | ||
| 39 | @Autowired | |
| 40 | GameStatsRepository gameStatsRepository; | |
| 41 | ||
| 42 | @Operation(summary = "Get all game stats") | |
| 43 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 44 | @GetMapping("") | |
| 45 | public Iterable<GameStats> allGameStats() { | |
| 46 |
1
1. allGameStats : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/GameStatsController::allGameStats → KILLED |
return gameStatsRepository.findAll(); |
| 47 | } | |
| 48 | ||
| 49 | @Operation(summary = "Get all stats for a game") | |
| 50 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 51 | @GetMapping("/game") | |
| 52 | public Iterable<GameStats> allGameStatsForGame( | |
| 53 | @Parameter(name = "gameId") @RequestParam Long gameId) { | |
| 54 |
1
1. allGameStatsForGame : replaced return value with Collections.emptyList for edu/ucsb/cs156/happiercows/controllers/GameStatsController::allGameStatsForGame → KILLED |
return gameStatsRepository.findAllByGameId(gameId); |
| 55 | } | |
| 56 | ||
| 57 | @Operation(summary = "Get all stats for a game as csv") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 | @GetMapping(value = "/download") | |
| 60 | public ResponseEntity<Resource> getCSV( | |
| 61 | @Parameter(name = "gameId") @RequestParam Long gameId) throws IOException { | |
| 62 | ||
| 63 | Iterable<GameStats> gameStats = gameStatsRepository.findAllByGameId(gameId); | |
| 64 | | |
| 65 | String filename = String.format("stats%05d.csv",gameId); | |
| 66 | ||
| 67 | ByteArrayInputStream bais = GameStatsCSVHelper.toCSV(gameStats); | |
| 68 | InputStreamResource isr = new InputStreamResource(bais); | |
| 69 | ||
| 70 |
1
1. getCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameStatsController::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 game as csv") | |
| 76 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 77 | @GetMapping(value = "/downloadAll") | |
| 78 | public ResponseEntity<Resource> getAllCSV() throws IOException { | |
| 79 | ||
| 80 | Iterable<GameStats> gameStats = gameStatsRepository.findAll(); | |
| 81 | | |
| 82 | String filename = String.format("GameStats.csv"); | |
| 83 | ||
| 84 | ByteArrayInputStream bais = GameStatsCSVHelper.toCSV(gameStats); | |
| 85 | InputStreamResource isr = new InputStreamResource(bais); | |
| 86 | ||
| 87 |
1
1. getAllCSV : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/GameStatsController::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 |