|
1
|
|
|
package easytests.api.v1.mappers; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.api.v1.models.Quiz; |
|
4
|
|
|
import easytests.api.v1.models.Testee; |
|
5
|
|
|
import easytests.core.models.QuizModel; |
|
6
|
|
|
import easytests.core.models.empty.AbstractModelEmpty; |
|
7
|
|
|
import java.time.LocalDateTime; |
|
8
|
|
|
import java.time.format.DateTimeFormatter; |
|
9
|
|
|
import org.modelmapper.Condition; |
|
10
|
|
|
import org.modelmapper.Converter; |
|
11
|
|
|
import org.modelmapper.ModelMapper; |
|
12
|
|
|
import org.springframework.stereotype.Service; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author miron97 |
|
17
|
|
|
*/ |
|
18
|
|
|
@Service("QuizzesMapperV1") |
|
19
|
|
|
public class QuizzesMapper extends ModelMapper { |
|
20
|
|
|
private static final String Z = "Z"; |
|
21
|
|
|
|
|
22
|
|
|
private Condition notEmptyModel = context -> !(context.getSource() instanceof AbstractModelEmpty); |
|
23
|
|
|
|
|
24
|
|
|
private Condition notNull = context -> context.getSource() != null; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
private Converter<LocalDateTime, String> localDateTimeStringConverter = dateTime -> { |
|
27
|
|
|
if (dateTime.getSource() != null) { |
|
28
|
|
|
return dateTime.getSource().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + Z; |
|
29
|
|
|
} else { |
|
30
|
|
|
return null; |
|
31
|
|
|
} |
|
32
|
|
|
}; |
|
33
|
|
|
|
|
34
|
|
|
public QuizzesMapper() { |
|
35
|
|
|
super(); |
|
36
|
|
|
this.createTypeMap(QuizModel.class, Quiz.class) |
|
37
|
|
|
.addMappings(mapper -> mapper.when(notEmptyModel) |
|
38
|
|
|
.<Testee>map(quizModel -> quizModel.getTestee(), |
|
39
|
|
|
(quiz, testee) -> quiz.setTestee(testee))) |
|
40
|
|
|
.addMappings(mapper -> mapper.when(notEmptyModel) |
|
41
|
|
|
.<Integer>map(quizModel -> quizModel.getTestee().getId(), |
|
42
|
|
|
(quiz, id) -> quiz.getTestee().setId(id))) |
|
43
|
|
|
.addMappings(mapper -> mapper.when(notEmptyModel) |
|
44
|
|
|
.<Integer>map(quizModel -> quizModel.getTestee().getQuiz().getId(), |
|
45
|
|
|
(quiz, id) -> quiz.getTestee().getQuiz().setId(id))) |
|
46
|
|
|
.addMappings(mapper -> mapper.using(localDateTimeStringConverter) |
|
47
|
|
|
.<String>map(QuizModel::getStartedAt, (quiz, startedAt) -> quiz.setStartedAt(startedAt))) |
|
48
|
|
|
.addMappings(mapper -> mapper.using(localDateTimeStringConverter) |
|
49
|
|
|
.<String>map(QuizModel::getFinishedAt, (quiz, finishedAt) -> quiz.setFinishedAt(finishedAt))); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|