This repository was archived by the owner on Sep 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelController.java
More file actions
84 lines (65 loc) · 3.49 KB
/
LevelController.java
File metadata and controls
84 lines (65 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package pt.ua.deti.codespell.codespellbackend.controller;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pt.ua.deti.codespell.codespellbackend.code_execution.CodeExecutionHandler;
import pt.ua.deti.codespell.codespellbackend.exception.implementations.BadRequestException;
import pt.ua.deti.codespell.codespellbackend.model.*;
import pt.ua.deti.codespell.codespellbackend.request.MessageResponse;
import pt.ua.deti.codespell.codespellbackend.service.LevelService;
import pt.ua.deti.codespell.codespellbackend.service.ScoreService;
@RestController
@RequestMapping("/api/level")
public class LevelController {
private final LevelService levelService;
private final ScoreService scoreService;
private final CodeExecutionHandler codeExecutionHandler;
@Autowired
public LevelController(LevelService levelService, ScoreService scoreService, CodeExecutionHandler codeExecutionHandler) {
this.levelService = levelService;
this.scoreService = scoreService;
this.codeExecutionHandler = codeExecutionHandler;
}
@GetMapping("/{level_id}/leaderboards")
public List<Score> getLevelLeaderboard(@PathVariable(value = "level_id") String levelIdStr, @RequestBody Settings settings) {
ObjectId levelId = new ObjectId(levelIdStr);
return scoreService.getScoresByLevelAndSettings(levelId, settings);
}
@GetMapping("/{level_id}/documentation")
public List<Documentation> getLevelDocumentation(@PathVariable(value = "level_id") String levelIdStr, ProgrammingLanguage language) {
ObjectId levelId = new ObjectId(levelIdStr);
Level level = levelService.findByLevelId(levelId);
return level.getDocumentation();
}
@GetMapping("/")
public List<Level> getLevelsList(ProgrammingLanguage programmingLanguage, SkillLevel skillLevel) {
return levelService.getAllLevels();
}
@GetMapping("/{level_id}/solutions")
public List<Solution> getLevelSolutions(@PathVariable(value = "level_id") String levelIdStr, ProgrammingLanguage language) {
ObjectId levelId = new ObjectId(levelIdStr);
Level level = levelService.findByLevelId(levelId);
return level.getSolutions();
}
@PostMapping("/{level_id}/submit/{solution_id}")
public MessageResponse submitLevelSolution(@PathVariable(value = "level_id") String levelIdStr, @PathVariable(value="solution_id") String solutionUniqueIdStr, @RequestBody String code) {
if (!ObjectId.isValid(levelIdStr))
throw new BadRequestException("Unable to parse level_id.");
if (!solutionUniqueIdStr.matches("^[\\da-fA-F]{8}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{4}-[\\da-fA-F]{12}$"))
throw new BadRequestException("Unable to parse solution_id to UUID");
ObjectId levelId = new ObjectId(levelIdStr);
Level level = levelService.findByLevelId(levelId);
UUID solutionUUID = UUID.fromString(solutionUniqueIdStr);
codeExecutionHandler.scheduleCodeExecution(level, solutionUUID, code);
return new MessageResponse(Date.from(Instant.now()), "Solution successfully submitted.");
}
@GetMapping("/{level_id}")
public Level getCurrentLevel(@PathVariable(value = "level_id") String levelIdStr) {
ObjectId levelId = new ObjectId(levelIdStr);
return levelService.findByLevelId(levelId);
}
}