AddUserPageViewModel.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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)
  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
  54. {
  55. myContext.Add(NewLogined);
  56. myContext.SaveChanges();
  57. NewUser.IdUser = myContext.Logineds.FirstOrDefault(x => x.Login == NewLogined.Login && x.Password == NewLogined.Password).Id;
  58. myContext.Add(NewUser);
  59. myContext.SaveChanges();
  60. MainWindowViewModel.Self.Page = new AdminPage();
  61. }
  62. }
  63. public async Task Image()
  64. {
  65. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  66. desktop.MainWindow?.StorageProvider is not { } provider)
  67. throw new NullReferenceException("Missing StorageProvider instance.");
  68. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  69. {
  70. Title = "Выберите фото для аватарки",
  71. AllowMultiple = false
  72. });
  73. await using var readStream = await files[0].OpenReadAsync();
  74. byte[] buffer = new byte[1000000];
  75. var bytes = readStream.ReadAtLeast(buffer, 1);
  76. Array.Resize(ref buffer, bytes);
  77. NewUser.Image = buffer;
  78. ImageUser = new Bitmap(new MemoryStream(NewUser.Image));
  79. myContext.SaveChanges();
  80. }
  81. public void Back() => MainWindowViewModel.Self.Page = new AdminPage();
  82. }
  83. }