NewUserViewModel.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Media.Imaging;
  4. using Avalonia.Platform.Storage;
  5. using CommunityToolkit.Mvvm.ComponentModel;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using TestRepeat.Models;
  13. using TestRepeat.Views;
  14. namespace TestRepeat.ViewModels
  15. {
  16. public partial class NewUserViewModel : ViewModelBase
  17. {
  18. [ObservableProperty] User newUser = new User();
  19. [ObservableProperty] Logined newLogin = new Logined();
  20. [ObservableProperty] List<Gender> listGender;
  21. [ObservableProperty] List<Role> listRole;
  22. [ObservableProperty] bool isUserCreated = false;
  23. [ObservableProperty] Bitmap imgUser;
  24. [ObservableProperty] string passwordContent;
  25. DateTimeOffset birthDateNewUser = DateTimeOffset.MinValue;
  26. public DateTimeOffset BirthDateNewUser
  27. {
  28. get => birthDateNewUser;
  29. set
  30. {
  31. NewUser.BirthDate = new DateTime(value.Year, value.Month, value.Day);
  32. }
  33. }
  34. public NewUserViewModel()
  35. {
  36. listGender = MainWindowViewModel.Db_context.Genders.ToList();
  37. listRole = MainWindowViewModel.Db_context.Roles.ToList();
  38. }
  39. public void CreateNewUserAndLogin()
  40. {
  41. NewLogin.Password = MD5.HashData(Encoding.ASCII.GetBytes(PasswordContent));
  42. MainWindowViewModel.Db_context.Logineds.Add(NewLogin);
  43. MainWindowViewModel.Db_context.SaveChanges();
  44. NewUser.IdUser = MainWindowViewModel.Db_context.Logineds.FirstOrDefault(log => log.Login == NewLogin.Login && log.Password == NewLogin.Password).Id;
  45. MainWindowViewModel.Db_context.Users.Add(NewUser);
  46. MainWindowViewModel.Db_context.SaveChanges();
  47. IsUserCreated = true;
  48. }
  49. public void BackToListUser()
  50. {
  51. MainWindowViewModel.Instance.Uc = new InfoUsersDate(MainWindowViewModel.Db_context.Users.ToList());
  52. }
  53. public void BackToAuth()
  54. {
  55. MainWindowViewModel.Instance.Uc = new Authorization();
  56. }
  57. public async void AddImgUser()
  58. {
  59. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
  60. desktop.MainWindow?.StorageProvider is not { } provider)
  61. throw new NullReferenceException("Missing StorageProvider instance.");
  62. var files = await provider.OpenFilePickerAsync(new FilePickerOpenOptions()
  63. {
  64. Title = "Âûáåðèòå ôàéë ñ èçîáðàæåíèåì ïîëüçîâàòåëÿ",
  65. AllowMultiple = false
  66. });
  67. await using var readStream = await files[0].OpenReadAsync();
  68. byte[] buffer = new byte[10000000];
  69. var bytes = readStream.ReadAtLeast(buffer, 1);
  70. Array.Resize(ref buffer, bytes);
  71. NewUser.ImgUser = buffer;
  72. ImgUser = new Bitmap(new MemoryStream(buffer));
  73. }
  74. }
  75. }