123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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<Gender> gendersList;
- List<Role> roleList;
- Bitmap imageUser;
- public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
- public User NewUser { get => newUser; set => newUser = value; }
- public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
- public Logined NewLogined { get => newLogined; set => newLogined = value; }
- public List<Role> 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)
- {
- Message = "Заполните все поля";
- }
- else if(myContext.Logineds.Include(x => x.User.IdGenderNavigation).FirstOrDefault(x => x.Login == newLogined.Login && x.Password == newLogined.Password) != null)
- {
- Message = "Такой пользователь уже существует";
- }
- 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();
- }
- }
|