From 992afeab5eda03637ce4b6e1e76545902cbf2ae3 Mon Sep 17 00:00:00 2001
From: Vijay Uppu <913468@dadeschools.net>
Date: Tue, 12 Dec 2023 13:40:27 -0500
Subject: [PATCH] multiple questions updated based on survey id
---
.../Controllers/QuestionsController.cs | 21 ++++++++++++
.../Interfaces/IQuestionsProvider.cs | 1 +
.../Providers/QuestionsProvider.cs | 33 +++++++++++++++++++
3 files changed, 55 insertions(+)
diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs
index 4acb1ed..bc5abe4 100644
--- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs
+++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs
@@ -1,5 +1,6 @@
using DamageAssesment.Api.Questions.Interfaces;
using Microsoft.AspNetCore.Authorization;
+using DamageAssesment.Api.Questions.Models;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Questions.Controllers
@@ -111,6 +112,26 @@ namespace DamageAssesment.Api.Questions.Controllers
return CreatedAtRoute("DefaultApi",questions);
}
///
+ /// PUT request for update a multiple question (multilingual) for survey.
+ ///
+ [HttpPut("questions/multiple/{surveyid}")]
+ public async Task CreateQuestions(int surveyid, List questions)
+ {
+ if (questions != null)
+ {
+ var result = await this.questionsProvider.PutQuestionsAsync(surveyid,questions);
+ if (result.IsSuccess)
+ {
+ return Ok(result.Question);
+ }
+ if (result.ErrorMessage == "Not Found")
+ return NotFound(result.ErrorMessage);
+
+ return BadRequest(result.ErrorMessage);
+ }
+ return CreatedAtRoute("DefaultApi", questions);
+ }
+ ///
/// POST request for creating a new question (multilingual).
///
diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs
index 4fcea32..086de87 100644
--- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs
+++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs
@@ -9,6 +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, 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 0ce443d..9cf2980 100644
--- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs
+++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs
@@ -376,6 +376,36 @@ 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
+ {
+ 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)
+ {
+ logger?.LogError(ex.ToString());
+ return (false, null, ex.Message);
+ }
+ }
public async Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question)
{
try
@@ -409,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");