123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Data.Common;
- using System.Linq;
- using MsBox.Avalonia;
- using ReactiveUI;
- using scool.Models;
- namespace scool.ViewModels
- {
- public class AddTeacherViewModel : ViewModelBase
- {
- public static _43pErmolinContext DB = new _43pErmolinContext();
- private string _name;
- private string _surname;
- private string _patronymic;
- private string _mail;
- private string _phone;
- private DateTimeOffset _birthday = new DateTimeOffset(DateTime.Now);
- private Subject _selectSubject;
- private string _exp;
- private bool _genderM = true;
- private bool _enableBTN = false;
- private ObservableCollection<Subject> _collSubSelect = new ObservableCollection<Subject>();
- public string TName { get => _name; set => this.RaiseAndSetIfChanged(ref _name, value); }
- public string TSurname { get => _surname; set => this.RaiseAndSetIfChanged(ref _surname, value); }
- public string TPatronymic { get => _patronymic; set => this.RaiseAndSetIfChanged(ref _patronymic, value); }
- public DateTimeOffset TBirthday { get => _birthday; set => this.RaiseAndSetIfChanged(ref _birthday, value); }
- public string TMail { get => _mail; set => this.RaiseAndSetIfChanged(ref _mail, value); }
- public string TPhone { get => _phone; set => this.RaiseAndSetIfChanged(ref _phone, value); }
- public string TExp { get => _exp; set => this.RaiseAndSetIfChanged(ref _exp, value); }
- public bool TGenderM { get => _genderM; set => this.RaiseAndSetIfChanged(ref _genderM, value); }
- public bool EnableBTN { get => _enableBTN; set => this.RaiseAndSetIfChanged(ref _enableBTN, value); }
- public ObservableCollection<Subject> CollSubSelect { get => _collSubSelect; set => this.RaiseAndSetIfChanged(ref _collSubSelect, value); }
- public List<Subject> CollSub => DB.Subjects.ToList();
- private bool CheckNotNull()
- {
- if (TName == null && TSurname == null && TPatronymic == null)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "ÔÈÎ íàäî ââåñòè");
- box.ShowAsync();
- return false;
- }
- if (TMail == null)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "Ýëåêòðîííóþ ïî÷òó æåëàòåëüíî (îáÿçàòåëüíî) çàïîëíèòü");
- box.ShowAsync();
- return false;
- }
- if (CollSubSelect.Count <= 0)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Íå-à", "Ïðåäïîäàâàòåëü áåç ïðåäìåòîâ?");
- box.ShowAsync();
- return false;
- }
- return true;
- }
- public Subject SelectSubject
- {
- get => _selectSubject;
- set
- {
- int f = 0;
- foreach (var item in CollSubSelect)
- {
- if (item == value)
- {
- f = 1;
- }
- }
- if (f == 0)
- {
- CollSubSelect.Add(value);
- }
- }
- }
- public bool AddNew()
- {
- if (CheckNotNull())
- {
- int IdGen = 1;
- int exp;
- string birtdate = TBirthday.Year + "-" + TBirthday.Month + "-" + TBirthday.Day;
- if (TGenderM == false)
- {
- IdGen = 2;
- }
- if (TExp == null)
- {
- exp = 0;
- }
- else
- {
- exp = Convert.ToInt32(TExp);
- }
- Teacher nT = new Teacher()
- {
- Name = TName,
- Surname = TSurname,
- Patronymic = TPatronymic,
- Birthdate = Convert.ToDateTime(birtdate),
- Email = TMail,
- Phone = TPhone,
- Expirience = exp,
- IdGender = IdGen
- };
- DB.Teachers.Add(nT);
- DB.SaveChanges();
- foreach (var item in CollSubSelect)
- {
- TeachersSubject ts = new TeachersSubject()
- {
- IdTeacher = nT.Id,
- IdSubject = item.Id
- };
- DB.TeachersSubjects.Add(ts);
- }
- DB.SaveChanges();
- CollSubSelect.Clear();
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- }
|