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 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 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 _genderList; private string _newPassword; private string _newPasswordConfirm; private bool _isChecked = false; private string? _workTimeInYear; } }