Explorar o código

Добавление изображения в ЛК

unknown hai 1 semana
pai
achega
4c5aabe789

BIN=BIN
AvaloniaHomeTry/Assets/background.jpg


BIN=BIN
AvaloniaHomeTry/Assets/Заглушка.jpg


+ 2 - 2
AvaloniaHomeTry/Models/Bolshakov41pContext.cs

@@ -15,7 +15,6 @@ public partial class Bolshakov41pContext : DbContext
     {
     }
 
-
     public virtual DbSet<Gender> Genders { get; set; }
 
     public virtual DbSet<RolesTable> RolesTables { get; set; }
@@ -47,7 +46,7 @@ public partial class Bolshakov41pContext : DbContext
             entity.ToTable("roles_table");
 
             entity.Property(e => e.Id).HasColumnName("id");
-            entity.Property(e => e.RoleName)
+            entity.Property(e => e.Role)
                 .HasMaxLength(20)
                 .HasColumnName("role");
         });
@@ -66,6 +65,7 @@ public partial class Bolshakov41pContext : DbContext
                 .HasMaxLength(30)
                 .HasColumnName("firstname");
             entity.Property(e => e.IdGender).HasColumnName("genderid");
+            entity.Property(e => e.Image).HasColumnName("image");
             entity.Property(e => e.Login)
                 .HasMaxLength(30)
                 .HasColumnName("login");

+ 1 - 1
AvaloniaHomeTry/Models/RolesTable.cs

@@ -7,7 +7,7 @@ public partial class RolesTable
 {
     public int Id { get; set; }
 
-    public string RoleName { get; set; } = null!;
+    public string Role { get; set; } = null!;
 
     public virtual ICollection<UsersTable> UsersTables { get; set; } = new List<UsersTable>();
 }

+ 2 - 0
AvaloniaHomeTry/Models/UsersTable.cs

@@ -23,6 +23,8 @@ public partial class UsersTable
 
     public int? IdRole { get; set; }
 
+    public byte[]? Image { get; set; }
+
     public virtual Gender? GenderNavigation { get; set; }
 
     public virtual RolesTable? RoleNavigation { get; set; }

+ 11 - 3
AvaloniaHomeTry/ViewModels/AdminPageViewModel.cs

@@ -1,6 +1,12 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.Threading.Tasks;
+using Avalonia;
+using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Media.Imaging;
+using Avalonia.Platform.Storage;
 using AvaloniaHomeTry.Models;
 using Microsoft.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
@@ -13,8 +19,6 @@ namespace AvaloniaHomeTry.ViewModels
 	{
 
         UsersTable myUsers = new UsersTable();
-        
-        UsersTable editedUser = new UsersTable();
 
 		List<Gender> genderList;
 
@@ -26,15 +30,17 @@ namespace AvaloniaHomeTry.ViewModels
 
         string find;
 
+
         
         public List<Gender> GenderList { get => genderList; set => this.RaiseAndSetIfChanged(ref genderList, value); }
-		public UsersTable EditedUser { get => editedUser; set => this.RaiseAndSetIfChanged(ref editedUser, value); }
         public List<UsersTable> UsersTables { get => usersTables; set => this.RaiseAndSetIfChanged(ref usersTables, value); }
         public List<UsersTable> UsersTables0 { get => usersTables0; set => this.RaiseAndSetIfChanged(ref usersTables0, value); }
         public Gender FilterGender { get => filterGender; set { filterGender = value; AllFilters(); } }
 
         public string Find { get => find; set { find = value; AllFilters(); } }
 
+
+
         public AdminPageViewModel()
         {
             usersTables = db.UsersTable.ToList();
@@ -67,5 +73,7 @@ namespace AvaloniaHomeTry.ViewModels
 
         public void MyProfile() => MainWindowViewModel.Self.UC = new UserPage(MainWindowViewModel.Self.ID);
         public void Exit() => MainWindowViewModel.Self.UC = new AuthPage();
+
+
     }
 }

+ 1 - 1
AvaloniaHomeTry/ViewModels/RegistrationPageViewModel.cs

@@ -59,7 +59,7 @@ namespace AvaloniaHomeTry.ViewModels
                     Firstname = Name,
                     Patronymic = Patronymic,
                     Dataofbirth = Dob,
-                    IdGender = GenderId.Id,
+                    IdGender = genderId.Id,
                     IdRole = 2
                 };
 

+ 68 - 7
AvaloniaHomeTry/ViewModels/UserPageViewModel.cs

@@ -1,32 +1,93 @@
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using Avalonia.Controls.ApplicationLifetimes;
+using System.Threading.Tasks;
+using Avalonia.Media.Imaging;
+using Avalonia.Platform.Storage;
 using AvaloniaHomeTry.Models;
 using Microsoft.EntityFrameworkCore;
 using ReactiveUI;
+using Avalonia;
 
 namespace AvaloniaHomeTry.ViewModels
 {
 	public class UserPageViewModel : ViewModelBase
 	{
-		UsersTable currentUser;
-		public UsersTable CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
+		UsersTable? currentUser;
 
-		public UserPageViewModel(int idUser)
+        Bitmap? userImage;
+
+        List<UsersTable> mainUsers;
+
+        List<UsersTable> secondUsers;
+
+
+        public UsersTable? CurrentUser { get => currentUser; set => this.RaiseAndSetIfChanged(ref currentUser, value); }
+
+        public Bitmap? UserImage { get => userImage; set => this.RaiseAndSetIfChanged(ref userImage, value); }
+
+        public UserPageViewModel(int idUser)
 		{
-			
+
 			currentUser = db.UsersTable.Include(x => x.GenderNavigation).Include(x => x.RoleNavigation).FirstOrDefault(x => x.Id == idUser);
-		}
+            UserImage = currentUser.Image != null ? new Bitmap(new MemoryStream(currentUser.Image)) : new Bitmap("Assets/background.jpg");
+        }
 
 		public List<string> Genders => db.Genders.Select(x => x.GenderName).ToList();
 
 		public string GenderIndex { get => currentUser.GenderNavigation.GenderName; set => currentUser.IdGender = db.Genders.FirstOrDefault(x => x.GenderName == value).Id; }
 
 		public string DR { get => currentUser.Dataofbirth.ToString(); set => currentUser.Dataofbirth = DateTime.Parse(value); }
+        public List<UsersTable> MainUsers { get => mainUsers; set => this.RaiseAndSetIfChanged(ref mainUsers,value); }
+        public List<UsersTable> SecondUsers { get => secondUsers; set => this.RaiseAndSetIfChanged(ref secondUsers, value); }
+
+        public void SaveData() => db.SaveChanges();
+
+
+      
+
+        public void ExitProfile()
+		{
+
+			if (currentUser.IdRole == 1)
+			{
+				MainWindowViewModel.Self.UC = new AdminPage();
+			}
+			else
+			{
+				MainWindowViewModel.Self.UC = new AuthPage();
+			}
+
+		}
+
+        public async Task Image(int id)
+        {
+            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[10000000];
+                var bytes = readStream.ReadAtLeast(buffer, 1);
+                Array.Resize(ref buffer, bytes);
+                currentUser = db.UsersTable.FirstOrDefault(x => x.Id == id);
+                currentUser.Image = buffer;
+                UserImage = new Bitmap(new MemoryStream(currentUser.Image));
+                db.SaveChanges();
+                SecondUsers = db.UsersTable.ToList();
+                MainUsers = SecondUsers;
 
-		public void SaveData() => db.SaveChanges();
+            }
+        }
 
-        public void Exit() => MainWindowViewModel.Self.UC = new AuthPage();
 
     }
 }

+ 15 - 10
AvaloniaHomeTry/Views/UserPage.axaml

@@ -24,30 +24,35 @@
 			<ColumnDefinition Width="*"/>
 		</Grid.ColumnDefinitions>
 		
-		<StackPanel HorizontalAlignment="Center" Margin="10" Grid.Row="0" Grid.Column="1">
+		<StackPanel HorizontalAlignment="Center" Grid.Row="0" Grid.Column="1">
 			<TextBlock Text="Личный кабинет" HorizontalAlignment="Center"/>
+			<Image Source="{Binding UserImage}" Width="200" Height="200"/>
+		</StackPanel>		
+		
+		<StackPanel HorizontalAlignment="Center" Grid.Row="1" Grid.Column="2" Margin="10">
+			<Button Content="Изменить изображение" Command="{Binding Image}" CommandParameter="{Binding CurrentUser.Id}" IsEnabled="True"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="1" Grid.Column="0" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="2" Grid.Column="0" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Фамилия"/>
 			<TextBox Text="{Binding CurrentUser.Surname}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="1" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="2" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Имя"/>
 			<TextBox Text="{Binding CurrentUser.Firstname}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="1" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="2" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Отчество"/>
 			<TextBox Text="{Binding CurrentUser.Patronymic}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
-		<StackPanel Grid.Row="2" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Логин"/>
 			<TextBox Text="{Binding CurrentUser.Login}" Width="200" HorizontalAlignment="Left"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="2" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Grid.Column="2" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Пол"/>
 			<ComboBox ItemsSource="{Binding Genders}" 
 					  SelectedItem="{Binding GenderIndex}" 
@@ -55,14 +60,14 @@
 
 		</StackPanel>
 			
-		<StackPanel Grid.Row="2" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
+		<StackPanel Grid.Row="3" Grid.Column="1" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Дата рождения"/>
 			<DatePicker SelectedDate="{Binding DR}"/>
 		</StackPanel>
 		
-		<StackPanel Grid.Row="3" Margin="5" HorizontalAlignment="Left">
+		<StackPanel Grid.Row="4" Margin="5" HorizontalAlignment="Center">
 			<TextBlock Text="Роль в системе:"/>
-			<TextBlock Text="{Binding CurrentUser.RoleNavigation.RoleName}"/>
+			<TextBlock Text="{Binding CurrentUser.RoleNavigation.Role}"/>
 		</StackPanel>
 		
 		<StackPanel Grid.Row="6" Grid.ColumnSpan="3" HorizontalAlignment="Center" Margin="5">
@@ -70,7 +75,7 @@
 		</StackPanel>
 
 		<StackPanel Grid.Row="7" Grid.ColumnSpan="3" HorizontalAlignment="Center" Margin="5">
-			<Button Content="Выйти" Command="{Binding Exit}"/>
+			<Button Content="Выйти" Command="{Binding ExitProfile}"/>
 		</StackPanel>
 	</Grid>