123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- using System;
- using System.Collections.Generic;
- using MsBox.Avalonia;
- using System.Reactive;
- using System.Text.RegularExpressions;
- using ReactiveUI;
- using Microsoft.EntityFrameworkCore;
- using System.Linq;
- using System.Numerics;
- using System.Text;
- namespace prakt1314.ViewModels
- {
- public class CabinetViewModel : ReactiveObject
- {
- public CabinetViewModel(int userID)
- {
- ConfirmChanges = ReactiveCommand.Create(ConfirmChangesCommand);
- using (AvaloniaPrakt13DbContext _dbContext = new AvaloniaPrakt13DbContext()) _genderList = _dbContext.UserGenders.ToList();
- using (AvaloniaPrakt13DbContext _dbContext = new AvaloniaPrakt13DbContext()) UserData = _dbContext.UserData
- .Include(a => a.IdUserNavigation)
- .Include(a => a.IdGenderNavigation)
- .Where(a => a.IdUser == userID)
- .FirstOrDefault();
- WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
- foreach (UserGender gender in GenderList)
- {
- _userData.IdGenderNavigation = (gender.IdGender == _userData.IdGender) ? gender : _userData.IdGenderNavigation;
- }
- this.RaisePropertyChanged(nameof(UserData));
- }
- public UserDatum UserData
- {
- get => _userData;
- set => this.RaiseAndSetIfChanged(ref _userData, value);
- }
- public ICollection<UserGender> GenderList => _genderList;
- public string NewPassword
- {
- get => _newPassword;
- set => this.RaiseAndSetIfChanged(ref _newPassword, value);
- }
- public string NewPasswordConfirm
- {
- get => _newPasswordConfirm;
- set => this.RaiseAndSetIfChanged(ref _newPasswordConfirm, value);
- }
- public ReactiveCommand<Unit, Unit> ConfirmChanges { get; }
- public bool IsChecked
- {
- get => _isChecked;
- set => this.RaiseAndSetIfChanged(ref _isChecked, value);
- }
- public string? WorkTimeInYear
- {
- get => _workTimeInYear;
- set => this.RaiseAndSetIfChanged(ref _workTimeInYear, value);
- }
- public void ConfirmChangesCommand()
- {
- try
- {
- double year = Convert.ToDouble(WorkTimeInYear);
- UserData.Worktime = (int)(year * 12);
- WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
- }
- catch
- {
- UserData.Worktime = 0;
- WorkTimeInYear = ((double)UserData.Worktime / 12).ToString();
- }
- if (UserData.Surname.Length == 0 || UserData.Surname is null)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ôàìèëèÿ ïóñòà!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.Name.Length == 0 || UserData.Name is null)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Èìÿ ïóñòîå!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.Patronymic is null || UserData.Patronymic.Length == 0)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Îò÷åñòâî ïóñòîå!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.IdUserNavigation.Login.Length < 3 || UserData.IdUserNavigation.Login is null)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ëîãèí íå ìåíåå 3 ñèìâîëîâ!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.Birthdate >= DateTime.Now)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Äàòà ðàæäåíèÿ â áóäóùåì!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.Email is null || UserData.Email.Length == 0)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ïî÷òà ïóñòà!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.Email.Length > 0 && !Regex.IsMatch(UserData.Email,
- "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"))
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ïî÷òà íåêîððåêòíà!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (UserData.PhoneNumber is null || UserData.PhoneNumber.Length == 0)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Íîìåð òåëåôîíà ïóñò!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (!(UserData.PhoneNumber is null || UserData.PhoneNumber.Length == 0))
- {
- if (!Regex.IsMatch(UserData.PhoneNumber, "^((8|\\+7)[\\- ]?)?(\\(?\\d{3}\\)?[\\- ]?)?[\\d\\- ]{7,10}$"))
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Òåëåôîí íåêîððåêòåí!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- StringBuilder ConvertedPhone = new StringBuilder("89");
- int NumberOfDigits = 0;
- foreach (char ch in UserData.PhoneNumber) if (char.IsDigit(ch)) NumberOfDigits++;
- int counter = -9 + NumberOfDigits;
- foreach (char ch in UserData.PhoneNumber) if (char.IsDigit(ch)) if (counter-- <= 0) ConvertedPhone.Append(ch);
- UserData.PhoneNumber = ConvertedPhone.ToString();
- }
- if (IsChecked)
- {
- if (NewPassword is null || NewPassword.Length == 0)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ïàðîëü ïóñò!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- if (NewPassword != NewPasswordConfirm)
- {
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ íå ñîõðàíåíû!"
- , "Ïàðîëè ðàçíûå!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- UserData.IdUserNavigation.Password = NewPassword;
- }
- using (AvaloniaPrakt13DbContext db = new AvaloniaPrakt13DbContext())
- {
- db.Entry(UserData).State = EntityState.Modified;
- db.Entry(UserData.IdUserNavigation).State = EntityState.Modified;
- db.Entry(UserData.IdGenderNavigation).State = EntityState.Modified;
- db.SaveChanges();
- }
- MessageBoxManager.GetMessageBoxStandard("Èçìåíåíèÿ ñîõðàíåíû!"
- , "Èçìåíåíèÿ óñïåøíî ñîõðàíåíû!"
- , MsBox.Avalonia.Enums.ButtonEnum.Ok)
- .ShowAsync();
- return;
- }
- private UserDatum _userData;
- private List<UserGender> _genderList;
- private string _newPassword;
- private string _newPasswordConfirm;
- private bool _isChecked = false;
- private string? _workTimeInYear;
- }
- }
|