This repository was archived by the owner on May 26, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathFormController.cs
More file actions
201 lines (174 loc) · 7.15 KB
/
Copy pathFormController.cs
File metadata and controls
201 lines (174 loc) · 7.15 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;
using VoteMonitor.Api.Core.Options;
using VoteMonitor.Api.Form.Commands;
using VoteMonitor.Api.Form.Models;
using VoteMonitor.Api.Form.Queries;
namespace VoteMonitor.Api.Form.Controllers
{
/// <inheritdoc />
/// <summary>
/// Form Controller offers support for CRUD Operations on the forms completed by observers.
/// </summary>
[Route("api/v1/form")]
public class FormController : Controller
{
private readonly ApplicationCacheOptions _cacheOptions;
private readonly IMediator _mediator;
public FormController(IMediator mediator, IOptions<ApplicationCacheOptions> cacheOptions)
{
_cacheOptions = cacheOptions.Value;
_mediator = mediator;
}
[HttpPost]
[Authorize("NgoAdmin")]
public async Task<ActionResult<int>> AddForm([FromBody] FormDTO newForm)
{
var formExists = await _mediator.Send(new ExistsFormByCodeOrIdQuery()
{
Id = newForm.Id,
Code = newForm.Code
});
if (formExists)
{
return BadRequest($"The form with the given code/id already exists");
}
var result = await _mediator.Send(new AddFormCommand { Form = newForm });
return result.Id;
}
[HttpPut]
[Authorize("Organizer")]
public async Task<ActionResult> UpdateForm([FromBody] FormDTO newForm)
{
var formExists = await _mediator.Send(new GetFormExistsByIdQuery() { Id = newForm.Id });
if (!formExists)
{
return NotFound($"The form with id {newForm.Id} was not found.");
}
var result = await _mediator.Send(new UpdateFormCommand { Id = newForm.Id, Form = newForm });
return Ok(result.Id);
}
/// <summary>
/// Returneaza versiunea tuturor formularelor sub forma unui array.
/// Daca versiunea returnata difera de cea din aplicatie, atunci trebuie incarcat formularul din nou
/// </summary>
/// <returns></returns>
[Authorize]
[HttpGet("versions")]
[Produces(typeof(Dictionary<string, int>))]
public async Task<IActionResult> GetFormVersions()
{
var formsAsDict = new Dictionary<string, int>();
(await _mediator.Send(new FormVersionQuery(null, null))).ForEach(form => formsAsDict.Add(form.Code, form.CurrentVersion));
return Ok(new { Versions = formsAsDict });
}
/// <summary>
/// Returns an array of forms
/// </summary>
/// <returns></returns>
[Authorize]
[HttpGet]
public async Task<IActionResult> GetFormsAsync(bool? diaspora, bool? draft)
=> Ok(new FormVersionsModel { FormVersions = await _mediator.Send(new FormVersionQuery(diaspora, draft)) });
/// <summary>
/// Se interogheaza ultima versiunea a formularului pentru observatori si se primeste definitia lui.
/// In definitia unui formular nu intra intrebarile standard (ora sosirii, etc).
/// Acestea se considera implicite pe fiecare formular.
/// </summary>
/// <param name="formId">Id-ul formularului pentru care trebuie preluata definitia</param>
/// <returns></returns>
[Authorize]
[HttpGet("{formId}")]
public async Task<IEnumerable<FormSectionDTO>> GetFormAsync(int formId)
{
var result = await _mediator.Send(new FormQuestionQuery
{
FormId = formId,
CacheHours = _cacheOptions.Hours,
CacheMinutes = _cacheOptions.Minutes,
CacheSeconds = _cacheOptions.Seconds
});
return result;
}
[HttpDelete]
[Authorize("Organizer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteForm(int formId)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _mediator.Send(new DeleteFormCommand { FormId = formId });
if (result.IsFailure)
{
switch (result.Error)
{
case DeleteFormErrorType.FormHasAnswers:
return BadRequest("Could not delete with form that has answers.");
case DeleteFormErrorType.FormNotFound:
return BadRequest("Could not find form with requested id.");
case DeleteFormErrorType.FormNotDraft:
return BadRequest("Could not delete a non draft form.");
default:
return BadRequest("An error occured when deleting form.");
}
}
return Ok();
}
[HttpDelete("section/{sectionId}")]
[Authorize("Organizer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteSection(int sectionId)
{
var deleted = await _mediator.Send(new DeleteSectionCommand { SectionId = sectionId });
if (!deleted)
{
return BadRequest("The section could not be deleted. Make sure the section exists and it doesn't already have saved answers.");
}
return Ok();
}
[HttpDelete("section/{sectionId}/question/{questionId}")]
[Authorize("Organizer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteQuestionAsync(int sectionId, int questionId)
{
var deleted = await _mediator.Send(new DeleteQuestionCommand()
{
QuestionId = questionId,
SectionId = sectionId
});
if (!deleted)
{
return BadRequest("The question could not be deleted. Make sure the question exists.");
}
return Ok();
}
[HttpDelete("section/{sectionId}/question/{questionId}/option/{optionId}")]
[Authorize("Organizer")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> DeleteQuestionOptionAsync(int sectionId, int questionId, int optionId)
{
var deleted = await _mediator.Send(new DeleteQuestionOptionCommand()
{
QuestionId = questionId,
SectionId = sectionId,
OptionId = optionId
});
if (!deleted)
{
return BadRequest("The option could not be deleted. Make sure the option exists.");
}
return Ok();
}
}
}