-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathQuestionsMapper.cs
More file actions
140 lines (116 loc) · 5.59 KB
/
QuestionsMapper.cs
File metadata and controls
140 lines (116 loc) · 5.59 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Vote.Monitor.Domain.Entities.FormBase.Questions;
using Vote.Monitor.Form.Module.Models;
using Vote.Monitor.Form.Module.Requests;
namespace Vote.Monitor.Form.Module.Mappers;
public static class QuestionsMapper
{
public static BaseQuestionModel ToModel(BaseQuestion question)
{
switch (question)
{
case TextQuestion textInputQuestion:
return TextQuestionModel.FromEntity(textInputQuestion);
case NumberQuestion numberInputQuestionRequest:
return NumberQuestionModel.FromEntity(numberInputQuestionRequest);
case DateQuestion dateInputQuestionRequest:
return DateQuestionModel.FromEntity(dateInputQuestionRequest);
case SingleSelectQuestion singleSelectQuestionRequest:
return SingleSelectQuestionModel.FromEntity(singleSelectQuestionRequest);
case MultiSelectQuestion multiSelectQuestionRequest:
return MultiSelectQuestionModel.FromEntity(multiSelectQuestionRequest);
case RatingQuestion ratingQuestionRequest:
return RatingQuestionModel.FromEntity(ratingQuestionRequest);
default: throw new ApplicationException("Unknown question type received");
}
}
public static BaseQuestion ToEntity(BaseQuestionRequest question)
{
switch (question)
{
case TextQuestionRequest textInputQuestion:
return TextQuestion.Create(textInputQuestion.Id,
textInputQuestion.Code,
textInputQuestion.Text,
textInputQuestion.Helptext,
textInputQuestion.InputPlaceholder,
ToEntity(textInputQuestion.DisplayLogic));
case NumberQuestionRequest numberInputQuestion:
return NumberQuestion.Create(numberInputQuestion.Id,
numberInputQuestion.Code,
numberInputQuestion.Text,
numberInputQuestion.Helptext,
numberInputQuestion.InputPlaceholder,
ToEntity(numberInputQuestion.DisplayLogic));
case DateQuestionRequest dateInputQuestion:
return DateQuestion.Create(dateInputQuestion.Id,
dateInputQuestion.Code,
dateInputQuestion.Text,
dateInputQuestion.Helptext,
ToEntity(dateInputQuestion.DisplayLogic));
case SingleSelectQuestionRequest singleSelectQuestion:
var singleSelectQuestionOptions = ToEntities(singleSelectQuestion.Options);
return SingleSelectQuestion.Create(singleSelectQuestion.Id,
singleSelectQuestion.Code,
singleSelectQuestion.Text,
singleSelectQuestionOptions,
singleSelectQuestion.Helptext,
ToEntity(singleSelectQuestion.DisplayLogic));
case MultiSelectQuestionRequest multiSelectQuestion:
var multiSelectQuestionOptions = ToEntities(multiSelectQuestion.Options);
var multiSelectQuestionEntity = MultiSelectQuestion.Create(multiSelectQuestion.Id,
multiSelectQuestion.Code,
multiSelectQuestion.Text,
multiSelectQuestionOptions,
multiSelectQuestion.Helptext,
ToEntity(multiSelectQuestion.DisplayLogic));
return multiSelectQuestionEntity;
case RatingQuestionRequest ratingQuestion:
return RatingQuestion.Create(ratingQuestion.Id,
ratingQuestion.Code,
ratingQuestion.Text,
RatingScale.FromValue(ratingQuestion.Scale.Value),
ratingQuestion.Helptext,
ratingQuestion.LowerLabel,
ratingQuestion.UpperLabel,
ToEntity(ratingQuestion.DisplayLogic));
case MatrixQuestionRequest matrixQuestion:
var matrixQuestionOptions = ToEntities(matrixQuestion.Options);
var matrixQuestionRows = ToEntities(matrixQuestion.Rows);
return MatrixQuestion.Create(matrixQuestion.Id,
matrixQuestion.Code,
matrixQuestion.Text,
matrixQuestion.Helptext,
ToEntity(matrixQuestion.DisplayLogic),
matrixQuestionOptions,
matrixQuestionRows);
default: throw new ApplicationException("Unknown question type received");
}
}
private static IReadOnlyList<SelectOption> ToEntities(IEnumerable<SelectOptionRequest> options)
{
return options
.Select(o => SelectOption.Create(o.Id, o.Text, o.IsFreeText, o.IsFlagged))
.ToList()
.AsReadOnly();
}
private static IReadOnlyList<MatrixOption> ToEntities(IEnumerable<MatrixOptionRequest> options)
{
return options
.Select(o => MatrixOption.Create(o.Id, o.Text, o.IsFlagged))
.ToList()
.AsReadOnly();
}
private static IReadOnlyList<MatrixRow> ToEntities(IEnumerable<MatrixRowRequest> options)
{
return options
.Select(o => MatrixRow.Create(o.Id, o.Text))
.ToList()
.AsReadOnly();
}
private static DisplayLogic? ToEntity(DisplayLogicRequest? displayLogic)
{
return displayLogic == null
? null
: DisplayLogic.Create(displayLogic.ParentQuestionId, displayLogic.Condition, displayLogic.Value);
}
}