|
1
|
|
|
package easytests.api.v1.controllers; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.api.v1.exceptions.ForbiddenException; |
|
4
|
|
|
import easytests.api.v1.exceptions.NotFoundException; |
|
5
|
|
|
import easytests.api.v1.mappers.QuizzesMapper; |
|
6
|
|
|
import easytests.api.v1.models.Quiz; |
|
7
|
|
|
import easytests.core.models.IssueModelInterface; |
|
8
|
|
|
import easytests.core.models.QuizModelInterface; |
|
9
|
|
|
import easytests.core.options.QuizzesOptionsInterface; |
|
10
|
|
|
import easytests.core.options.builder.QuizzesOptionsBuilderInterface; |
|
11
|
|
|
import easytests.core.services.IssuesServiceInterface; |
|
12
|
|
|
import easytests.core.services.QuizzesServiceInterface; |
|
13
|
|
|
import java.util.List; |
|
14
|
|
|
import java.util.stream.Collectors; |
|
15
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
16
|
|
|
import org.springframework.beans.factory.annotation.Qualifier; |
|
17
|
|
|
import org.springframework.web.bind.annotation.*; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author miron97 |
|
22
|
|
|
*/ |
|
23
|
|
|
@RestController("QuizzesControllerV1") |
|
24
|
|
|
@SuppressWarnings("checkstyle:MultipleStringLiterals") |
|
25
|
|
|
@RequestMapping("/v1/quizzes") |
|
26
|
|
|
public class QuizzesController extends AbstractController { |
|
27
|
|
|
|
|
28
|
|
|
@Autowired |
|
29
|
|
|
protected QuizzesServiceInterface quizzesService; |
|
30
|
|
|
|
|
31
|
|
|
@Autowired |
|
32
|
|
|
private QuizzesOptionsBuilderInterface quizzesOptions; |
|
33
|
|
|
|
|
34
|
|
|
@Autowired |
|
35
|
|
|
private IssuesServiceInterface issuesService; |
|
36
|
|
|
|
|
37
|
|
|
@Autowired |
|
38
|
|
|
@Qualifier("QuizzesMapperV1") |
|
39
|
|
|
private QuizzesMapper quizzesMapper; |
|
40
|
|
|
|
|
41
|
|
|
@GetMapping("") |
|
42
|
|
|
public List<Quiz> list(@RequestParam(name = "issueId", required = true) Integer issueId) |
|
43
|
|
|
throws NotFoundException, ForbiddenException { |
|
44
|
|
|
final IssueModelInterface issueModel = this.issuesService.find(issueId); |
|
45
|
|
|
|
|
46
|
|
|
if (issueModel == null) { |
|
47
|
|
|
throw new NotFoundException(); |
|
48
|
|
|
} |
|
49
|
|
|
if (!this.acl.hasAccess(issueModel)) { |
|
50
|
|
|
throw new ForbiddenException(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
final List<QuizModelInterface> quizModels = this.quizzesService |
|
54
|
|
|
.findByIssue(issueModel); |
|
55
|
|
|
|
|
56
|
|
|
return quizModels |
|
57
|
|
|
.stream() |
|
58
|
|
|
.map(model -> this.quizzesMapper.map(model, Quiz.class)) |
|
59
|
|
|
.collect(Collectors.toList()); |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* show(quizId) |
|
63
|
|
|
*/ |
|
64
|
|
|
|
|
65
|
|
|
@GetMapping("/{quizId}") |
|
66
|
|
|
public Quiz show(@PathVariable Integer quizId) throws NotFoundException, ForbiddenException { |
|
67
|
|
|
final QuizModelInterface quizModel = this.getQuizModel(quizId); |
|
68
|
|
|
if (!this.acl.hasAccess(quizModel)) { |
|
69
|
|
|
throw new ForbiddenException(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return this.quizzesMapper.map(quizModel, Quiz.class); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private QuizModelInterface getQuizModel(Integer id, QuizzesOptionsInterface quizOptions) throws NotFoundException { |
|
76
|
|
|
final QuizModelInterface quizModel = this.quizzesService.find(id, quizOptions); |
|
77
|
|
|
if (quizModel == null) { |
|
78
|
|
|
throw new NotFoundException(); |
|
79
|
|
|
} |
|
80
|
|
|
return quizModel; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private QuizModelInterface getQuizModel(Integer id) throws NotFoundException { |
|
84
|
|
|
return this.getQuizModel(id, this.quizzesOptions.forAuth()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|