| 1 | package edu.ucsb.cs156.happiercows.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 4 | ||
| 5 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 6 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | ||
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.data.domain.Page; | |
| 11 | import org.springframework.data.domain.PageRequest; | |
| 12 | import org.springframework.data.domain.Sort; | |
| 13 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 14 | import org.springframework.web.bind.annotation.GetMapping; | |
| 15 | import org.springframework.web.bind.annotation.PostMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 17 | import org.springframework.web.bind.annotation.RequestParam; | |
| 18 | import org.springframework.web.bind.annotation.RestController; | |
| 19 | ||
| 20 | ||
| 21 | import edu.ucsb.cs156.happiercows.entities.jobs.Job; | |
| 22 | import edu.ucsb.cs156.happiercows.jobs.InstructorReportJob; | |
| 23 | import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobFactory; | |
| 24 | import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobSingleCommons; | |
| 25 | import edu.ucsb.cs156.happiercows.jobs.InstructorReportJobSingleCommonsFactory; | |
| 26 | import edu.ucsb.cs156.happiercows.jobs.MilkTheCowsJobFactory; | |
| 27 | import edu.ucsb.cs156.happiercows.jobs.SetCowHealthJobFactory; | |
| 28 | import edu.ucsb.cs156.happiercows.jobs.TestJob; | |
| 29 | import edu.ucsb.cs156.happiercows.jobs.UpdateCowHealthJobFactory; | |
| 30 | import edu.ucsb.cs156.happiercows.repositories.jobs.JobsRepository; | |
| 31 | import edu.ucsb.cs156.happiercows.services.jobs.JobContextConsumer; | |
| 32 | import edu.ucsb.cs156.happiercows.services.jobs.JobService; | |
| 33 | ||
| 34 | ||
| 35 | @Tag(name = "Jobs") | |
| 36 | @RequestMapping("/api/jobs") | |
| 37 | @RestController | |
| 38 | public class JobsController extends ApiController { | |
| 39 | @Autowired | |
| 40 | private JobsRepository jobsRepository; | |
| 41 | ||
| 42 | @Autowired | |
| 43 | private JobService jobService; | |
| 44 | ||
| 45 | @Autowired | |
| 46 | ObjectMapper mapper; | |
| 47 | ||
| 48 | @Autowired | |
| 49 | UpdateCowHealthJobFactory updateCowHealthJobFactory; | |
| 50 | ||
| 51 | @Autowired | |
| 52 | MilkTheCowsJobFactory milkTheCowsJobFactory; | |
| 53 | ||
| 54 | @Autowired | |
| 55 | SetCowHealthJobFactory setCowHealthJobFactory; | |
| 56 | ||
| 57 | @Autowired | |
| 58 | InstructorReportJobFactory instructorReportJobFactory; | |
| 59 | ||
| 60 | @Autowired | |
| 61 | InstructorReportJobSingleCommonsFactory instructorReportJobSingleCommonsFactory; | |
| 62 | ||
| 63 | @Operation(summary = "List all jobs") | |
| 64 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 65 | @GetMapping("/all") | |
| 66 | public Iterable<Job> allJobs() { | |
| 67 | Iterable<Job> jobs = jobsRepository.findAll(); | |
| 68 |
1
1. allJobs : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobs → KILLED |
return jobs; |
| 69 | } | |
| 70 | ||
| 71 | @Operation(summary = "List all jobs") | |
| 72 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 73 | @GetMapping("/all/pageable") | |
| 74 | public Page<Job> allJobsPaged( | |
| 75 | @Parameter(name="page") @RequestParam int page, | |
| 76 | @Parameter(name="size") @RequestParam int size | |
| 77 | ) { | |
| 78 | Page<Job> jobs = jobsRepository.findAll(PageRequest.of(page, size, Sort.by("id").descending())); | |
| 79 |
1
1. allJobsPaged : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::allJobsPaged → KILLED |
return jobs; |
| 80 | } | |
| 81 | ||
| 82 | @Operation(summary = "Launch Test Job (click fail if you want to test exception handling)") | |
| 83 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 84 | @PostMapping("/launch/testjob") | |
| 85 | public Job launchTestJob( | |
| 86 | @Parameter(name="fail") @RequestParam Boolean fail, | |
| 87 | @Parameter(name="sleepMs") @RequestParam Integer sleepMs | |
| 88 | ) { | |
| 89 | TestJob testJob = TestJob.builder() | |
| 90 | .fail(fail) | |
| 91 | .sleepMs(sleepMs) | |
| 92 | .build(); | |
| 93 | ||
| 94 |
1
1. launchTestJob : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED |
return jobService.runAsJob(testJob); |
| 95 | } | |
| 96 | ||
| 97 | @Operation(summary = "Launch Job to Milk the Cows (click fail if you want to test exception handling)") | |
| 98 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 99 | @PostMapping("/launch/milkthecowjob") | |
| 100 | public Job launchTestJob( | |
| 101 | ) { | |
| 102 | JobContextConsumer milkTheCowsJob = milkTheCowsJobFactory.create(); | |
| 103 |
1
1. launchTestJob : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::launchTestJob → KILLED |
return jobService.runAsJob(milkTheCowsJob); |
| 104 | } | |
| 105 | ||
| 106 | @Operation(summary = "Launch Job to Update Cow Health") | |
| 107 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 108 | @PostMapping("/launch/updatecowhealth") | |
| 109 | public Job updateCowHealth( | |
| 110 | ) { | |
| 111 | JobContextConsumer updateCowHealthJob = updateCowHealthJobFactory.create(); | |
| 112 |
1
1. updateCowHealth : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::updateCowHealth → KILLED |
return jobService.runAsJob(updateCowHealthJob); |
| 113 | } | |
| 114 | ||
| 115 | @Operation(summary = "Launch Job to Set Cow Health") | |
| 116 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 117 | @PostMapping("/launch/setcowhealth") | |
| 118 | public Job setCowHealth( | |
| 119 | @Parameter(name="commonsID") @RequestParam Long commonsID, | |
| 120 | @Parameter(name="health") @RequestParam double health | |
| 121 | ) { | |
| 122 | JobContextConsumer setCowHealthJob = setCowHealthJobFactory.create(commonsID, health); | |
| 123 |
1
1. setCowHealth : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::setCowHealth → KILLED |
return jobService.runAsJob(setCowHealthJob); |
| 124 | } | |
| 125 | ||
| 126 | @Operation(summary = "Launch Job to Produce Instructor Report") | |
| 127 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 128 | @PostMapping("/launch/instructorreport") | |
| 129 | public Job instructorReport( | |
| 130 | ) { | |
| 131 | InstructorReportJob instructorReportJob = (InstructorReportJob) instructorReportJobFactory.create(); | |
| 132 |
1
1. instructorReport : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReport → KILLED |
return jobService.runAsJob(instructorReportJob); |
| 133 | } | |
| 134 | ||
| 135 | @Operation(summary = "Launch Job to Produce Instructor Report for a single commons") | |
| 136 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 137 | @PostMapping("/launch/instructorreportsinglecommons") | |
| 138 | public Job instructorReportSingleCommons( | |
| 139 | @Parameter(name="commonsId") @RequestParam Long commonsId | |
| 140 | ) { | |
| 141 | ||
| 142 | InstructorReportJobSingleCommons instructorReportJobSingleCommons = (InstructorReportJobSingleCommons) instructorReportJobSingleCommonsFactory.create(commonsId); | |
| 143 |
1
1. instructorReportSingleCommons : replaced return value with null for edu/ucsb/cs156/happiercows/controllers/JobsController::instructorReportSingleCommons → KILLED |
return jobService.runAsJob(instructorReportJobSingleCommons); |
| 144 | } | |
| 145 | } | |
Mutations | ||
| 68 |
1.1 |
|
| 79 |
1.1 |
|
| 94 |
1.1 |
|
| 103 |
1.1 |
|
| 112 |
1.1 |
|
| 123 |
1.1 |
|
| 132 |
1.1 |
|
| 143 |
1.1 |