ШмелевМВ il y a 12 heures
Parent
commit
9bceb8b497

+ 9 - 4
AvaloniaApplicationTestNew/ViewModels/AddUserPageViewModel.cs

@@ -45,7 +45,7 @@ namespace AvaloniaApplicationTestNew.ViewModels
         public void CreateUser()
         {
             if (string.IsNullOrWhiteSpace(newLogined.Login) || string.IsNullOrWhiteSpace(newLogined.Password) || newLogined.IdRoleNavigation == null
-                || string.IsNullOrWhiteSpace(newUser.Name) || newUser.IdGenderNavigation == null)
+                || string.IsNullOrWhiteSpace(newUser.Name) || newUser.IdGenderNavigation == null || newUser.BirthDate == null)
             {
                 Message = "Заполните все поля";
             }
@@ -53,10 +53,15 @@ namespace AvaloniaApplicationTestNew.ViewModels
             {
                 Message = "Такой пользователь уже существует";
             }
+            else if(DateTime.Today <= newUser.BirthDate)
+            {
+                Message = "Пользователь родился в будущем? Исправьте дату";
+                newUser.BirthDate = null;
+            }
             else
-                    {
-                        myContext.Add(NewLogined);
-                        myContext.SaveChanges();
+            {
+                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();

+ 51 - 3
AvaloniaApplicationTestNew/ViewModels/UserPageViewModel.cs

@@ -20,22 +20,46 @@ namespace AvaloniaApplicationTestNew.ViewModels
         User current_user;
         List<Gender> gendersList;
         Bitmap imageUser;
+        bool isReadyToDelete = false;
+        Trait selectTrait;
+        Trait selectItem;
+        List<Trait> withoutTraits;
+        List<Trait> withTraits;
 
         public string Message { get => message; set => this.RaiseAndSetIfChanged(ref message, value); }
         public User Current_user { get => current_user; set => current_user = value; }
         public List<Gender> GendersList { get => gendersList; set => gendersList = value; }
         public Bitmap ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
+        public Trait SelectTrait { get => selectTrait; set => this.RaiseAndSetIfChanged(ref selectTrait, value); }
+        public Trait SelectItem { get => selectItem; set => this.RaiseAndSetIfChanged(ref selectItem, value); }
+        public List<Trait> WithoutTraits { get => withoutTraits; set => this.RaiseAndSetIfChanged(ref withoutTraits, value); }
+        public List<Trait> WithTraits { get => withTraits; set => this.RaiseAndSetIfChanged(ref withTraits, value); }
+        public bool IsReadyToDelete { get => isReadyToDelete; set => this.RaiseAndSetIfChanged(ref isReadyToDelete, value); }
 
         public UserPageViewModel(int id) 
         {
-            Current_user = myContext.Users.Include(x => x.IdGenderNavigation).FirstOrDefault(x => x.IdUser == id);
+            Current_user = myContext.Users.Include(x => x.IdTraits).Include(x => x.IdGenderNavigation).FirstOrDefault(x => x.IdUser == id);
             GendersList = myContext.Genders.ToList();
             imageUser = Current_user.Image != null ? new Bitmap(new MemoryStream(Current_user.Image)) : new Bitmap("Assets\\ava.png");
+            WithoutTraits = myContext.Traits.ToList().Except(Current_user.IdTraits).ToList();
+            WithTraits = Current_user.IdTraits.ToList();
         }
         public void SaveAndChange()
         {
-            myContext.SaveChanges();
-            Message = "Данные сохранены";
+            if (string.IsNullOrWhiteSpace(Current_user.Name) || Current_user.IdGenderNavigation == null || Current_user.BirthDate == null)
+            {
+                Message = "Заполните все поля";
+            }
+            else if (DateTime.Today <= Current_user.BirthDate)
+            {
+                Message = "Пользователь родился в будущем? Исправьте дату";
+                Current_user.BirthDate = null;
+            }
+            else
+            {
+                myContext.SaveChanges();
+                Message = "Данные сохранены";
+            }
         }
         public void Back()
         {
@@ -73,5 +97,29 @@ namespace AvaloniaApplicationTestNew.ViewModels
             MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
             MainWindowViewModel.Self.Page = new UserPage();
         }
+        public void AddTrait()
+        {
+            if(SelectTrait != null)
+            {
+                Current_user.IdTraits.Add(SelectTrait);
+                SaveAndChange();
+                MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
+                MainWindowViewModel.Self.Page = new UserPage();
+            }
+        }
+        public void DeleteTrait()
+        {
+            if(SelectItem != null)
+            {
+                Current_user.IdTraits.Remove(SelectItem);
+                SaveAndChange();
+                MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(Current_user.IdUser);
+                MainWindowViewModel.Self.Page = new UserPage();
+            }
+            else
+            {
+                IsReadyToDelete = true;
+            }
+        }
     }
 }

+ 3 - 2
AvaloniaApplicationTestNew/Views/AddUserPage.axaml

@@ -21,7 +21,8 @@
 			</ComboBox.ItemTemplate>
 		</ComboBox>
 		<TextBox Watermark="Имя пользователя" Text="{Binding AddUserPageVM.NewUser.Name}"/>
-		<DatePicker SelectedDate="{Binding UserPageVM.Current_user.BirthDate, Converter={StaticResource myDateTimeOffset}}"/>		<ComboBox ItemsSource="{Binding AddUserPageVM.GendersList}" SelectedItem="{Binding AddUserPageVM.NewUser.IdGenderNavigation}">
+		<DatePicker SelectedDate="{Binding AddUserPageVM.NewUser.BirthDate, Converter={StaticResource myDateTimeOffset}}"/>		
+		<ComboBox ItemsSource="{Binding AddUserPageVM.GendersList}" SelectedItem="{Binding AddUserPageVM.NewUser.IdGenderNavigation}">
 			<ComboBox.ItemTemplate>
 				<DataTemplate>
 					<TextBlock Text="{Binding Gender1}"/>
@@ -31,7 +32,7 @@
 		<Button Content="Добавить изображение" Command="{Binding AddUserPageVM.Image}"/>
 		<Button Content="Сохранить пользователя" Command="{Binding AddUserPageVM.CreateUser}"/>
 		<Button Content="Назад" Command="{Binding AddUserPageVM.Back}"/>
-		<TextBlock Text="{Binding AddUserPageVM.Message}"/>
+		<TextBlock Text="{Binding AddUserPageVM.Message}" Foreground="Red"/>
 	</StackPanel>
 </UserControl>
 

+ 1 - 1
AvaloniaApplicationTestNew/Views/Markup.axaml

@@ -8,7 +8,7 @@
              x:Class="AvaloniaApplicationTestNew.Views.Markup">
 
 	<Grid ColumnDefinitions="200,*" RowDefinitions="50,*,50">
-		<TextBlock Text="Заголовок" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
+		<TextBlock Text="Ваши персональные данные" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
 		<TextBlock Text="Меню" Grid.Row="1" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
 		<TextBlock Text="Футер" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
 		<TextBlock Text="" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>

+ 45 - 21
AvaloniaApplicationTestNew/Views/UserPage.axaml

@@ -3,7 +3,7 @@
 			 xmlns:vm="using:AvaloniaApplicationTestNew.ViewModels"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+             mc:Ignorable="d" d:DesignWidth="1400" d:DesignHeight="800"
              x:Class="AvaloniaApplicationTestNew.Views.UserPage"
 			 x:DataType="vm:MainWindowViewModel"
 			 xmlns:conv="using:AvaloniaApplicationTestNew.Resources">
@@ -11,25 +11,49 @@
 		<conv:DateConverter x:Key="myDateTimeOffset"/>
 		<conv:ImageConverter x:Key="myByteaBitmap"/>
 	</UserControl.Resources>
-	<StackPanel VerticalAlignment="Center">
-		<Image Source="{Binding UserPageVM.Current_user.Image, Converter={StaticResource myByteaBitmap}}" Width="200" Height="200"/>
-		<!--<Image Source="{Binding UserPageVM.ImageUser}" Width="200" Height="200"/>-->
-		<TextBox Watermark="Имя пользователя" Text="{Binding UserPageVM.Current_user.Name}"/>
-		<DatePicker SelectedDate="{Binding UserPageVM.Current_user.BirthDate, Converter={StaticResource myDateTimeOffset}}"/>
-		<ComboBox ItemsSource="{Binding UserPageVM.GendersList}" SelectedItem="{Binding UserPageVM.Current_user.IdGenderNavigation}">
-			<ComboBox.ItemTemplate>
-				<DataTemplate>
-					<TextBlock Text="{Binding Gender1}"/>
-				</DataTemplate>
-			</ComboBox.ItemTemplate>
-		</ComboBox>
-		<Button Content="Выбрать аватарку пользователя" Command="{Binding UserPageVM.Image}"/>
-		<Button Content="Удалить изображение" Command="{Binding UserPageVM.DeleteImage}"/>
-		<Button Content="Сохранить изменения" Command="{Binding UserPageVM.SaveAndChange}"/>
-		<Button Content="Выйти" Command="{Binding UserPageVM.Back}" IsVisible="{Binding !IsButtonVisible}"/>
-		<Button Content="Удалить пользователя" IsVisible="{Binding IsButtonVisible}" Command="{Binding DeleteUser}"/>
-		<Button Content="Назад" IsVisible="{Binding IsButtonVisible}" Command="{Binding BackToList}"/>
-		<TextBlock Text="{Binding UserPageVM.Message}"/>
-	</StackPanel>
+	<Grid ColumnDefinitions="*,*" RowDefinitions="300, *">
+		<StackPanel Grid.ColumnSpan="2" Grid.Row="0" Orientation="Vertical">
+			<Image Source="{Binding UserPageVM.Current_user.Image, Converter={StaticResource myByteaBitmap}}" Width="200" Height="200"/>
+		</StackPanel>
+		<StackPanel Grid.Column="0" Grid.Row="1">
+			<TextBox Watermark="Имя пользователя" Text="{Binding UserPageVM.Current_user.Name}"/>
+			<DatePicker SelectedDate="{Binding UserPageVM.Current_user.BirthDate, Converter={StaticResource myDateTimeOffset}}"/>
+			<ComboBox ItemsSource="{Binding UserPageVM.GendersList}" SelectedItem="{Binding UserPageVM.Current_user.IdGenderNavigation}">
+				<ComboBox.ItemTemplate>
+					<DataTemplate>
+						<TextBlock Text="{Binding Gender1}"/>
+					</DataTemplate>
+				</ComboBox.ItemTemplate>
+			</ComboBox>
+			<Button Content="Выбрать аватарку пользователя" Command="{Binding UserPageVM.Image}"/>
+			<Button Content="Удалить изображение" Command="{Binding UserPageVM.DeleteImage}"/>
+			<Button Content="Сохранить изменения" Command="{Binding UserPageVM.SaveAndChange}"/>
+			<Button Content="Выйти" Command="{Binding UserPageVM.Back}" IsVisible="{Binding !IsButtonVisible}"/>
+			<Button Content="Удалить пользователя" IsVisible="{Binding IsButtonVisible}" Command="{Binding DeleteUser}"/>
+			<Button Content="Назад" IsVisible="{Binding IsButtonVisible}" Command="{Binding BackToList}"/>
+			<TextBlock Text="{Binding UserPageVM.Message}" Foreground="Red"/>
+		</StackPanel>
+		<StackPanel Grid.Column="1" Orientation="Vertical" Grid.Row="1">
+			<ComboBox ItemsSource="{Binding UserPageVM.WithoutTraits}" SelectedItem="{Binding UserPageVM.SelectTrait}" Grid.Column="1">
+				<ComboBox.ItemTemplate>
+					<DataTemplate>
+						<TextBlock Text="{Binding Traitname}"/>
+					</DataTemplate>
+				</ComboBox.ItemTemplate>
+			</ComboBox>
+			<StackPanel Grid.Column="1">
+				<Button Content="Добавить качество" Command="{Binding UserPageVM.AddTrait}"/>
+				<Button Content="Удалить качество" Command="{Binding UserPageVM.DeleteTrait}"/>
+			</StackPanel>
+			<TextBlock Foreground="Red" IsVisible="{Binding UserPageVM.IsReadyToDelete}" Text="Сначала выделите качество из списка, нажав на него"/>
+			<ListBox ItemsSource="{Binding UserPageVM.WithTraits}" SelectedItem="{Binding UserPageVM.SelectItem}" Grid.Column="1">
+				<ListBox.ItemTemplate>
+					<DataTemplate>
+						<TextBlock Text="{Binding Traitname}"/>
+					</DataTemplate>
+				</ListBox.ItemTemplate>
+			</ListBox>
+		</StackPanel>
+	</Grid>
 </UserControl>