multiple questions updated based on survey id
This commit is contained in:
parent
3f41cf6e10
commit
992afeab5e
@ -1,5 +1,6 @@
|
|||||||
using DamageAssesment.Api.Questions.Interfaces;
|
using DamageAssesment.Api.Questions.Interfaces;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using DamageAssesment.Api.Questions.Models;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DamageAssesment.Api.Questions.Controllers
|
namespace DamageAssesment.Api.Questions.Controllers
|
||||||
@ -111,6 +112,26 @@ namespace DamageAssesment.Api.Questions.Controllers
|
|||||||
return CreatedAtRoute("DefaultApi",questions);
|
return CreatedAtRoute("DefaultApi",questions);
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// PUT request for update a multiple question (multilingual) for survey.
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("questions/multiple/{surveyid}")]
|
||||||
|
public async Task<IActionResult> CreateQuestions(int surveyid, List<Models.Question> 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);
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
/// POST request for creating a new question (multilingual).
|
/// POST request for creating a new question (multilingual).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ namespace DamageAssesment.Api.Questions.Interfaces
|
|||||||
Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language);
|
Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language);
|
||||||
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question);
|
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question);
|
||||||
Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Question, string ErrorMessage)> PostQuestionsAsync(List<Models.Question> Questions);
|
Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Question, string ErrorMessage)> PostQuestionsAsync(List<Models.Question> Questions);
|
||||||
|
Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Question, string ErrorMessage)> PutQuestionsAsync(int surveyId,List<Models.Question> Questions);
|
||||||
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question);
|
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question);
|
||||||
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> DeleteQuestionAsync(int id);
|
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> DeleteQuestionAsync(int id);
|
||||||
|
|
||||||
|
@ -376,6 +376,36 @@ namespace DamageAssesment.Api.Questions.Providers
|
|||||||
return (false, null, ex.Message);
|
return (false, null, ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Question, string ErrorMessage)> PutQuestionsAsync(int surveyId, List<Models.Question> Questions)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var questions=await questionDbContext.Questions.AsNoTracking().Where(a=>a.SurveyId == surveyId).ToListAsync();
|
||||||
|
if (questions != null)
|
||||||
|
{
|
||||||
|
List<int> 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<Models.MultiLanguage> results = new List<MultiLanguage>();
|
||||||
|
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)
|
public async Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -409,8 +439,11 @@ namespace DamageAssesment.Api.Questions.Providers
|
|||||||
|
|
||||||
if (question != null)
|
if (question != null)
|
||||||
{
|
{
|
||||||
|
var questiontrans=await questionDbContext.QuestionsTranslations.AsNoTracking().Where(x=>x.QuestionId== id).ToListAsync();
|
||||||
var result = mapper.Map<Db.Question, Models.MultiLanguage>(question);
|
var result = mapper.Map<Db.Question, Models.MultiLanguage>(question);
|
||||||
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
|
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
|
||||||
|
if(questiontrans!=null)
|
||||||
|
questionDbContext.QuestionsTranslations.RemoveRange(questiontrans);
|
||||||
questionDbContext.Questions.Remove(question);
|
questionDbContext.Questions.Remove(question);
|
||||||
questionDbContext.SaveChanges();
|
questionDbContext.SaveChanges();
|
||||||
return (true, result, $"QuestionID {id} deleted Successfuly");
|
return (true, result, $"QuestionID {id} deleted Successfuly");
|
||||||
|
Loading…
Reference in New Issue
Block a user