diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs index f7f2268..6249712 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs @@ -108,7 +108,7 @@ namespace DamageAssesment.Api.Answers.Providers 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 { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Models/Employee.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Models/Employee.cs index 37afe32..5defc90 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Models/Employee.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Models/Employee.cs @@ -4,18 +4,10 @@ namespace DamageAssesment.Api.Employees.Models { public class Employee { - [Key] public string Id { get; set; } - - [StringLength(50)] public string Name { get; set; } - public DateTime BirthDate { get; set; } - - [StringLength(50)] public string OfficePhoneNumber { get; set; } - - [StringLength(50)] public string Email { get; set; } public bool IsActive { get; set; } public string? PreferredLanguage { get; set; } = "en"; diff --git a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs index 48a6a1a..2615cfd 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyResponsesProvider.cs @@ -709,16 +709,16 @@ namespace DamageAssesment.Api.SurveyResponses.Providers } - async Task ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId) + bool ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId) { 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) { List listAnswerInfo = new List(); 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"; logger?.LogInformation(message); @@ -757,18 +757,19 @@ namespace DamageAssesment.Api.SurveyResponses.Providers //var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID)); foreach (QuestionRequest ans in answers.Answers) { + ProcessAnswers(ans, surveyResponse.Id); //var stopwatch = new Stopwatch(); //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); - answerTasks.Add(task); + //answerTasks.Add(task); //stopwatch.Stop(); //answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id)); } - await Task.WhenAll(answerTasks); + //await Task.WhenAll(answerTasks); return (true, surveyResponse, null); } else diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs index e912e96..95c73b0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Db/SurveysDbContext.cs @@ -2,13 +2,25 @@ namespace DamageAssesment.Api.Surveys.Db { - public class SurveysDbContext:DbContext + public class SurveysDbContext : DbContext { + public DbSet Surveys { get; set; } + public DbSet SurveysTranslation { get; set; } public SurveysDbContext(DbContextOptions options) : base(options) { } - public DbSet Surveys { get; set; } - public DbSet SurveysTranslation { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); + } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index abec85a..6070d78 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -60,7 +60,6 @@ namespace DamageAssesment.Api.Surveys.Providers if (surveys != null) { - if (string.IsNullOrEmpty(Language)) { surveysList = from s in surveys @@ -184,17 +183,20 @@ namespace DamageAssesment.Api.Surveys.Providers { var surveys = await surveyDbContext.Surveys.ToListAsync(); - int Id = surveys.Count + 1; - 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(); - int count = surveyTranslations.Count; + Db.Survey _survey = new Db.Survey { IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now }; + + surveyDbContext.Surveys.Add(_survey); + await surveyDbContext.SaveChangesAsync(); + + //var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync(); + foreach (var title in survey.Titles) { - count++; - surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = count, SurveyId = Id, Language = title.Language, Title = title.Title }); + surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation {SurveyId = _survey.Id, Language = title.Language, Title = title.Title }); } await surveyDbContext.SaveChangesAsync(); - return (true, survey, "Successful"); + survey.Id = _survey.Id; + return (true,survey, "Successful"); } else {