From 2e148fe4f28fb9164bf30bee1aac11e38e06b324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20=C5=A0inik?= Date: Sun, 16 Jun 2024 17:00:56 +0200 Subject: [PATCH] fix: Add foreign key constraints and fix bugs in view --- LangLang/Repositories/DatabaseContext.cs | 54 +++++++++++++++++++ .../SqlRepositories/ExamSlotRepository.cs | 6 +++ .../ExamViewModels/ExamSlotCreateVM.cs | 42 +++++++++++---- .../ExamViewModels/ExamSlotUpdateVM.cs | 22 ++++++-- .../StudentView/Tabs/AvailableCourses.xaml.cs | 2 +- .../ExamSlotView/ExamSlotCreateWindow.xaml | 2 +- .../ExamSlotView/ExamSlotUpdateWindow.xaml | 2 +- .../ExamSlotView/ExamSlotUpdateWindow.xaml.cs | 12 +---- .../Views/TutorView/Tabs/ExamsReview.xaml.cs | 2 +- 9 files changed, 114 insertions(+), 30 deletions(-) diff --git a/LangLang/Repositories/DatabaseContext.cs b/LangLang/Repositories/DatabaseContext.cs index a9d9272..fdbe3d5 100644 --- a/LangLang/Repositories/DatabaseContext.cs +++ b/LangLang/Repositories/DatabaseContext.cs @@ -21,6 +21,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) base.OnModelCreating(modelBuilder); ConfigureTutorEntity(modelBuilder); ConfigureTutorSkillEntity(modelBuilder); + ConfigureCourseEntity(modelBuilder); + ConfigureCourseTimeSlotEntity(modelBuilder); modelBuilder.Entity().ToTable("Tutor"); modelBuilder.Entity().ToTable("Course"); @@ -65,5 +67,57 @@ private void ConfigureTutorSkillEntity(ModelBuilder modelBuilder) .HasForeignKey(ts => ts.LanguageLevelId) .OnDelete(DeleteBehavior.Cascade); } + + private void ConfigureCourseEntity(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasKey(c => c.Id); + + modelBuilder.Entity() + .Property(c => c.TutorId) + .HasColumnName("TutorId"); + + modelBuilder.Entity() + .Property(c => c.LanguageLevelId) + .HasColumnName("LanguageLevelId"); + + modelBuilder.Entity() + .HasOne() + .WithMany() + .HasForeignKey(c => c.TutorId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne() + .WithMany() + .HasForeignKey(c => c.LanguageLevelId) + .OnDelete(DeleteBehavior.Cascade); + } + + private void ConfigureCourseTimeSlotEntity(ModelBuilder modelBuilder) + { + modelBuilder.Entity() + .HasKey(cts => cts.Id); + + modelBuilder.Entity() + .Property(cts => cts.CourseId) + .HasColumnName("CourseId"); + + modelBuilder.Entity() + .Property(cts => cts.TimeSlotId) + .HasColumnName("TimeSlotId"); + + modelBuilder.Entity() + .HasOne() + .WithMany() + .HasForeignKey(cts => cts.CourseId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne() + .WithMany() + .HasForeignKey(cts => cts.TimeSlotId) + .OnDelete(DeleteBehavior.Cascade); + } } } \ No newline at end of file diff --git a/LangLang/Repositories/SqlRepositories/ExamSlotRepository.cs b/LangLang/Repositories/SqlRepositories/ExamSlotRepository.cs index 4ff4cb8..2f34fae 100644 --- a/LangLang/Repositories/SqlRepositories/ExamSlotRepository.cs +++ b/LangLang/Repositories/SqlRepositories/ExamSlotRepository.cs @@ -1,5 +1,6 @@ using LangLang.Domain.Models; using LangLang.Domain.RepositoryInterfaces; +using Microsoft.EntityFrameworkCore; using System.Collections.Generic; using System.Linq; @@ -37,6 +38,11 @@ public void Add(ExamSlot exam) public void Update(ExamSlot exam) { + var existingExam = _context.ExamSlot.Find(exam.Id); + if (existingExam != null) + { + _context.Entry(existingExam).State = EntityState.Detached; + } _context.ExamSlot.Update(exam); _context.SaveChanges(); } diff --git a/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotCreateVM.cs b/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotCreateVM.cs index 0d64e40..5619c3c 100644 --- a/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotCreateVM.cs +++ b/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotCreateVM.cs @@ -4,34 +4,49 @@ using LangLang.WPF.ViewModels.ExamViewModel; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Windows; namespace LangLang.WPF.ViewModels.ExamViewModels { public class ExamSlotCreateVM { - public List Skills { get; set; } + public ObservableCollection Courses { get; set; } + private List courses { get; set; } public CourseViewModel SelectedCourse { get; set; } public ExamSlotViewModel ExamSlot { get; set; } + private Tutor loggedId { get; set; } + private CourseService courseService { get; set; } public ExamSlotCreateVM(Tutor loggedIn) { - CourseService courseService = new(); ExamSlot = new ExamSlotViewModel(); - if (loggedIn == null) + Courses = new(); + this.loggedId = loggedIn; + courseService = new CourseService(); + ExamSlot.ExamDate = DateTime.Now; + ExamSlot.Modifiable = true; + Update(); + } + + public void Update() + { + Courses.Clear(); + if (loggedId == null) { - Skills = courseService.GetAll(); + courses = courseService.GetAll(); ExamSlot.TutorId = -1; } else { - Skills = courseService.GetBySkills(loggedIn); - ExamSlot.TutorId = loggedIn.Id; + courses = courseService.GetBySkills(loggedId); + ExamSlot.TutorId = loggedId.Id; + } + foreach (var course in courses) + { + Courses.Add(new CourseViewModel(course)); } - SelectedCourse = null; - ExamSlot.ExamDate = DateTime.Now; - ExamSlot.Modifiable = true; - } + public bool CreateExam() { ExamSlot.CreatedAt = DateTime.Now; @@ -41,7 +56,12 @@ public bool CreateExam() if (SelectedCourse == null) MessageBox.Show("Must select language and level."); else if(ExamSlot.TutorId == -1) { - ExamSlot.TutorId = SmartSystem.GetTutorForExam(ExamSlot.ToExamSlot()); + if (loggedId == null) + { + ExamSlot.TutorId = SmartSystem.GetTutorForExam(ExamSlot.ToExamSlot()); + } + else ExamSlot.TutorId = loggedId.Id; + if(ExamSlot.TutorId != -1) { examSlotService.Add(ExamSlot.ToExamSlot()); diff --git a/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotUpdateVM.cs b/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotUpdateVM.cs index 8c5fc82..8b336fc 100644 --- a/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotUpdateVM.cs +++ b/LangLang/WPF/ViewModels/ExamViewModels/ExamSlotUpdateVM.cs @@ -4,27 +4,39 @@ using System.Collections.Generic; using System.Windows; using LangLang.Configuration; +using System.Collections.ObjectModel; +using LangLang.WPF.ViewModels.CourseViewModels; namespace LangLang.WPF.ViewModels.ExamViewModels { public class ExamSlotUpdateVM { - public List Skills { get; set; } + public ObservableCollection Courses { get; set; } + private List courses { get; set; } public Course SelectedCourse { get; set; } public ExamSlotViewModel ExamSlot { get; set; } public ExamSlotUpdateVM(int selectedExamId, Tutor loggedIn) { - - //Courses = courses.Values.ToList(); + Courses = new(); SelectedCourse = new Course(); ExamSlotService examSlotService = new(); ExamSlot = new ExamSlotViewModel(examSlotService.Get(selectedExamId)); CourseService courseService = new(); - Skills = courseService.GetBySkills(loggedIn); - + courses = courseService.GetBySkills(loggedIn); + Update(); + } + public void Update() + { + Courses.Clear(); + foreach (var course in courses) + { + Courses.Add(new CourseViewModel(course)); + } } + + public bool UpdateExam() { if (ExamSlot.IsValid) diff --git a/LangLang/WPF/Views/StudentView/Tabs/AvailableCourses.xaml.cs b/LangLang/WPF/Views/StudentView/Tabs/AvailableCourses.xaml.cs index 6547296..4c3e62d 100644 --- a/LangLang/WPF/Views/StudentView/Tabs/AvailableCourses.xaml.cs +++ b/LangLang/WPF/Views/StudentView/Tabs/AvailableCourses.xaml.cs @@ -20,7 +20,7 @@ public AvailableCourses(Student currentlyLoggedIn, StudentWindow parentWindow) this.parentWindow = parentWindow; SetDataForReview(); - levelCoursecb.ItemsSource = Enum.GetValues(typeof(LanguageLevel)); + levelCoursecb.ItemsSource = Enum.GetValues(typeof(Level)); AdjustButton(); } diff --git a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotCreateWindow.xaml b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotCreateWindow.xaml index 5b6b8bf..d5420f5 100644 --- a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotCreateWindow.xaml +++ b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotCreateWindow.xaml @@ -237,7 +237,7 @@ diff --git a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml index e955dbb..6d6a5de 100644 --- a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml +++ b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml @@ -234,7 +234,7 @@ diff --git a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml.cs b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml.cs index c0ead20..0bf4977 100644 --- a/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml.cs +++ b/LangLang/WPF/Views/TutorView/AdditionalWindows/ExamSlotView/ExamSlotUpdateWindow.xaml.cs @@ -5,23 +5,18 @@ namespace LangLang.WPF.Views.TutorView.AdditionalWindows.ExamSlotView { - /// - /// Interaction logic for ExamSlotUpdateWindow.xaml - /// public partial class ExamSlotUpdateWindow : Window { public ExamSlotUpdateVM ExamUpdateVM { get; set; } private ExamsReview _parent; public ExamSlotUpdateWindow(int selectedExamId, Tutor loggedIn, ExamsReview parent) - { - - + { InitializeComponent(); _parent = parent; ExamUpdateVM = new ExamSlotUpdateVM(selectedExamId, loggedIn); DataContext = ExamUpdateVM; - } + private void examSlotUpdateBtn_Click(object sender, RoutedEventArgs e) { if (ExamUpdateVM.UpdateExam()) @@ -29,9 +24,6 @@ private void examSlotUpdateBtn_Click(object sender, RoutedEventArgs e) _parent.Update(); Close(); } - } - - } } diff --git a/LangLang/WPF/Views/TutorView/Tabs/ExamsReview.xaml.cs b/LangLang/WPF/Views/TutorView/Tabs/ExamsReview.xaml.cs index ed1e0ac..ad40a23 100644 --- a/LangLang/WPF/Views/TutorView/Tabs/ExamsReview.xaml.cs +++ b/LangLang/WPF/Views/TutorView/Tabs/ExamsReview.xaml.cs @@ -24,7 +24,7 @@ public void Update() } private void ExamSlotCreateWindowBtn_Click(object sender, RoutedEventArgs e) { - ExamSlotCreateWindow createWindow = new(ExamsTutorVM.LoggedIn,this); + ExamSlotCreateWindow createWindow = new(ExamsTutorVM.LoggedIn, this); createWindow.Show(); } private void ExamSlotUpdateWindowBtn_Click(object sender, RoutedEventArgs e)