Update to Async Attachement, Answer and Update Multiple Answers submission - backlog #288

This commit is contained in:
Reginald Cherenfant Jasmin
2023-08-27 11:55:58 -04:00
parent 5a641ff3aa
commit 96ccb96bf1
50 changed files with 166 additions and 1087 deletions

View File

@ -4,7 +4,9 @@ using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace DamageAssesment.Api.SurveyResponses.Providers
@ -251,7 +253,6 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
answers = new List<Models.SurveyResponse>();
return (true, answers, "Empty object returned");
}
}
catch (Exception ex)
{
@ -266,16 +267,16 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
{
if (surveyResponse != null)
{
var surveyResponses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
surveyResponse.Id = surveyResponses.Count + 1;
surveyResponseDbContext.SurveyResponses.Add(mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse));
surveyResponseDbContext.SaveChanges();
var _surveyResponse = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse);
surveyResponseDbContext.SurveyResponses.Add(_surveyResponse);
await surveyResponseDbContext.SaveChangesAsync();
surveyResponse.Id = _surveyResponse.Id;
return (true, surveyResponse, "Request Successful");
}
else
{
logger?.LogInformation($"SurveyResponseID={surveyResponse.Id} cannot be added");
return (false, null, "Survey cannot be added");
logger?.LogInformation($"SurveyResponse cannot be added");
return (false, null, "SurveyResponse cannot be added");
}
}
catch (Exception ex)
@ -299,7 +300,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
_SurveyResponse.SurveyId = SurveyResponse.SurveyId;
_SurveyResponse.EmployeeId = SurveyResponse.EmployeeId;
_SurveyResponse.LocationId = SurveyResponse.LocationId;
surveyResponseDbContext.SaveChanges();
await surveyResponseDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
}
else
@ -331,7 +332,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
if (_SurveyResponse != null)
{
surveyResponseDbContext.Remove(_SurveyResponse);
surveyResponseDbContext.SaveChanges();
await surveyResponseDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
}
else
@ -458,7 +459,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.Id,
ans.AnswerText,
ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
@ -503,7 +504,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.QuestionId,
ans.AnswerText,
ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
@ -546,7 +547,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.QuestionId,
ans.AnswerText,
ans.Comment,
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
@ -650,7 +651,7 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
ans.Id,
ans.AnswerText,
ans.Comment,
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.CategoryId, q.Questions }).SingleOrDefault(),
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
}
};
@ -709,67 +710,75 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
}
bool ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId)
async Task<bool> ProcessAnswers(AnswerRequest answerRequest, int surveyResponseId)
{
if (questionRequest != null)
if (answerRequest != null)
{
var answer = answerServiceProvider.PostAnswersAsync(new Answer {QuestionId = questionRequest.QuestionId, AnswerText = questionRequest.AnswerText, Comment = questionRequest.Comment, SurveyResponseId = surveyResponseId });
if (answer != null)
{
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = questionRequest.PostedFiles });
var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
var answer = await answerServiceProvider.PostAnswersAsync(new Models.Answer {QuestionId = answerRequest.QuestionId, AnswerText = answerRequest.AnswerText, Comment = answerRequest.Comment, SurveyResponseId = surveyResponseId });
if (answer != null)
{
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = answerRequest.PostedFiles });
var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
string message = $"Answer for question {questionRequest.QuestionId} saved to the database";
logger?.LogInformation(message);
return (true);
}
else
{
string message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database";
logger?.LogInformation(message);
return (false);
}
string message = $"Answer for question {answerRequest.QuestionId} saved to the database";
logger?.LogInformation(message);
return (true);
}
else
{
string message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database";
logger?.LogInformation(message);
return (false);
}
}
else
{
var message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database - questionRequest object is null";
var message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database - answerRequest object is null";
logger?.LogInformation(message);
return (false);
}
}
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.AnswerRequest answers)
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.Request request)
{
try
{
if (answers != null)
if (request != null)
{
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { Id = 0, SurveyId = answers.SurveyId, EmployeeId = answers.EmployeeId, LocationId = answers.LocationId });
var response = await PostSurveyResponseAsync(new Models.SurveyResponse {SurveyId = request.SurveyId, EmployeeId = request.EmployeeId, LocationId = request.LocationId });
if (response.IsSuccess)
{
var surveyResponse = response.SurveyResponse;
var tasks = request.Answers.Select(x => ProcessAnswers(x,surveyResponse.Id));
var answerTasks = new List<Task>(); //new List<string>();
//var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID));
foreach (QuestionRequest ans in answers.Answers)
{
ProcessAnswers(ans, surveyResponse.Id);
// foreach (AnswerRequest ans in request.Answers)
// {
// answer = new Models.Answer { QuestionId = ans.QuestionId, AnswerText = ans.AnswerText, Comment = ans.Comment, SurveyResponseId = surveyResponse.Id };
//var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
//await Task.Delay(500);
// var rep = await answerServiceProvider.PostAnswersAsync(answer);
// var jsonObject = JsonConvert.SerializeObject(rep);
//x = x + jsonObject.ToString() + " ";
//i++;
// answer = new Models.Answer { QuestionId = ans.QuestionId, AnswerText = ans.AnswerText, Comment = ans.Comment, SurveyResponseId = surveyResponse.Id };
// await answerServiceProvider.PostAnswersAsync(answer);
//ProcessAnswers(ans, surveyResponse.Id);
//var stopwatch = new Stopwatch();
//stopwatch.Start();
// var task = Task.Run(() => ProcessAnswers(ans, surveyResponse.Id));
//var task = await ProcessAnswers(ans, surveyResponse.Id);
//answerTasks.Add(task);
//stopwatch.Stop();
//answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id));
}
//await Task.WhenAll(answerTasks);
// }
await Task.WhenAll(tasks);
return (true, surveyResponse, null);
}
else