| 1 | package edu.ucsb.cs156.courses.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.courses.entities.Grade; | |
| 4 | import edu.ucsb.cs156.courses.entities.UCSBSubject; | |
| 5 | import edu.ucsb.cs156.courses.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.courses.repositories.GradeRepository; | |
| 7 | import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository; | |
| 8 | import edu.ucsb.cs156.courses.services.CSVToGradeHistoryService; | |
| 9 | import edu.ucsb.cs156.courses.services.UCSBSubjectsService; | |
| 10 | import io.swagger.annotations.Api; | |
| 11 | import io.swagger.annotations.ApiOperation; | |
| 12 | import io.swagger.annotations.ApiParam; | |
| 13 | import lombok.extern.slf4j.Slf4j; | |
| 14 | ||
| 15 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 16 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 17 | ||
| 18 | import org.springframework.beans.factory.annotation.Autowired; | |
| 19 | import org.springframework.dao.DuplicateKeyException; | |
| 20 | import org.springframework.http.HttpStatus; | |
| 21 | import org.springframework.http.ResponseEntity; | |
| 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 | ||
| 27 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 28 | import org.springframework.web.bind.annotation.RequestParam; | |
| 29 | import org.springframework.web.bind.annotation.RequestPart; | |
| 30 | ||
| 31 | import org.springframework.web.bind.annotation.RestController; | |
| 32 | import org.springframework.web.multipart.MultipartFile; | |
| 33 | import org.springframework.web.server.ResponseStatusException; | |
| 34 | ||
| 35 | import java.io.IOException; | |
| 36 | import java.io.InputStreamReader; | |
| 37 | import java.io.Reader; | |
| 38 | import java.util.ArrayList; | |
| 39 | import java.util.List; | |
| 40 | ||
| 41 | @Slf4j | |
| 42 | @Api(description = "API for grade data") | |
| 43 | @RequestMapping("/api/grade") | |
| 44 | @RestController | |
| 45 | public class GradeController extends ApiController { | |
| 46 | @Autowired | |
| 47 | GradeRepository gradeRepository; | |
| 48 | ||
| 49 | @Autowired | |
| 50 | ObjectMapper mapper; | |
| 51 | ||
| 52 | @Autowired | |
| 53 | CSVToGradeHistoryService csvToGradeHistoryService; | |
| 54 | ||
| 55 | @ApiOperation(value = "Get all Grade") | |
| 56 | @GetMapping("/all") | |
| 57 | public Iterable<Grade> allHistory() { | |
| 58 | Iterable<Grade> gradeHistoryRows = gradeRepository.findAll(); | |
| 59 |
1
1. allHistory : replaced return value with null for edu/ucsb/cs156/courses/controllers/GradeController::allHistory → NO_COVERAGE |
return gradeHistoryRows; |
| 60 | } | |
| 61 | ||
| 62 | @ApiOperation(value = "Load grade history into database from uploaded CSV") | |
| 63 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 64 | @PostMapping(value = "upload", produces = "application/json") | |
| 65 | public ResponseEntity<String> uploadCSV(@RequestPart MultipartFile file) throws IOException{ | |
| 66 | log.info("Starting upload CSV"); | |
| 67 | try { | |
| 68 | Reader reader = new InputStreamReader(file.getInputStream()); | |
| 69 | List<Grade> uploadedRows = csvToGradeHistoryService.parse(reader); | |
| 70 | List<Grade> savedCourse = (List<Grade>) gradeRepository.saveAll(uploadedRows); | |
| 71 | String body = mapper.writeValueAsString(savedCourse); | |
| 72 |
1
1. uploadCSV : replaced return value with null for edu/ucsb/cs156/courses/controllers/GradeController::uploadCSV → NO_COVERAGE |
return ResponseEntity.ok().body(body); |
| 73 | } catch(Exception e){ | |
| 74 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Malformed CSV", e); | |
| 75 | } | |
| 76 | } | |
| 77 | } | |
| 78 | ||
Mutations | ||
| 59 |
1.1 |
|
| 72 |
1.1 |