using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Media.Imaging; using Avalonia.Media.TextFormatting; using Avalonia.Platform.Storage; using AvaloniaApplicationTestNew.Models; using AvaloniaApplicationTestNew.Views; using Microsoft.EntityFrameworkCore; using ReactiveUI; using Tmds.DBus.Protocol; namespace AvaloniaApplicationTestNew.ViewModels { public class AddUserPageViewModel : ViewModelBase { string message; User newUser; Logined newLogined; List gendersList; List roleList; Bitmap imageUser; public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); } public User NewUser { get => newUser; set => newUser = value; } public List GendersList { get => gendersList; set => gendersList = value; } public Logined NewLogined { get => newLogined; set => newLogined = value; } public List RoleList { get => roleList; set => roleList = value; } public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); } public AddUserPageViewModel() { NewUser = new User(); NewLogined = new Logined(); RoleList = myContext.Roles.ToList(); GendersList = myContext.Genders.ToList(); } public void CreateUser() { if (string.IsNullOrWhiteSpace(newLogined.Login) || string.IsNullOrWhiteSpace(newLogined.Password) || newLogined.IdRoleNavigation == null || string.IsNullOrWhiteSpace(newUser.Name) || newUser.IdGenderNavigation == null || newUser.BirthDate == null) { Message = "Заполните все поля"; } else if(myContext.Logineds.Include(x => x.User.IdGenderNavigation).FirstOrDefault(x => x.Login == newLogined.Login && x.Password == newLogined.Password) != null) { Message = "Такой пользователь уже существует"; } else if(DateTime.Today <= newUser.BirthDate) { Message = "Пользователь родился в будущем? Исправьте дату"; newUser.BirthDate = null; } else { myContext.Add(NewLogined); myContext.SaveChanges(); NewUser.IdUser = myContext.Logineds.FirstOrDefault(x => x.Login == NewLogined.Login && x.Password == NewLogined.Password).Id; myContext.Add(NewUser); myContext.SaveChanges(); MainWindowViewModel.Self.Page = new AdminPage(); } } public async Task Image() { if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop || desktop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("Missing StorageProvider instance."); var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions() { Title = "Выберите фото для аватарки", AllowMultiple = false }); await using var readStream = await files[0].OpenReadAsync(); byte[] buffer = new byte[1000000]; var bytes = readStream.ReadAtLeast(buffer, 1); Array.Resize(ref buffer, bytes); NewUser.Image = buffer; ImageUser = new Bitmap(new MemoryStream(NewUser.Image)); myContext.SaveChanges(); } public void Back() => MainWindowViewModel.Self.Page = new AdminPage(); } }