From 57a7e4619407c4ee55ffdaaf0c45d544b3b9760d Mon Sep 17 00:00:00 2001 From: Qais Yousuf Date: Sun, 24 Mar 2024 00:53:00 +0100 Subject: [PATCH] the deletion of questions in the edit mode --- Data/SurveyContext.cs | 6 +- .../Implemnetation/QuestionnaireRepository.cs | 22 +- .../Interaces/IQuestionnaireRepository.cs | 4 +- .../Controllers/QuestionnaireController.cs | 276 ++++++-------- .../Admin/Views/Questionnaire/Create.cshtml | 107 +++++- .../Admin/Views/Questionnaire/Edit.cshtml | 276 ++++++-------- ...1512_QuestionTypeEntityRemoved.Designer.cs | 355 ++++++++++++++++++ ...0240323171512_QuestionTypeEntityRemoved.cs | 34 ++ Web/Migrations/SurveyContextModelSnapshot.cs | 16 - Web/Program.cs | 13 +- Web/appsettings.json | 3 +- 11 files changed, 751 insertions(+), 361 deletions(-) create mode 100644 Web/Migrations/20240323171512_QuestionTypeEntityRemoved.Designer.cs create mode 100644 Web/Migrations/20240323171512_QuestionTypeEntityRemoved.cs diff --git a/Data/SurveyContext.cs b/Data/SurveyContext.cs index ce06d34..a616d98 100644 --- a/Data/SurveyContext.cs +++ b/Data/SurveyContext.cs @@ -14,6 +14,7 @@ namespace Data { } + public DbSet Pages { get; set; } public DbSet Banners { get; set; } @@ -31,7 +32,7 @@ namespace Data public DbSet Questions { get; set; } public DbSet Answers { get; set; } - public DbSet QuestionTypeEntities { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -68,9 +69,6 @@ namespace Data modelBuilder.Entity() .HasKey(a => a.Id); - modelBuilder.Entity() - - .HasKey(t => t.Id); base.OnModelCreating(modelBuilder); } diff --git a/Services/Implemnetation/QuestionnaireRepository.cs b/Services/Implemnetation/QuestionnaireRepository.cs index db51dcd..9dddbae 100644 --- a/Services/Implemnetation/QuestionnaireRepository.cs +++ b/Services/Implemnetation/QuestionnaireRepository.cs @@ -28,12 +28,12 @@ namespace Services.Implemnetation await _context.SaveChangesAsync(); } - public void Delete(int? id) - { - var questionnairId = GetQuesById(id); + //public void Delete(int? id) + //{ + // var questionnairId = GetQuesById(id); - _context.Questionnaires.Remove(questionnairId); - } + // _context.Questionnaires.Remove(questionnairId); + //} public List GetAllQuestions() { @@ -55,11 +55,21 @@ namespace Services.Implemnetation return _context.Questionnaires.AsNoTracking().Include(x => x.Questions).ThenInclude(x => x.Answers).FirstOrDefault(x=>x.Id==id); } - public void Update(Questionnaire questionnaire) + public async Task Update(Questionnaire questionnaire) { _context.Questionnaires.Update(questionnaire); + await _context.SaveChangesAsync(); + + } + + public async Task Delete(int? id) + { + var questionnairId = GetQuesById(id); + + _context.Questionnaires.Remove(questionnairId); + await _context.SaveChangesAsync(); } } } diff --git a/Services/Interaces/IQuestionnaireRepository.cs b/Services/Interaces/IQuestionnaireRepository.cs index 762c594..3f2ff29 100644 --- a/Services/Interaces/IQuestionnaireRepository.cs +++ b/Services/Interaces/IQuestionnaireRepository.cs @@ -15,8 +15,8 @@ namespace Services.Interaces Questionnaire GetQuestionnaireWithQuestionAndAnswer(int? id); void Add(Questionnaire questionnaire); - void Update(Questionnaire questionnaire); - void Delete(int? id); + Task Update(Questionnaire questionnaire); + Task Delete(int? id); Task commitAsync(); } diff --git a/Web/Areas/Admin/Controllers/QuestionnaireController.cs b/Web/Areas/Admin/Controllers/QuestionnaireController.cs index a6c08f8..8e94212 100644 --- a/Web/Areas/Admin/Controllers/QuestionnaireController.cs +++ b/Web/Areas/Admin/Controllers/QuestionnaireController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.IdentityModel.Tokens; using Model; using Services.Interaces; @@ -17,7 +18,7 @@ namespace Web.Areas.Admin.Controllers private readonly SurveyContext _context; private readonly IQuestionRepository _question; - public QuestionnaireController(IQuestionnaireRepository Questionnaire,SurveyContext Context, IQuestionRepository Question) + public QuestionnaireController(IQuestionnaireRepository Questionnaire, SurveyContext Context, IQuestionRepository Question) { _questionnaire = Questionnaire; _context = Context; @@ -30,7 +31,7 @@ namespace Web.Areas.Admin.Controllers var question = _question.GetQuestionsWithAnswers(); - + List viewmodel = new List(); @@ -43,22 +44,22 @@ namespace Web.Areas.Admin.Controllers Description = item.Description, Title = item.Title, Questions = item.Questions, - + }); } - + return View(viewmodel); } [HttpGet] public IActionResult Create() - - + + { - + var questionTypes = Enum.GetValues(typeof(QuestionType)).Cast(); - + ViewBag.QuestionTypes = new SelectList(questionTypes); var questionnaire = new QuestionnaireViewModel @@ -85,22 +86,22 @@ namespace Web.Areas.Admin.Controllers var questionnaire = new Questionnaire { - Id=viewmodel.Id, - Title=viewmodel.Title, - Description=viewmodel.Description, + Id = viewmodel.Id, + Title = viewmodel.Title, + Description = viewmodel.Description, }; - + var questions = viewmodel.Questions; foreach (var questionViewModel in viewmodel.Questions) { var question = new Question { - QuestionnaireId=questionViewModel.QuestionnaireId, + QuestionnaireId = questionViewModel.QuestionnaireId, Text = questionViewModel.Text, Type = questionViewModel.Type, - Answers = new List() + Answers = new List() }; foreach (var answerViewModel in questionViewModel.Answers) @@ -108,28 +109,28 @@ namespace Web.Areas.Admin.Controllers var answer = new Answer { Text = answerViewModel.Text, - QuestionId=answerViewModel.QuestionId, - + QuestionId = answerViewModel.QuestionId, + }; - + question.Answers.Add(answer); } - + questionnaire.Questions.Add(question); } - + _questionnaire.Add(questionnaire); - await _questionnaire.commitAsync(); + await _questionnaire.commitAsync(); TempData["Success"] = "Questionnaire created successfully"; - return RedirectToAction("Index"); + return RedirectToAction("Index"); } return View(viewmodel); } @@ -141,7 +142,7 @@ namespace Web.Areas.Admin.Controllers .Cast() .Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() }); ViewBag.QuestionTypes = questionTypes; - + var questionnaire = _questionnaire.GetQuestionnaireWithQuestionAndAnswer(id); if (questionnaire == null) @@ -154,27 +155,27 @@ namespace Web.Areas.Admin.Controllers 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, - QuestionnaireId=q.QuestionnaireId, - - + QuestionnaireId = q.QuestionnaireId, + + Answers = q.Answers.Select(a => new Answer { Id = a.Id, Text = a.Text, - Question=a.Question, - QuestionId=a.QuestionId + Question = a.Question, + QuestionId = a.QuestionId + + + - - - }).ToList() }).ToList() }; @@ -187,8 +188,8 @@ namespace Web.Areas.Admin.Controllers { var questionTypes = Enum.GetValues(typeof(QuestionType)) - .Cast() - .Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() }); + .Cast() + .Select(e => new SelectListItem { Value = e.ToString(), Text = e.ToString() }); ViewBag.QuestionTypes = questionTypes; if (ModelState.IsValid) @@ -205,6 +206,28 @@ namespace Web.Areas.Admin.Controllers existingQuestionnaire.Title = viewModel.Title; existingQuestionnaire.Description = viewModel.Description; + var existingQuestionIds = existingQuestionnaire.Questions.Select(q => q.Id).ToList(); + + // Iterate through existing questions and remove those not found in the view model + foreach (var existingQuestion in existingQuestionnaire.Questions.ToList()) + { + // If the ID of the existing question is not found in the view model, remove it + if (!viewModel.Questions.Any(q => q.Id == existingQuestion.Id)) + { + existingQuestionnaire.Questions.Remove(existingQuestion); + + } + await _questionnaire.Update(existingQuestionnaire); + } + + // Update the questionnaire with the modified list of questions + + + + + + + // Update or add new questions foreach (var questionViewModel in viewModel.Questions) { @@ -213,140 +236,49 @@ namespace Web.Areas.Admin.Controllers if (existingQuestion != null) { + var answersToRemove = new List(); existingQuestion.Text = questionViewModel.Text; existingQuestion.Type = questionViewModel.Type; - - - foreach (var answerViewModel in questionViewModel.Answers) + + foreach (var answerViewModel in questionViewModel.Answers) + { + // Check if the answer already exists + var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); + + if (answerViewModel.Id == 0) { - // Check if the answer already exists - var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); - - 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; - } - - } - - + existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); + + + } + else if (answerViewModel.Text == null) + { + existingQuestion.Answers.Remove(existingAnswer); + await _questionnaire.Update(existingQuestionnaire); + } + + + else if (existingAnswer != null) + { + + existingAnswer.Text = answerViewModel.Text; + } + + } + - } - //else - //{ - - // var newQuestion = new Question - // { - // Text = questionViewModel.Text, - // Type = questionViewModel.Type, - // Answers = questionViewModel.Answers?.Select(a => new Answer { Text = a.Text }).ToList() ?? new List() - // }; - // existingQuestionnaire.Questions.Add(newQuestion); - //} - - //if (existingQuestion != null) - //{ - // existingQuestion.Text = questionViewModel.Text; - // existingQuestion.Type = questionViewModel.Type; - - // //var answerId = existingQuestion.Answers.Select(x => x.Id).ToList(); - // // Update or add new answers - // //foreach (var answerViewModel in questionViewModel.Answers) - // //{ - // // var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); - - // // if (existingAnswer != null) - // // { - // // existingAnswer.Text = answerViewModel.Text; - // // } - // // else - // // { - // // // Handle adding new answers if necessary - // // existingQuestion.Answers.Add(new Answer { Text = answerViewModel.Text }); - // // } - // //} - // if (questionViewModel.Answers != null) - // { - // // Update or add new answers - // foreach (var answerViewModel in questionViewModel.Answers) - // { - // var existingAnswer = existingQuestion.Answers.FirstOrDefault(a => a.Id == answerViewModel.Id); - - // if (existingAnswer != null) - // { - // // Update existing answer - // existingAnswer.Text = answerViewModel.Text; - // } - // else - // { - // foreach (var newanswers in questionViewModel.Answers) - // { - // existingQuestion.Answers.Add(new Answer { Text = newanswers.Text }); - // } - // // Check if the answer with the same text already exists - // //var answerWithSameText = existingQuestion.Answers.FirstOrDefault(a => a.Text == answerViewModel.Text); - - // //if (answerWithSameText == null) - // //{ - // // // Add new answer only if it doesn't exist with the same text - - // //} - // //else - // //{ - // // // Optionally handle the case where an answer with the same text already exists - // // // You can choose to do nothing, show a message, or take any other action - // //} - // } - // } - // } - - //} - //else - //{ - // // Add new question with its answers - // var newQuestion = new Question - // { - // Text = questionViewModel.Text, - // Type = questionViewModel.Type, - // Answers = questionViewModel.Answers.Select(a => new Answer { Text = a.Text }).ToList() - // }; - - // existingQuestionnaire.Questions.Add(newQuestion); - //} } - // 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); - //} + + await _questionnaire.Update(existingQuestionnaire); - // Save changes to the database - _questionnaire.Update(existingQuestionnaire); - await _questionnaire.commitAsync(); TempData["Success"] = "Questionnaire updated successfully"; - return RedirectToAction("Index"); + return RedirectToAction(nameof(Index)); } // If ModelState is not valid, re-display the form with validation errors @@ -391,13 +323,35 @@ namespace Web.Areas.Admin.Controllers [HttpPost] [ActionName("Delete")] - public IActionResult DeleteConfirm(int id) + public async Task DeleteConfirm(int id) { - _questionnaire.Delete(id); - _questionnaire.commitAsync(); - - return Json(new { success = true, message = "Item deleted successfully" }); - + + + + try + { + var deletedQuestionnaire = _questionnaire.Delete(id); + + + if (deletedQuestionnaire == null) + { + return NotFound(); // Or handle not found case appropriately + } + + // If deletion is successful, you can redirect to a success page or return a success message + return Json(new { success = true, message = "Item deleted successfully" }); + } + catch (Exception ex) + { + // Log the exception or handle it appropriately + return StatusCode(500, "An error occurred while processing your request."); + } + + + + //return StatusCode(500, "An error occurred while processing your request"); + + } [HttpGet] diff --git a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml index 5a2678b..b17d900 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Create.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Create.cshtml @@ -57,9 +57,10 @@ } + | @@ -83,8 +84,106 @@ } - - + + @* - + *@ } \ No newline at end of file diff --git a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml index fc697c2..511e09d 100644 --- a/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml +++ b/Web/Areas/Admin/Views/Questionnaire/Edit.cshtml @@ -4,7 +4,7 @@ ViewData["Title"] = "Edit"; } - + *@ +
+
+
+
Edit Survey
+ +
+
+ +
+ + + +
+
+ + + +
+
+
+ @for (int i = 0; i < Model.Questions.Count; i++) + { +
+
+ +
+
+
+ +
+

NEXT QUESTION

+
+ +
+
+
+
+ +
+ @Model.Questions[i].Text + | + +
+ + + +
+ @Model.Questions[i].Text + +
+
+ } +
+
+
+ + | Back to list +
+
+
+
+
+
+ + + + + +@*
Edit Survey
@@ -68,6 +163,7 @@
@Model.Questions[i].Text +
- + +
}
@@ -110,79 +207,6 @@
- - - - - -@*
-
-
-
Edit Survey
- -
-
- -
- - - -
-
- - - -
-
-
- @for (int i = 0; i < Model.Questions.Count; i++) - { -
- -
- - - -
-
- - - -
-
- @for (int j = 0; j < Model.Questions[i].Answers.Count; j++) - { -
- -
- - - - -
- -
- } -
- - -
- - - } - -
-
- - - - - | Back to list -
- -
-
-
*@ @@ -193,6 +217,9 @@ + + + @section Scripts { - - @* *@ + @* // $(document).on('click', '.removeQuestion', function () { + // $(this).closest('.question').remove(); + }); *@ } diff --git a/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.Designer.cs b/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.Designer.cs new file mode 100644 index 0000000..c7582c2 --- /dev/null +++ b/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.Designer.cs @@ -0,0 +1,355 @@ +// +using System; +using Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Web.Migrations +{ + [DbContext(typeof(SurveyContext))] + [Migration("20240323171512_QuestionTypeEntityRemoved")] + partial class QuestionTypeEntityRemoved + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Model.Address", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CVR") + .HasColumnType("nvarchar(max)"); + + b.Property("City") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Country") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Mobile") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PostalCode") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("State") + .HasColumnType("nvarchar(max)"); + + b.Property("Street") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Addresss"); + }); + + modelBuilder.Entity("Model.Answer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("QuestionId") + .HasColumnType("int"); + + b.Property("Text") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("QuestionId"); + + b.ToTable("Answers"); + }); + + modelBuilder.Entity("Model.Banner", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUrl") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LinkUrl") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Banners"); + }); + + modelBuilder.Entity("Model.Footer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ImageUlr") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastUpdated") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Owner") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Sitecopyright") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdatedBy") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Footers"); + }); + + modelBuilder.Entity("Model.FooterSocialMedia", b => + { + b.Property("FooterId") + .HasColumnType("int"); + + b.Property("SocialId") + .HasColumnType("int"); + + b.HasKey("FooterId", "SocialId"); + + b.HasIndex("SocialId"); + + b.ToTable("FooterSocialMedias"); + }); + + modelBuilder.Entity("Model.Page", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BannerId") + .HasColumnType("int"); + + b.Property("Content") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Slug") + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("BannerId"); + + b.ToTable("Pages"); + }); + + modelBuilder.Entity("Model.Question", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("QuestionnaireId") + .HasColumnType("int"); + + b.Property("Text") + .HasColumnType("nvarchar(max)"); + + b.Property("Type") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("QuestionnaireId"); + + b.ToTable("Questions"); + }); + + modelBuilder.Entity("Model.Questionnaire", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Description") + .HasColumnType("nvarchar(max)"); + + b.Property("Title") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Questionnaires"); + }); + + modelBuilder.Entity("Model.SocialMedia", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Url") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("SocialMedia"); + }); + + modelBuilder.Entity("Model.Answer", b => + { + b.HasOne("Model.Question", "Question") + .WithMany("Answers") + .HasForeignKey("QuestionId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Question"); + }); + + modelBuilder.Entity("Model.FooterSocialMedia", b => + { + b.HasOne("Model.Footer", "Footer") + .WithMany("FooterSocialMedias") + .HasForeignKey("FooterId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Model.SocialMedia", "SocialMedia") + .WithMany("FooterSocialMedias") + .HasForeignKey("SocialId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Footer"); + + b.Navigation("SocialMedia"); + }); + + modelBuilder.Entity("Model.Page", b => + { + b.HasOne("Model.Banner", "banner") + .WithMany() + .HasForeignKey("BannerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("banner"); + }); + + modelBuilder.Entity("Model.Question", b => + { + b.HasOne("Model.Questionnaire", "Questionnaire") + .WithMany("Questions") + .HasForeignKey("QuestionnaireId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Questionnaire"); + }); + + modelBuilder.Entity("Model.Footer", b => + { + b.Navigation("FooterSocialMedias"); + }); + + modelBuilder.Entity("Model.Question", b => + { + b.Navigation("Answers"); + }); + + modelBuilder.Entity("Model.Questionnaire", b => + { + b.Navigation("Questions"); + }); + + modelBuilder.Entity("Model.SocialMedia", b => + { + b.Navigation("FooterSocialMedias"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.cs b/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.cs new file mode 100644 index 0000000..ede3426 --- /dev/null +++ b/Web/Migrations/20240323171512_QuestionTypeEntityRemoved.cs @@ -0,0 +1,34 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Web.Migrations +{ + /// + public partial class QuestionTypeEntityRemoved : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "QuestionTypeEntities"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "QuestionTypeEntities", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Type = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_QuestionTypeEntities", x => x.Id); + }); + } + } +} diff --git a/Web/Migrations/SurveyContextModelSnapshot.cs b/Web/Migrations/SurveyContextModelSnapshot.cs index 5fa25d5..186ba09 100644 --- a/Web/Migrations/SurveyContextModelSnapshot.cs +++ b/Web/Migrations/SurveyContextModelSnapshot.cs @@ -235,22 +235,6 @@ namespace Web.Migrations b.ToTable("Questions"); }); - modelBuilder.Entity("Model.QuestionTypeEntities", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Type") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.ToTable("QuestionTypeEntities"); - }); - modelBuilder.Entity("Model.Questionnaire", b => { b.Property("Id") diff --git a/Web/Program.cs b/Web/Program.cs index 7d42286..6605168 100644 --- a/Web/Program.cs +++ b/Web/Program.cs @@ -1,5 +1,6 @@ using Data; using Microsoft.AspNetCore.Mvc.Razor; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Services.Implemnetation; using Services.Interaces; @@ -11,7 +12,15 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); -builder.Services.ConfigureSQLConnection(builder.Configuration); +var config = builder.Configuration; + +builder.Services.AddDbContext(options => +{ + options.UseSqlServer(config.GetConnectionString("SurveyVista"), cfg => cfg.MigrationsAssembly("Web")); +}); + + +//builder.Services.ConfigureSQLConnection(builder.Configuration); builder.Services.ConfigurePageServices(); builder.Services.ConfigureBannerServices(); @@ -20,7 +29,7 @@ builder.Services.ConfigureSocialMedia(); builder.Services.ConfigureFooter(); builder.Services.ConfigureQuestionnarie(); builder.Services.ConfigureQuestion(); -//builder.Services.AddScoped(); +builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Web/appsettings.json b/Web/appsettings.json index 4eb9c27..50a92a0 100644 --- a/Web/appsettings.json +++ b/Web/appsettings.json @@ -6,7 +6,8 @@ } }, "AllowedHosts": "*", + "ConnectionStrings": { - "SurveyVista": "data source=SEO-PC; initial catalog=SurveyVista; integrated security=true; TrustServerCertificate=true;" + "SurveyVista": "data source=SEO-PC; initial catalog=SurveyVista;integrated security=True; TrustServerCertificate=True;" } }