diff --git a/Data/SurveyContext.cs b/Data/SurveyContext.cs index 7e1d741..ce06d34 100644 --- a/Data/SurveyContext.cs +++ b/Data/SurveyContext.cs @@ -69,6 +69,7 @@ namespace Data .HasKey(a => a.Id); modelBuilder.Entity() + .HasKey(t => t.Id); base.OnModelCreating(modelBuilder); diff --git a/Model/Answer.cs b/Model/Answer.cs index 8e6783b..5af1e15 100644 --- a/Model/Answer.cs +++ b/Model/Answer.cs @@ -10,6 +10,7 @@ namespace Model public class Answer { public int Id { get; set; } + public string? Text { get; set; } public int QuestionId { get; set; } // Foreign key for Question [ForeignKey("QuestionId")] diff --git a/Services/Implemnetation/QuestionnaireRepository.cs b/Services/Implemnetation/QuestionnaireRepository.cs index e939124..db51dcd 100644 --- a/Services/Implemnetation/QuestionnaireRepository.cs +++ b/Services/Implemnetation/QuestionnaireRepository.cs @@ -57,6 +57,7 @@ namespace Services.Implemnetation public void Update(Questionnaire questionnaire) { + _context.Questionnaires.Update(questionnaire); } diff --git a/Web/Areas/Admin/Controllers/QuestionnaireController.cs b/Web/Areas/Admin/Controllers/QuestionnaireController.cs index 62be761..aafc7a7 100644 --- a/Web/Areas/Admin/Controllers/QuestionnaireController.cs +++ b/Web/Areas/Admin/Controllers/QuestionnaireController.cs @@ -66,8 +66,6 @@ namespace Web.Areas.Admin.Controllers Questions = new List(), Answers = new List() - - }; @@ -156,6 +154,7 @@ namespace Web.Areas.Admin.Controllers Id = questionnaire.Id, Title = questionnaire.Title, Description = questionnaire.Description, + Questions = questionnaire.Questions .Select(q => new Question @@ -163,13 +162,18 @@ namespace Web.Areas.Admin.Controllers Id = q.Id, Text = q.Text, Type = q.Type, + QuestionnaireId=q.QuestionnaireId, Answers = q.Answers.Select(a => new Answer { Id = a.Id, Text = a.Text, - Question=a.Question + Question=a.Question, + QuestionId=a.QuestionId + + + }).ToList() }).ToList() @@ -204,7 +208,7 @@ namespace Web.Areas.Admin.Controllers // Update or add new questions foreach (var questionViewModel in viewModel.Questions) { - var existingQuestion = existingQuestionnaire.Questions.SingleOrDefault(q => q.Id == questionViewModel.Id); + var existingQuestion = existingQuestionnaire.Questions.FirstOrDefault(q => q.Id == questionViewModel.Id); if (existingQuestion != null) @@ -212,11 +216,8 @@ namespace Web.Areas.Admin.Controllers existingQuestion.Text = questionViewModel.Text; existingQuestion.Type = questionViewModel.Type; - bool newAnswersAdded = false; // Flag to track if new answers were added - - // Check if the question has any answers + - // Loop through each answer in the view model foreach (var answerViewModel in questionViewModel.Answers) { // Check if the answer already exists @@ -225,37 +226,33 @@ namespace Web.Areas.Admin.Controllers if (answerViewModel.Id==0) { - existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); - - - + } + else if (string.IsNullOrEmpty(answerViewModel.Text)) + { + + existingQuestion.Answers.Remove(existingAnswer); + _context.SaveChanges(); + + } else if(existingAnswer !=null) { existingAnswer.Text = answerViewModel.Text; } - } - - //// Add newly answers that exist only in the view model - //foreach (var answerViewModel in questionViewModel.Answers.Where(av => existingQuestion.Answers.All(ea => ea.Id != av.Id))) - //{ - // existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); - // newAnswersAdded = true; // Set flag to true - //} + } - // If new answers were added, remove any null references - if (newAnswersAdded) - { - existingQuestion.Answers.RemoveAll(a => a == null); - } + + + + } else { - // Add new question with its answers + var newQuestion = new Question { Text = questionViewModel.Text, @@ -338,18 +335,17 @@ namespace Web.Areas.Admin.Controllers } // Remove any questions that are not in the view model - var questionIdsInViewModel = viewModel.Questions.Select(q => q.Id); - var questionsToRemove = existingQuestionnaire.Questions.Where(q => !questionIdsInViewModel.Contains(q.Id)).ToList(); - foreach (var questionToRemove in questionsToRemove) - { - existingQuestionnaire.Questions.Remove(questionToRemove); - } + //var questionIdsInViewModel = viewModel.Questions.Select(q => q.Id); + //var questionsToRemove = existingQuestionnaire.Questions.Where(q => !questionIdsInViewModel.Contains(q.Id)).ToList(); + //foreach (var questionToRemove in questionsToRemove) + //{ + // existingQuestionnaire.Questions.Remove(questionToRemove); + //} // Save changes to the database _questionnaire.Update(existingQuestionnaire); - TempData["Success"] = "Questionnaire updated successfully"; await _questionnaire.commitAsync(); - + TempData["Success"] = "Questionnaire updated successfully"; return RedirectToAction("Index"); } @@ -404,5 +400,40 @@ namespace Web.Areas.Admin.Controllers } + [HttpGet] + public IActionResult Details(int id) + { + var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast(); + + ViewBag.QuestionTypes = new SelectList(questionTypes); + var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id); + + if (questionnaire == null) + { + return NotFound(); // Or handle not found case appropriately + } + + var viewModel = new QuestionnaireViewModel + { + Id = questionnaire.Id, + Title = questionnaire.Title, + Description = questionnaire.Description, + Questions = questionnaire.Questions + .Select(q => new Question + { + Id = q.Id, + Text = q.Text, + Type = q.Type, + Answers = q.Answers.Select(a => new Answer + { + Id = a.Id, + Text = a.Text + }).ToList() + }).ToList() + }; + + return View(viewModel); + } + } } diff --git a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml index a1fa742..ec80f65 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml @@ -26,12 +26,15 @@ -
+
-
+
-

Create Questions

-
+

Create Questions

+ +
+ + @for (int i = 0; i < Model.Questions?.Count; i++) {
@@ -49,28 +52,32 @@ {
- +
}
+ +
+ }
- -
-
- - | Back to list -
+ + + +
+ + - -
@@ -116,43 +123,55 @@ // Add answers input fields - newQuestionHtml += `
`; + newQuestionHtml += `
`; newQuestionHtml += `

`; newQuestionHtml += ``; - newQuestionHtml += `

`; + newQuestionHtml += `
`; newQuestionHtml += ``; - newQuestionHtml += ``; + newQuestionHtml += `
`; + newQuestionHtml += ``; - newQuestionHtml += `

`; + newQuestionHtml += `
` newQuestionHtml += `
`; newQuestionHtml += `
`; newQuestionHtml += `
`; // Add Remove Question button newQuestionHtml += ``; - newQuestionHtml += `
` + newQuestionHtml += `
` newQuestionHtml += `
`; - + $("#questions-container .form-group").append(newQuestionHtml); questionIndex++; }); - - // Add answer dynamically without "Add Answer" button $("#questions-container").on("click", ".add-answer", function () { var questionIndex = $(this).closest('.answers-container').data('question-index'); var answerIndex = $(this).closest('.answers-container').find('.answer-group').length; - var answerGroup = $(this).closest('.answers-container').find('.answer-group:first').clone(); - answerGroup.find('input').val(''); // Clear input values - answerGroup.find('.add-answer').hide(); // Hide the "Add Answer" button - answerGroup.find('.remove-answer').show(); // Show the "Remove Answer" button + var answerGroupHtml = ` +
+
+ + + +
+ +
`; - answerGroup.data('answer-index', answerIndex); - answerGroup.find('label').text(`Answer ${answerIndex + 1}`); - answerGroup.find('input').attr('name', `Questions[${questionIndex}].Answers[${answerIndex}].Text`); - $(this).closest('.answers-container').append(answerGroup); + // Append the new answer group before the "Add Answer" button + $(this).before(answerGroupHtml); + + // Show the "Remove Answer" button only for the newly added answer group + $(this).prev('.answer-group').find('.remove-answer').show(); }); + + + + + + + // Remove answer dynamically $("#questions-container").on("click", ".remove-answer", function () { $(this).closest('.answer-group').remove(); diff --git a/Web/Areas/Admin/Views/Questionnaire/Details.cshtml b/Web/Areas/Admin/Views/Questionnaire/Details.cshtml new file mode 100644 index 0000000..5e93647 --- /dev/null +++ b/Web/Areas/Admin/Views/Questionnaire/Details.cshtml @@ -0,0 +1,103 @@ +@model QuestionnaireViewModel + +@{ + ViewData["Title"] = "Details"; +} + + + +
+ + + +
+
+ Details +
+ +
+
+ +
Details of questionnaire @Model.Title
+ + +
+ + + + + + + + + + + + + + + + + + + + + + +
IdQuestionnaireDescriptionQuestions
@Model.Id@Model.Title@Html.Raw(@Model.Description) + + + + + + + + + @foreach (var question in Model.Questions) + { + + + + + + + } +
IdQuestionQuestion TypeAnswers
@question.Id + @question.Text + + @question.Type + + + + + + + @foreach (var answer in question.Answers) + { + + + + + + + } +
IdAnswer
+ @answer.Id + + @answer.Text +
+
+ + +
+ +
+ +
+ + +
+ diff --git a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml index 2f68a27..85ef9bc 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml @@ -10,58 +10,62 @@
Edit Survey
-
- -
- - - -
-
- - - -
- -
- @for (int i = 0; i < Model.Questions.Count; i++) -{ -
- -
- - - -
-
- - - -
-
- @for (int j = 0; j < Model.Questions[i].Answers.Count; j++) - { -
- + +
- - - + + +
- -
- } -
- - -
-} +
+ + + +
+
+
+ @for (int i = 0; i < Model.Questions.Count; i++) + { +
+ +
+ + + +
+
+ + + +
+
+ @for (int j = 0; j < Model.Questions[i].Answers.Count; j++) + { +
+ +
+ + + + +
-
+
+ } +
+ - - - +
+ } + +
+
+ + + + + | Back to list +
@@ -72,36 +76,48 @@ @section Scripts { + + @{ + + } - // Correctly find the length of answers for the specific question - var newAnswerIndex = questionContainer.find('.answers .form-group').length; + + + + + + @* this script is working for adding mutiple answers for a question *@ - // Function to remove an answer - $(document).on('click', '.removeAnswer', function () { - $(this).closest('.form-group').remove(); - }); - }); - - @* this script is working for adding mutiple answers for a question *@ - - @* + *@ } - + diff --git a/Web/Areas/Admin/Views/Questionnaire/Index.cshtml b/Web/Areas/Admin/Views/Questionnaire/Index.cshtml index 8dfbdef..2506143 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Index.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Index.cshtml @@ -3,7 +3,7 @@ @{ ViewData["Title"] = "Questionnaire"; } -
+
@@ -14,14 +14,14 @@

Create New

- +
- + @@ -63,10 +63,10 @@ - } diff --git a/Web/Web.csproj b/Web/Web.csproj index 6c9edbc..b74379c 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -4,6 +4,7 @@ net8.0enableenable + Exe @@ -16,7 +17,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - +
Id Title DescriptionTotal QuestionTotal Questions Questions | Type | Answers Action Delete | - Edit + Edit| + Details