123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.EntityFrameworkCore;
- using MsBox.Avalonia;
- using MsBox.Avalonia.Enums;
- using ReactiveUI;
- using TeacheresAndCourses.Models;
- namespace TeacheresAndCourses.ViewModels
- {
- public class AddTeacherViewModel : ViewModelBase
- {
- string buttonText;
- public string ButtonText { get => buttonText; set => this.RaiseAndSetIfChanged(ref buttonText, value); }
- Teacher? newTeacher;
- public Teacher? NewTeacher { get => newTeacher; set => this.RaiseAndSetIfChanged(ref newTeacher, value); }
- public List<TeachersAndSubject>? teacSubjectsList;
- public List<TeachersAndSubject>? TeacSubjectsList { get => teacSubjectsList; set => this.RaiseAndSetIfChanged(ref teacSubjectsList, value); }
- public List<TeachersAndCourse>? teacCoursesList;
- public List<TeachersAndCourse>? TeacCoursesList { get => teacCoursesList; set => this.RaiseAndSetIfChanged(ref teacCoursesList, value); }
- public AddTeacherViewModel() {
- newTeacher = new Teacher() { GenderNavigation = new Gender() };
- ButtonText = "Äîáàâèòü";
- }
- public AddTeacherViewModel(int teacherId)
- {
- newTeacher = MainWindowViewModel.connection.Teachers.Include(x=>x.GenderNavigation).Include(x=>x.TeachersAndCourses).ThenInclude(x=>x.Course).
- Include(x=>x.TeachersAndSubjects).ThenInclude(x=>x.Subject).FirstOrDefault(x=>x.Teacherid== teacherId);
- TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
- TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
- ButtonText = "Ñîõðàíèòü èçìåíåíèÿ";
- }
- public List<Course> courseList => MainWindowViewModel.connection.Courses.ToList().Except(newTeacher.TeachersAndCourses.Select(x => x.Course)).ToList();
- public List<Subject> subjectList => MainWindowViewModel.connection.Subjects.ToList().Except(newTeacher.TeachersAndSubjects.Select(x=>x.Subject)).ToList();
- public List<Gender> genderList => MainWindowViewModel.connection.Genders.ToList();
- // Äîáàâëåíèå âûáðàííîãî ïðåäìåòà â ñïèñîê
- Subject? newSub;
- public Subject? NewSub { get => null;
- set {
- if (value!=null)
- {
- NewTeacher.TeachersAndSubjects.Add(new TeachersAndSubject() { Teacher = NewTeacher, Subject = value });
- TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
- this.RaisePropertyChanged(nameof(subjectList));
- }
- } }
- // Äîáàâëåíèå âûáðàííîãî êóðñà â ñïèñîê
- Course? newCou;
- public Course? NewCou
- {
- get => null;
- set
- {
- if (value != null)
- {
- NewTeacher.TeachersAndCourses.Add(new TeachersAndCourse() { Teacher = NewTeacher, Course = value });
- TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
- this.RaisePropertyChanged(nameof(courseList));
- }
- }
- }
- // Óäàëåíèå ïðåäìåòà èç ñïèñêà ñ äîáàâëåííûìè ïðåäìåòàìè
- public void DeleteSub(TeachersAndSubject deleteSub)
- {
- NewTeacher.TeachersAndSubjects.Remove(deleteSub);
- TeacSubjectsList = NewTeacher.TeachersAndSubjects.ToList();
- this.RaisePropertyChanged(nameof(subjectList));
- }
- // Óäàëåíèå êóðñà èç ñïèñêà ñ äîáàâëåííûìè êóðñàìè
- public void DeleteCou(TeachersAndCourse deleteCou)
- {
- NewTeacher.TeachersAndCourses.Remove(deleteCou);
- TeacCoursesList = NewTeacher.TeachersAndCourses.ToList();
- this.RaisePropertyChanged(nameof(courseList));
- }
- public void ToMainPage()
- {
- MainWindowViewModel.Instance.Uc = new TeachersPage();
- }
- public void ToAddCoursePage()
- {
- MainWindowViewModel.Instance.Uc = new AddCourse();
- }
- public async void AddTeacher()
- {
- ButtonResult checking = await MessageBoxManager.GetMessageBoxStandard("Ïðîâåðêà äåéñòâèÿ", "Âû õîòèòå ñîõðàíèòü äàííûå?", ButtonEnum.YesNo).ShowAsync();
- switch (checking)
- {
- case ButtonResult.Yes:
- {
- if (NewTeacher.Teacherid == 0)
- {
- MainWindowViewModel.connection.Teachers.Add(NewTeacher);
- }
- MainWindowViewModel.connection.SaveChanges();
- MainWindowViewModel.Instance.Uc = new TeachersPage();
- break;
- }
- case ButtonResult.No:
- {
- break;
- }
- }
- }
- }
- }
|