Преглед изворни кода

добавили удаление и добавление пользователя

fly9024 пре 1 месец
родитељ
комит
fe6238b595

+ 24 - 10
AvaloniaApplication2/ViewModels/Page2ViewModel.cs

@@ -16,25 +16,39 @@ namespace AvaloniaApplication2.ViewModels
 {
    public partial class Page2ViewModel : ViewModelBase
     {
-        [ObservableProperty] User editedUser;
+        [ObservableProperty] Logined editedLogined;
         [ObservableProperty] List<Gender> gendersList;
         [ObservableProperty] Bitmap imageUser;
-        
-        public Page2ViewModel(int id) 
+
+        public Page2ViewModel()
+        {
+            GendersList = db.Genders.ToList();
+            editedLogined = new Logined(){ User = new User { IdGenderNavigation = new Gender() }, IdRole=2 };
+        }
+
+            public Page2ViewModel(int id) 
         {
             GendersList = db.Genders.ToList();
-            editedUser = db.Users.Include(x=>x.IdGenderNavigation).Include(x=>x.IdTraits).FirstOrDefault(x=>x.IdUser == id);
-            imageUser = EditedUser.Image != null ? new Bitmap(new MemoryStream(editedUser.Image)) : new Bitmap("Assets/заглушка.jpg");
+            editedLogined = db.Logineds.Include(x=>x.User.IdTraits).Include(x=>x.User.IdGenderNavigation).FirstOrDefault(x=>x.Id == id);
+            imageUser = EditedLogined.User.Image != null ? new Bitmap(new MemoryStream(editedLogined.User.Image)) : new Bitmap("Assets/заглушка.jpg");
         }
         public DateTimeOffset DateTimeOffset
         {
-            get => new DateTimeOffset((DateTime)EditedUser.BirthDate, TimeSpan.Zero);
-            set => EditedUser.BirthDate = new DateTime(value.Year, value.Month, value.Day);
+            get => new DateTimeOffset((DateTime)EditedLogined.User.BirthDate, TimeSpan.Zero);
+            set => EditedLogined.User.BirthDate = new DateTime(value.Year, value.Month, value.Day);
         }
 
         public void GoBack() => MainWindowViewModel.Self.Page = new Page1();
 
-        public void Save() => db.SaveChanges();
+        public void Save()
+        {
+            if (EditedLogined.Id==0) db.Logineds.Add(EditedLogined);
+            db.SaveChanges();
+        }
+
+      
+
+
         public async Task Image()
         {
             if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
@@ -53,8 +67,8 @@ namespace AvaloniaApplication2.ViewModels
             byte[] buffer = new byte[10000000];
             var bytes = readStream.ReadAtLeast(buffer, 1);
             Array.Resize(ref buffer,bytes);
-            editedUser.Image = buffer;
-            ImageUser = new Bitmap(new MemoryStream(editedUser.Image));
+            EditedLogined.User.Image = buffer;
+            ImageUser = new Bitmap(new MemoryStream(EditedLogined.User.Image));
             db.SaveChanges();
         }
 

+ 9 - 1
AvaloniaApplication2/ViewModels/PageStartAdminViewModel.cs

@@ -62,9 +62,17 @@ namespace AvaloniaApplication2.ViewModels
 
         public void Press(int id)
         {
-            MainWindowViewModel.Self.Page2VM = new Page2ViewModel(id);
+            MainWindowViewModel.Self.Page2VM  = id == 0 ? new Page2ViewModel() : new Page2ViewModel(id);
             MainWindowViewModel.Self.Page = new Page2();
         }
+        public void DeleteUser(int id)
+        {
+            Logined deletedUser = db.Logineds.FirstOrDefault(x => x.Id == id);
+            if (deletedUser!=null) db.Logineds.Remove(deletedUser);
+            db.SaveChanges();
+            MainWindowViewModel.Self.Page = new PageStartAdmin();
+        }
+
 
     }
 }

+ 9 - 5
AvaloniaApplication2/Views/Page2.axaml

@@ -18,11 +18,11 @@
 	<StackPanel Orientation="Horizontal">
 		<StackPanel>
 			<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
-			<TextBox Text="{Binding Page2VM.EditedUser.Name}" />
-			<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>		
+			<TextBox Text="{Binding Page2VM.EditedLogined.User.Name}" />
+			<TextBox Text="{Binding Page2VM.EditedLogined.User.BirthDate}"/>		
 			<!--<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>-->
-			<DatePicker SelectedDate="{Binding Page2VM.EditedUser.BirthDate, Converter={StaticResource myDateConverter}}"/>
-			<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
+			<DatePicker SelectedDate="{Binding Page2VM.EditedLogined.User.BirthDate, Converter={StaticResource myDateConverter}}"/>
+			<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedLogined.User.IdGenderNavigation}">
 				<ComboBox.ItemTemplate>
 					<DataTemplate>
 						<TextBlock Text="{Binding Gender1}"/>
@@ -30,10 +30,14 @@
 				</ComboBox.ItemTemplate>
 			</ComboBox>
 			<Button Content="Выбрать изображение пользователя" Command="{Binding Page2VM.Image}"/>
+			<StackPanel>
+				<TextBox Text="{Binding Page2VM.EditedLogined.Login}"/>
+				<TextBox Text="{Binding Page2VM.EditedLogined.Password}"/>
+			</StackPanel>
 			<Button Content="Сохранить изменения" Command="{Binding Page2VM.Save}"/>
 			<Button Content="Развойти обратно" Command="{Binding Page2VM.GoBack}"/>
 		</StackPanel>
-		<ListBox ItemsSource="{Binding Page2VM.EditedUser.IdTraits}">
+		<ListBox ItemsSource="{Binding Page2VM.EditedLogined.User.IdTraits}">
 			<ListBox.ItemTemplate>
 				<DataTemplate>
 					<TextBlock Text="{Binding TraitName}"/>

+ 3 - 3
AvaloniaApplication2/Views/Page2New.axaml

@@ -8,10 +8,10 @@
 			 x:DataType="vm:MainWindowViewModel">
 	<StackPanel>
 		<Image Source="{Binding Page2VM.ImageUser}" Width="100" Height="100"/>
-		<TextBox Text="{Binding Page2VM.EditedUser.Name}"/>
-		<TextBox Text="{Binding Page2VM.EditedUser.BirthDate}"/>
+		<TextBox Text="{Binding Page2VM.EditedLogined.User.Name}"/>
+		<TextBox Text="{Binding Page2VM.EditedLogined.User.BirthDate}"/>
 		<DatePicker SelectedDate="{Binding Page2VM.DateTimeOffset}"/>
-		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedUser.IdGenderNavigation}">
+		<ComboBox ItemsSource="{Binding Page2VM.GendersList}" SelectedItem="{Binding Page2VM.EditedLogined.User.IdGenderNavigation}">
 			<ComboBox.ItemTemplate>
 				<DataTemplate>
 					<TextBlock Text="{Binding Gender1}"/>

+ 1 - 1
AvaloniaApplication2/Views/PageAdminNew.axaml

@@ -27,7 +27,7 @@
 						<TextBlock Text="{Binding Gender1}"/>
 					</DataTemplate>
 				</ComboBox.ItemTemplate>
-			</ComboBox>
+			</ComboBox>			
 		</StackPanel>
 		<ListBox ItemsSource="{Binding Users}" Grid.Row="1">
 			<ListBox.ItemTemplate>

+ 4 - 2
AvaloniaApplication2/Views/PageStartAdmin.axaml

@@ -29,6 +29,7 @@
 						</DataTemplate>
 					</ComboBox.ItemTemplate>
 				</ComboBox>
+				<Button Content="Добавить пользователя" Command="{Binding Press}" CommandParameter="0"/>
 			</StackPanel>			
 			<ListBox ItemsSource="{Binding Users}" Grid.Row="1">
 				<ListBox.ItemTemplate>
@@ -38,8 +39,9 @@
 								<TextBlock Text="{Binding Name, StringFormat=Имя: {0}}"/>
 								<TextBlock Text="{Binding BirthDate,StringFormat=Дата рождения: {0:dd/MM/yyyy}}"/>
 								<TextBlock Text="{Binding IdGenderNavigation.Gender1, StringFormat=Пол: {0}}"/>
-								<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>
-								<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}">
+								<TextBlock Text="{Binding IdUserNavigation.IdRoleNavigation.RoleName, StringFormat=Роль в системе: {0}}"/>								
+								<Button Content="Изменить персональные данные пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).Press}" CommandParameter="{Binding IdUser}"/>
+									<Button Content="Удалить пользователя" Command="{Binding $parent[UserControl].((vm:PageStartAdminViewModel)DataContext).DeleteUser}" CommandParameter="{Binding IdUser}">
 									<Button.IsVisible>
 										<MultiBinding Converter="{StaticResource myUserLogicalConverter}">
 											<Binding Path="IdUser"/>