Skip to content

Commit

Permalink
Merge pull request #470 from kzi-nastava/feat/add-foreign-keys-course…
Browse files Browse the repository at this point in the history
…-coursetimeslot

fix: Add foreign key constraints and fix bugs in view
  • Loading branch information
natasakasikovic authored Jun 16, 2024
2 parents fa8ca7c + 2e148fe commit 1812c55
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 30 deletions.
54 changes: 54 additions & 0 deletions LangLang/Repositories/DatabaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
base.OnModelCreating(modelBuilder);
ConfigureTutorEntity(modelBuilder);
ConfigureTutorSkillEntity(modelBuilder);
ConfigureCourseEntity(modelBuilder);
ConfigureCourseTimeSlotEntity(modelBuilder);

modelBuilder.Entity<Tutor>().ToTable("Tutor");
modelBuilder.Entity<Course>().ToTable("Course");
Expand Down Expand Up @@ -65,5 +67,57 @@ private void ConfigureTutorSkillEntity(ModelBuilder modelBuilder)
.HasForeignKey(ts => ts.LanguageLevelId)
.OnDelete(DeleteBehavior.Cascade);
}

private void ConfigureCourseEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasKey(c => c.Id);

modelBuilder.Entity<Course>()
.Property(c => c.TutorId)
.HasColumnName("TutorId");

modelBuilder.Entity<Course>()
.Property(c => c.LanguageLevelId)
.HasColumnName("LanguageLevelId");

modelBuilder.Entity<Course>()
.HasOne<Tutor>()
.WithMany()
.HasForeignKey(c => c.TutorId)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Course>()
.HasOne<LanguageLevel>()
.WithMany()
.HasForeignKey(c => c.LanguageLevelId)
.OnDelete(DeleteBehavior.Cascade);
}

private void ConfigureCourseTimeSlotEntity(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CourseTimeSlot>()
.HasKey(cts => cts.Id);

modelBuilder.Entity<CourseTimeSlot>()
.Property(cts => cts.CourseId)
.HasColumnName("CourseId");

modelBuilder.Entity<CourseTimeSlot>()
.Property(cts => cts.TimeSlotId)
.HasColumnName("TimeSlotId");

modelBuilder.Entity<CourseTimeSlot>()
.HasOne<Course>()
.WithMany()
.HasForeignKey(cts => cts.CourseId)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<CourseTimeSlot>()
.HasOne<TimeSlot>()
.WithMany()
.HasForeignKey(cts => cts.TimeSlotId)
.OnDelete(DeleteBehavior.Cascade);
}
}
}
6 changes: 6 additions & 0 deletions LangLang/Repositories/SqlRepositories/ExamSlotRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using LangLang.Domain.Models;
using LangLang.Domain.RepositoryInterfaces;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -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();
}
Expand Down
42 changes: 31 additions & 11 deletions LangLang/WPF/ViewModels/ExamViewModels/ExamSlotCreateVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course> Skills { get; set; }
public ObservableCollection<CourseViewModel> Courses { get; set; }
private List<Course> 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;
Expand All @@ -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());
Expand Down
22 changes: 17 additions & 5 deletions LangLang/WPF/ViewModels/ExamViewModels/ExamSlotUpdateVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course> Skills { get; set; }
public ObservableCollection<CourseViewModel> Courses { get; set; }
private List<Course> courses { get; set; }
public Course SelectedCourse { get; set; }
public ExamSlotViewModel ExamSlot { get; set; }

public ExamSlotUpdateVM(int selectedExamId, Tutor loggedIn)
{

//Courses = courses.Values.ToList<Course>();
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
</Border>
<DataGrid Grid.Column="0" Margin="15,45,15,50" AutoGenerateColumns="False"
x:Name="CoursesDataGrid"
ItemsSource="{Binding Skills, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Courses, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedCourse, UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single" IsReadOnly="True" SelectionChanged="CoursesDataGrid_SelectionChanged">
<DataGrid.Columns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
</Border>
<DataGrid Grid.Column="0" Margin="15,45,15,50" AutoGenerateColumns="False"
x:Name="CoursesDataGrid"
ItemsSource="{Binding Skills, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Courses, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedCourse, UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single" IsReadOnly="True">
<DataGrid.Columns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,25 @@

namespace LangLang.WPF.Views.TutorView.AdditionalWindows.ExamSlotView
{
/// <summary>
/// Interaction logic for ExamSlotUpdateWindow.xaml
/// </summary>
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())
{
_parent.Update();
Close();
}

}


}
}
2 changes: 1 addition & 1 deletion LangLang/WPF/Views/TutorView/Tabs/ExamsReview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1812c55

Please sign in to comment.