123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Avalonia.Controls;
- using AvaloniaTeachersDB.Models;
- using AvaloniaTeachersDB.Views;
- using MsBox.Avalonia.Enums;
- using MsBox.Avalonia;
- using ReactiveUI;
- using Tmds.DBus.Protocol;
- using System.Collections.ObjectModel;
- namespace AvaloniaTeachersDB.ViewModels
- {
- public class AddTeacherViewModel : ReactiveObject
- {
- public static _43pToropovaContext DBConnection = new _43pToropovaContext();
- private string _name;
- private string _surname;
- private string _patronymic;
- private string _mail;
- private string _phone;
- private DateTimeOffset _birthday = new DateTimeOffset(DateTime.Now);
- private SchoolSubject _selectSubject;
- private int _selectIndexSubject;
- private string _exp;
- private bool _genderM = true;
- private bool _enableBTN = false;
- //private List<SchoolSubject> _collSubSelect = new List<SchoolSubject>();
- private ObservableCollection<SchoolSubject> _collSubSelect = new ObservableCollection<SchoolSubject>();
- 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<SchoolSubject> CollSubSelect { get => _collSubSelect; set => this.RaiseAndSetIfChanged(ref _collSubSelect, value); }
- public List<SchoolSubject> CollSub => DBConnection.SchoolSubjects.ToList();
- private bool CheckNotNull()
- {
- if (TName == null && TSurname == null && TPatronymic == null)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ôèî");
- box.ShowAsync();
- return false;
- }
- if (TMail == null)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ïî÷òû");
- box.ShowAsync();
- return false;
- }
- if(CollSubSelect.Count<=0)
- {
- var box = MessageBoxManager.GetMessageBoxStandard("Error", "íåò ïðåïîäàâàåìûõ ïðåäìåòîâ");
- box.ShowAsync();
- return false;
- }
- return true;
- }
- public SchoolSubject SelectSubject
- {
- get => _selectSubject;
- set
- {
-
- int f = 0;
- foreach (var item in CollSubSelect)
- {
- if (item == value)
- {
- f = 1;
- }
- }
- if (f == 0)
- CollSubSelect.Add(value);
-
- }
- }
- /*
- public int SelectIndexSubject
- {
- get => _selectIndexSubject;
- set
- {
- var mews = MessageBoxManager.GetMessageBoxStandard("ff", value.ToString());
- mews.ShowAsync();
- SchoolSubject tmp;
- if (CollSubSelect.Count>0)
- {
- int ind= 0;
- foreach(var item in CollSubSelect)
- {
- if (value == ind)
- {
- tmp = item;
- break;
- }
- ind++;
- }
- CollSubSelect.RemoveAt(ind);
- }
- }
- }
- */
- public bool AddNewTeacher()
- {
- if (CheckNotNull())
- {
- int indG = 1;
- int exp;
- //DateTime tmp = new DateTime(TBirthday.Year, TBirthday.Month, TBirthday.Day);
- string birtdate = TBirthday.Year +"-"+ TBirthday.Month +"-"+ TBirthday.Day;
- if (TGenderM == false)
- indG = 2;
- if (TExp == null)
- exp = 0;
- else
- exp = Convert.ToInt32(TExp);
- Teacher nT = new Teacher()
- {
- NameTeacher = TName,
- SurnameTeacher = TSurname,
- PatronymicTeacher = TPatronymic,
- BirthdayTeacher = DateOnly.Parse(birtdate),
- Mail = TMail,
- Phone = TPhone,
- ExperienceTeacher = exp,
- IdGender = indG
- };
- DBConnection.Teachers.Add(nT);
- DBConnection.SaveChanges();
- foreach(var item in CollSubSelect)
- {
- TeachersSubject ts = new TeachersSubject()
- {
- IdTeacher = nT.IdTeachers,
- IdSubjects = item.IdSubjects
- };
- DBConnection.TeachersSubjects.Add(ts);
- }
- DBConnection.SaveChanges();
- CollSubSelect.Clear();
- return true;
- }
- return false;
- }
- }
- }
|