-
Notifications
You must be signed in to change notification settings - Fork 3
/
SurveysController.cs
483 lines (423 loc) · 11.5 KB
/
SurveysController.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
using BlazingApple.Survey.Shared;
using BlazingApple.Survey.Shared.DataTransferObjects;
using BlazingAppleConsumer.Server.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Radzen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Claims;
using System.Threading.Tasks;
using Shared = BlazingApple.Survey.Shared;
namespace BlazingAppleConsumer.Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SurveysController : ControllerBase
{
private readonly ApplicationDbContext _context;
public SurveysController(ApplicationDbContext context)
{
_context = context;
}
// DELETE: api/Surveys/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteSurvey(Guid id)
{
Shared.Survey? survey = await _context.Surveys.FindAsync(id);
if (survey == null)
{
return NotFound();
}
_context.Surveys.Remove(survey);
await _context.SaveChangesAsync();
return NoContent();
}
// DELETE: api/surveys/questions/5
[HttpDelete("questions/{id}")]
public async Task<IActionResult> DeleteQuestion(Guid id)
{
Question? question = await _context.Questions.FindAsync(id);
if (question == null)
{
return NotFound();
}
_context.Questions.Remove(question);
await _context.SaveChangesAsync();
return NoContent();
}
// GET: api/surveys/questions/5
[HttpGet("questions/{id}")]
public async Task<ActionResult<Question>> GetQuestion(Guid questionId)
{
Question? question = await _context.Questions.FirstOrDefaultAsync(q => q.Id == questionId);
return question == null ? (ActionResult<Question>)NotFound() : (ActionResult<Question>)Ok(question);
}
// GET: api/surveys/5
[HttpGet("{id}")]
public async Task<ActionResult<Shared.Survey>> GetSurvey(Guid id)
{
Shared.Survey? survey = await _context.Surveys
.Include(surv => surv.Questions)
.ThenInclude(q => q.Options)
.Include(surv => surv.Questions)
.ThenInclude(question => question.Answers)
.FirstOrDefaultAsync(surv => surv.Id == id);
return survey == null ? (ActionResult<Shared.Survey>)NotFound() : (ActionResult<Shared.Survey>)Ok(survey);
}
// GET: api/surveys/5
[HttpGet("answers/{id}")]
public async Task<ActionResult<Answer>> GetSurveyAnswer(Guid id)
{
Answer? surveyAnswer = await _context.Answers.FindAsync(id);
return surveyAnswer == null ? (ActionResult<Answer>)NotFound() : (ActionResult<Answer>)Ok(surveyAnswer);
}
// GET: api/surveys/answers
[HttpGet("answers")]
public async Task<ActionResult<List<Answer>>> GetSurveyAnswers()
{
return await _context.Answers.ToListAsync();
}
// GET: api/surveys/5/questions
[HttpGet("{id}/questions")]
public async Task<ActionResult<List<Question>>> GetQuestions(Guid surveyId)
{
return await _context.Questions.Where(question => question.SurveyId == surveyId).ToListAsync();
}
[HttpPost("results/{surveyId}")]
public async Task<ActionResult<List<DTOQuestion>>> GetSurveyResults(Guid surveyId, LoadArgs loadArgs)
{
IQueryable<Question> query = _context.Questions
.Where(x => x.SurveyId == surveyId && x.Type != QuestionType.TextArea)
.Include(x => x.Options)
.OrderBy(x => x.Position);
List<Question> results = await query
.Skip(loadArgs.Skip)
.Take(loadArgs.Top)
.ToListAsync();
List<DTOQuestion> SurveyResultsCollection = new();
foreach (Question question in results)
{
DTOQuestion newDtoQuestion = new()
{
Id = question.Id,
Prompt = question.Prompt,
Type = question.Type,
Position = question.Position,
Required = question.Required
};
List<AnswerResponse> answers = new();
if (question.Type is QuestionType.Date or QuestionType.DateTime)
{
List<AnswerResponse> tempAnswerResponses = _context.Answers
.Where(x => x.QuestionId == question.Id)
.GroupBy(n => n.AnswerValueDateTime)
.Select(n => new AnswerResponse
{
OptionLabel = (n.Key ?? DateTime.MinValue).ToString(),
Responses = n.Count()
}
).OrderBy(n => n.OptionLabel).ToList();
foreach (AnswerResponse? answer in tempAnswerResponses)
{
string ShortDate = answer.OptionLabel;
try
{
DateTime dtTempDate = Convert.ToDateTime(answer.OptionLabel);
ShortDate = dtTempDate.ToShortDateString();
}
catch
{
// use original string
}
answers.Add(
new AnswerResponse
{
OptionLabel = ShortDate,
Responses = answer.Responses
});
}
}
else
{
answers = _context.Answers
.Where(x => x.QuestionId == question.Id)
.GroupBy(n => n.AnswerValue)
.Select(n => new AnswerResponse
{
OptionLabel = n.Key ?? string.Empty,
Responses = n.Count()
})
.OrderBy(n => n.OptionLabel).ToList();
}
if (answers.Count > 10)
{
// Only take top 10
newDtoQuestion.AnswerResponses = answers
.OrderByDescending(x => x.Responses)
.Take(10).ToList();
// Put remaining questions in "Other"
List<AnswerResponse> otherAnswers = answers.OrderByDescending(x => x.Responses).Skip(10).ToList();
double sumOfOther = otherAnswers.Sum(x => x.Responses);
newDtoQuestion.AnswerResponses.Add(new AnswerResponse() { OptionLabel = "Other", Responses = sumOfOther });
}
else
{
newDtoQuestion.AnswerResponses = answers;
}
SurveyResultsCollection.Add(newDtoQuestion);
}
return Ok(SurveyResultsCollection);
}
[HttpGet("results/{surveyId}/count")]
public async Task<ActionResult<int>> GetSurveyResultsCount([FromRoute] Guid surveyId)
{
int resultsCount = await _context.Answers
.AsNoTracking()
.Where(a => a.Question!.SurveyId == surveyId)
.Select(a => a.UserId)
.Distinct()
.CountAsync();
return resultsCount;
}
// GET: api/surveys
[HttpGet()]
public async Task<ActionResult<List<Shared.Survey>>> GetSurveys()
{
List<Shared.Survey> surveys = await _context.Surveys
.Include(surv => surv.Questions)
.ThenInclude(question => question.Options)
.Include(surv => surv.Questions)
.ThenInclude(question => question.Answers).ToListAsync();
return surveys;
}
// POST: api/surveys/questions
[HttpPost("questions")]
public async Task<ActionResult<Shared.Survey>> PostQuestion(Question question)
{
if (QuestionExists(question.Id))
{
await PutQuestion(question.Id, question);
return Ok(question);
}
else
{
_context.Questions.Add(question);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (QuestionExists(question.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetQuestion), new { id = question.Id }, question);
}
}
// POST: api/Users
[HttpPost]
public async Task<ActionResult<Shared.Survey>> PostSurvey(Shared.Survey survey)
{
survey.UserId = GetUserId();
if (SurveyExists(survey.Id))
{
await PutSurvey(survey.Id, survey);
return Ok(survey);
}
else
{
_context.Surveys.Add(survey);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (SurveyExists(survey.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetSurvey), new { id = survey.Id }, survey);
}
}
// POST: api/surveys/answers
[HttpPost("answers")]
public async Task<ActionResult<bool>> PostSurveyAnswer(DTOSurvey dtoSurvey)
{
dtoSurvey.UserId = GetUserId();
if (dtoSurvey.Questions == null)
{
return false;
}
foreach (DTOQuestion question in dtoSurvey.Questions)
{
// Delete possible existing answer
List<Answer> ExistingAnswers = await _context.Answers
.Where(x => x.QuestionId == question.Id && x.UserId == dtoSurvey.UserId)
.ToListAsync();
if (ExistingAnswers != null)
{
_context.Answers.RemoveRange(ExistingAnswers);
await _context.SaveChangesAsync();
}
// Save Answer
if (question.Type != QuestionType.DropdownMultiSelect)
{
Answer NewSurveyAnswer = new()
{
AnswerValue = question.AnswerValueString,
AnswerValueDateTime = question.AnswerValueDateTime,
QuestionId = question.Id,
UserId = dtoSurvey.UserId
};
_context.Answers.Add(NewSurveyAnswer);
await _context.SaveChangesAsync();
}
if (question.AnswerValueList != null)
{
foreach (string answer in question.AnswerValueList)
{
Answer NewSurveyAnswerValueList = new()
{
AnswerValue = answer,
QuestionId = question.Id,
UserId = dtoSurvey.UserId
};
_context.Answers.Add(NewSurveyAnswerValueList);
await _context.SaveChangesAsync();
}
}
}
return true;
}
// PUT: api/surveys/questions/5
[HttpPut("questions/{id}")]
public async Task<IActionResult> PutQuestion(Guid id, Question question)
{
if (id != question.Id)
{
return BadRequest();
}
if (question.Options is not null)
{
IEnumerable<Guid> optionIds = question.Options.Select(o => o.Id);
IQueryable<QuestionOption> existingOptions = _context.QuestionOptions
.Where(lqo => lqo.QuestionId == id && !optionIds.Contains(lqo.Id));
_context.Entry(question).State = EntityState.Modified;
_context.QuestionOptions.RemoveRange(existingOptions);
IEnumerable<QuestionOption> newOptions = question.Options.Where(o => o.Id == default);
foreach (QuestionOption option in newOptions)
{
_context.Entry(option).State = EntityState.Added;
}
}
else
{
_context.Entry(question).State = EntityState.Modified;
}
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!QuestionExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetQuestion), new { id = question.Id }, question);
}
// PUT: api/Surveys/5
[HttpPut("{id}")]
public async Task<ActionResult<Shared.Survey>> PutSurvey(Guid id, Shared.Survey survey)
{
if (id != survey.Id)
{
return BadRequest();
}
_context.Entry(survey).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SurveyExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return CreatedAtAction(nameof(GetSurvey), new { id = survey.Id }, survey);
}
// PUT: api/surveys/answers/5
[HttpPut("answers/{id}")]
public async Task<IActionResult> PutSurveyAnswer(Guid id, Answer surveyAnswer)
{
if (id != surveyAnswer.Id)
{
return BadRequest();
}
_context.Entry(surveyAnswer).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!SurveyAnswerExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
private string? GetUserId()
{
foreach (Claim claim in HttpContext.User.Claims)
{
Console.WriteLine(claim.Type + ":\t\t-\t" + claim.Value);
}
return HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
}
private bool QuestionExists(Guid id)
{
return _context.Questions.Any(e => e.Id == id);
}
private bool SurveyAnswerExists(Guid id)
{
return _context.Answers.Any(e => e.Id == id);
}
private bool SurveyExists(Guid id)
{
return _context.Surveys.Any(e => e.Id == id);
}
}
}