-
Notifications
You must be signed in to change notification settings - Fork 2
Extracted services and controllers into separate files #91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/main/kotlin/ch/uzh/ifi/access/controller/ExampleController.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package ch.uzh.ifi.access.controller | ||
|
||
import ch.uzh.ifi.access.model.constants.TaskStatus | ||
import ch.uzh.ifi.access.model.dto.ExampleDurationDTO | ||
import ch.uzh.ifi.access.model.dto.ExampleInformationDTO | ||
import ch.uzh.ifi.access.model.dto.SubmissionDTO | ||
import ch.uzh.ifi.access.projections.TaskWorkspace | ||
import ch.uzh.ifi.access.service.EmitterService | ||
import ch.uzh.ifi.access.service.ExampleService | ||
import ch.uzh.ifi.access.service.RoleService | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.scheduling.annotation.EnableAsync | ||
import org.springframework.security.access.prepost.PreAuthorize | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.web.bind.annotation.* | ||
import org.springframework.web.server.ResponseStatusException | ||
|
||
|
||
@RestController | ||
@RequestMapping("/courses/{course}/examples") | ||
@EnableAsync | ||
class ExampleController( | ||
private val exampleService: ExampleService, | ||
private val roleService: RoleService, | ||
private val emitterService: EmitterService, | ||
) { | ||
private val logger = KotlinLogging.logger {} | ||
|
||
// TODO: Change this to returning TaskOverview, as we don't need more information. However, when changing it, an error occurs in the courses/{course} endpoint. | ||
@GetMapping("") | ||
@PreAuthorize("hasRole(#course)") | ||
fun getExamples( | ||
@PathVariable course: String, | ||
authentication: Authentication | ||
): List<TaskWorkspace> { | ||
return exampleService.getExamples(course) | ||
} | ||
|
||
@GetMapping("/{example}/information") | ||
@PreAuthorize("hasRole(#course+'-assistant')") | ||
fun getGeneralInformation( | ||
@PathVariable course: String, | ||
@PathVariable example: String, | ||
authentication: Authentication | ||
): ExampleInformationDTO { | ||
val participantsOnline = roleService.getOnlineCount(course) | ||
val totalParticipants = exampleService.getCourseBySlug(course).participantCount | ||
val numberOfStudentsWhoSubmitted = exampleService.countStudentsWhoSubmittedExample(course, example) | ||
val passRatePerTestCase = exampleService.getExamplePassRatePerTestCase(course, example) | ||
|
||
return ExampleInformationDTO( | ||
participantsOnline, | ||
totalParticipants, | ||
numberOfStudentsWhoSubmitted, | ||
passRatePerTestCase | ||
) | ||
} | ||
|
||
@PostMapping("/{example}/submit") | ||
@PreAuthorize("hasRole(#course) and (#submission.restricted or hasRole(#course + '-assistant'))") | ||
fun evaluateExampleSubmission( | ||
@PathVariable course: String, | ||
@PathVariable example: String, | ||
@RequestBody submission: SubmissionDTO, | ||
authentication: Authentication | ||
) { | ||
submission.userId = authentication.name | ||
// Is there a better way than passing null to assignmentSlug? | ||
exampleService.createExampleSubmission(course, example, submission) | ||
} | ||
|
||
@GetMapping("/{example}/users/{user}") | ||
@PreAuthorize("hasRole(#course+'-assistant') or (#user == authentication.name)") | ||
fun getExample( | ||
@PathVariable course: String, | ||
@PathVariable example: String, | ||
@PathVariable user: String | ||
): TaskWorkspace { | ||
return exampleService.getExample(course, example, user) | ||
} | ||
|
||
// Invoked by the teacher when publishing an example to inform the students | ||
@PostMapping("/{example}/publish") | ||
@PreAuthorize("hasRole(#course+'-supervisor')") | ||
fun publishExample( | ||
@PathVariable course: String, | ||
@PathVariable example: String, | ||
@RequestBody body: ExampleDurationDTO, | ||
) { | ||
val activeExample = exampleService.getExamples(course).firstOrNull { | ||
it.status == TaskStatus.Interactive | ||
} | ||
|
||
if (activeExample != null) { | ||
throw ResponseStatusException( | ||
HttpStatus.NOT_FOUND, | ||
"An interactive example already exists." | ||
) | ||
} | ||
|
||
val updatedExample = exampleService.publishExampleBySlug(course, example, body.duration) | ||
|
||
emitterService.sendMessage(course, "redirect", "/courses/$course/examples/$example") | ||
emitterService.sendMessage(course, "timer-update", "${updatedExample.start}/${updatedExample.end}") | ||
} | ||
|
||
// Invoked by the teacher when want to extend the time of an active example by a certain amount of seconds | ||
@PutMapping("/{example}/extend") | ||
@PreAuthorize("hasRole(#course+'-supervisor')") | ||
fun extendExampleDeadline( | ||
@PathVariable course: String, | ||
@PathVariable example: String, | ||
@RequestBody body: ExampleDurationDTO, | ||
) { | ||
val updatedExample = exampleService.extendExampleDeadlineBySlug(course, example, body.duration) | ||
|
||
emitterService.sendMessage( | ||
course, | ||
"message", | ||
"Submission time extended by the lecturer by ${body.duration} seconds." | ||
) | ||
emitterService.sendMessage(course, "timer-update", "${updatedExample.start}/${updatedExample.end}") | ||
} | ||
|
||
// Invoked by the teacher when want to terminate the active example | ||
@PutMapping("/{example}/terminate") | ||
@PreAuthorize("hasRole(#course+'-supervisor')") | ||
fun terminateExample( | ||
@PathVariable course: String, | ||
@PathVariable example: String | ||
) { | ||
val updatedExample = exampleService.terminateExampleBySlug(course, example) | ||
emitterService.sendMessage( | ||
course, | ||
"message", | ||
"The example has been terminated by the lecturer." | ||
) | ||
emitterService.sendMessage(course, "timer-update", "${updatedExample.start}/${updatedExample.end}") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ import lombok.Data | |
class Results( | ||
var points: Double? = null, | ||
var hints: MutableList<String> = mutableListOf(), | ||
var tests: MutableList<String> = mutableListOf() | ||
var tests: MutableList<String> = mutableListOf(), | ||
var testScores: MutableList<Double> = mutableListOf() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer needed, as discussed. |
||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here:)