diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs index dbd4170..672e705 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs @@ -109,12 +109,11 @@ namespace DamageAssesment.Api.Questions.Controllers } return CreatedAtRoute("DefaultApi",questions); } - /// - /// POST request for creating a multiple question (multilingual). + /// PUT request for update a multiple question (multilingual) for survey. /// [HttpPut("questions/multiple/{surveyid}")] - public async Task UpdateQuestions(int surveyid, List questions) + public async Task CreateQuestions(int surveyid, List questions) { if (questions != null) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs index eb2be52..086de87 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs @@ -9,7 +9,7 @@ namespace DamageAssesment.Api.Questions.Interfaces Task<(bool IsSuccess, List SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question); Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PostQuestionsAsync(List Questions); - Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PutQuestionsAsync(int surveyId, List Questions); + Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PutQuestionsAsync(int surveyId,List Questions); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question); Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> DeleteQuestionAsync(int id); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs index b0b002a..4c2de16 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs @@ -376,15 +376,29 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - + public async Task<(bool IsSuccess, IEnumerable Question, string ErrorMessage)> PutQuestionsAsync(int surveyId, List Questions) { try { - questionDbContext.Questions.ToList().RemoveAll(s=> s.SurveyId == surveyId); - questionDbContext.SaveChanges(); - var response = await PostQuestionsAsync(Questions); - return (response); + var questions=await questionDbContext.Questions.AsNoTracking().Where(a=>a.SurveyId == surveyId).ToListAsync(); + if (questions != null) + { + List questionids=questions.Select(a=>a.Id).ToList(); + var questiontrans = await questionDbContext.QuestionsTranslations.AsNoTracking().Where(x => questionids.Contains(x.QuestionId)).ToListAsync(); + if (questiontrans != null) + questionDbContext.QuestionsTranslations.RemoveRange(questiontrans); + questionDbContext.Questions.RemoveRange(questions); + questionDbContext.SaveChanges(); + } + List results = new List(); + logger?.LogInformation("Query Question"); + foreach (Models.Question Question in Questions) + { + Question.SurveyId = surveyId; + results.Add(InsertQuestion(Question)); + } + return (true, results, null); } catch (Exception ex) { @@ -425,8 +439,11 @@ namespace DamageAssesment.Api.Questions.Providers if (question != null) { + var questiontrans=await questionDbContext.QuestionsTranslations.AsNoTracking().Where(x=>x.QuestionId== id).ToListAsync(); var result = mapper.Map(question); result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, "")); + if(questiontrans!=null) + questionDbContext.QuestionsTranslations.RemoveRange(questiontrans); questionDbContext.Questions.Remove(question); questionDbContext.SaveChanges(); return (true, result, $"QuestionID {id} deleted Successfuly");