Conflict resolved in Questions, Surveys

This commit is contained in:
Reginald Cherenfant Jasmin 2023-08-25 17:51:07 -04:00
parent fe1614fee3
commit 099055d088
5 changed files with 33 additions and 26 deletions

View File

@ -108,7 +108,7 @@ namespace DamageAssesment.Api.Answers.Providers
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Db.Answer Answer) public (bool IsSuccess, Models.Answer Answer, string ErrorMessage) PostAnswerAsync(Db.Answer Answer)
{ {
try try
{ {

View File

@ -4,18 +4,10 @@ namespace DamageAssesment.Api.Employees.Models
{ {
public class Employee public class Employee
{ {
[Key]
public string Id { get; set; } public string Id { get; set; }
[StringLength(50)]
public string Name { get; set; } public string Name { get; set; }
public DateTime BirthDate { get; set; } public DateTime BirthDate { get; set; }
[StringLength(50)]
public string OfficePhoneNumber { get; set; } public string OfficePhoneNumber { get; set; }
[StringLength(50)]
public string Email { get; set; } public string Email { get; set; }
public bool IsActive { get; set; } public bool IsActive { get; set; }
public string? PreferredLanguage { get; set; } = "en"; public string? PreferredLanguage { get; set; } = "en";

View File

@ -709,16 +709,16 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
} }
async Task<bool> ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId) bool ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId)
{ {
if (questionRequest != null) if (questionRequest != null)
{ {
var answer = await answerServiceProvider.PostAnswersAsync(new Answer { Id = 0, QuestionId = questionRequest.QuestionId, AnswerText = questionRequest.AnswerText, Comment = questionRequest.Comment, SurveyResponseId = surveyResponseId }); var answer = answerServiceProvider.PostAnswersAsync(new Answer {QuestionId = questionRequest.QuestionId, AnswerText = questionRequest.AnswerText, Comment = questionRequest.Comment, SurveyResponseId = surveyResponseId });
if (answer != null) if (answer != null)
{ {
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>(); List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = questionRequest.PostedFiles }); listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = questionRequest.PostedFiles });
var attachments = await attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo }); var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
string message = $"Answer for question {questionRequest.QuestionId} saved to the database"; string message = $"Answer for question {questionRequest.QuestionId} saved to the database";
logger?.LogInformation(message); logger?.LogInformation(message);
@ -757,18 +757,19 @@ namespace DamageAssesment.Api.SurveyResponses.Providers
//var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID)); //var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID));
foreach (QuestionRequest ans in answers.Answers) foreach (QuestionRequest ans in answers.Answers)
{ {
ProcessAnswers(ans, surveyResponse.Id);
//var stopwatch = new Stopwatch(); //var stopwatch = new Stopwatch();
//stopwatch.Start(); //stopwatch.Start();
var task = Task.Run(() => ProcessAnswers(ans, surveyResponse.Id)); // var task = Task.Run(() => ProcessAnswers(ans, surveyResponse.Id));
//var task = await ProcessAnswers(ans, surveyResponse.Id); //var task = await ProcessAnswers(ans, surveyResponse.Id);
answerTasks.Add(task); //answerTasks.Add(task);
//stopwatch.Stop(); //stopwatch.Stop();
//answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id)); //answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id));
} }
await Task.WhenAll(answerTasks); //await Task.WhenAll(answerTasks);
return (true, surveyResponse, null); return (true, surveyResponse, null);
} }
else else

View File

@ -2,13 +2,25 @@
namespace DamageAssesment.Api.Surveys.Db namespace DamageAssesment.Api.Surveys.Db
{ {
public class SurveysDbContext:DbContext public class SurveysDbContext : DbContext
{ {
public DbSet<Db.Survey> Surveys { get; set; }
public DbSet<Db.SurveyTranslation> SurveysTranslation { get; set; }
public SurveysDbContext(DbContextOptions options) : base(options) public SurveysDbContext(DbContextOptions options) : base(options)
{ {
} }
public DbSet<Db.Survey> Surveys { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
public DbSet<Db.SurveyTranslation> SurveysTranslation { get; set; } {
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Survey>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<SurveyTranslation>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
} }
} }

View File

@ -60,7 +60,6 @@ namespace DamageAssesment.Api.Surveys.Providers
if (surveys != null) if (surveys != null)
{ {
if (string.IsNullOrEmpty(Language)) if (string.IsNullOrEmpty(Language))
{ {
surveysList = from s in surveys surveysList = from s in surveys
@ -184,17 +183,20 @@ namespace DamageAssesment.Api.Surveys.Providers
{ {
var surveys = await surveyDbContext.Surveys.ToListAsync(); var surveys = await surveyDbContext.Surveys.ToListAsync();
int Id = surveys.Count + 1; Db.Survey _survey = new Db.Survey { IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now };
surveyDbContext.Surveys.Add(new Db.Survey { Id = Id, IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now });
var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync(); surveyDbContext.Surveys.Add(_survey);
int count = surveyTranslations.Count; await surveyDbContext.SaveChangesAsync();
//var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
foreach (var title in survey.Titles) foreach (var title in survey.Titles)
{ {
count++; surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation {SurveyId = _survey.Id, Language = title.Language, Title = title.Title });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = count, SurveyId = Id, Language = title.Language, Title = title.Title });
} }
await surveyDbContext.SaveChangesAsync(); await surveyDbContext.SaveChangesAsync();
return (true, survey, "Successful"); survey.Id = _survey.Id;
return (true,survey, "Successful");
} }
else else
{ {