AddUserPageViewModel.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Reactive.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Avalonia;
  10. using Avalonia.Controls.ApplicationLifetimes;
  11. using Avalonia.Media.Imaging;
  12. using Avalonia.Media.TextFormatting;
  13. using Avalonia.Platform.Storage;
  14. using AvaloniaApplicationTestNew.Models;
  15. using AvaloniaApplicationTestNew.Views;
  16. using Microsoft.EntityFrameworkCore;
  17. using ReactiveUI;
  18. using Tmds.DBus.Protocol;
  19. namespace AvaloniaApplicationTestNew.ViewModels
  20. {
  21. public class AddUserPageViewModel : ViewModelBase
  22. {
  23. string message;
  24. User newUser;
  25. Logined newLogined;
  26. List<Gender> gendersList;
  27. List<Role> roleList;
  28. Bitmap imageUser;
  29. public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
  30. public User NewUser { get => newUser; set => newUser = value; }
  31. public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
  32. public Logined NewLogined { get => newLogined; set => newLogined = value; }
  33. public List<Role> RoleList { get => roleList; set => roleList = value; }
  34. public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
  35. public AddUserPageViewModel()
  36. {
  37. NewUser = new User();
  38. NewLogined = new Logined();
  39. RoleList = myContext.Roles.ToList();
  40. GendersList = myContext.Genders.ToList();
  41. }
  42. public void CreateUser()
  43. {
  44. if (string.IsNullOrWhiteSpace(newLogined.Login) || string.IsNullOrWhiteSpace(newLogined.Password) || newLogined.IdRoleNavigation == null
  45. || string.IsNullOrWhiteSpace(newUser.Name) || newUser.IdGenderNavigation == null || newUser.BirthDate == null)
  46. {
  47. Message = "Заполните все поля";
  48. }
  49. else if(myContext.Logineds.Include(x => x.User.IdGenderNavigation).FirstOrDefault(x => x.Login == newLogined.Login && x.Password == newLogined.Password) != null)
  50. {
  51. Message = "Такой пользователь уже существует";
  52. }
  53. else if(DateTime.Today <= newUser.BirthDate)
  54. {
  55. Message = "Пользователь родился в будущем? Исправьте дату";
  56. newUser.BirthDate = null;
  57. }
  58. else
  59. {
  60. myContext.Add(NewLogined);
  61. myContext.SaveChanges();
  62. NewUser.IdUser = myContext.Logineds.FirstOrDefault(x => x.Login == NewLogined.Login && x.Password == NewLogined.Password).Id;
  63. myContext.Add(NewUser);
  64. myContext.SaveChanges();
  65. MainWindowViewModel.Self.Page = new AdminPage();
  66. }
  67. }
  68. public async Task Image()
  69. {
  70. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  71. desktop.MainWindow?.StorageProvider is not { } provider)
  72. throw new NullReferenceException("Missing StorageProvider instance.");
  73. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  74. {
  75. Title = "Выберите фото для аватарки",
  76. AllowMultiple = false
  77. });
  78. await using var readStream = await files[0].OpenReadAsync();
  79. byte[] buffer = new byte[1000000];
  80. var bytes = readStream.ReadAtLeast(buffer, 1);
  81. Array.Resize(ref buffer, bytes);
  82. NewUser.Image = buffer;
  83. ImageUser = new Bitmap(new MemoryStream(NewUser.Image));
  84. myContext.SaveChanges();
  85. }
  86. public void Back() => MainWindowViewModel.Self.Page = new AdminPage();
  87. }
  88. }