Browse Source

add change

AnnaBezaeva 2 weeks ago
parent
commit
998786b570

+ 30 - 0
AvaloniaApplication5/Converters/DateConverter.cs

@@ -0,0 +1,30 @@
+using Avalonia.Data.Converters;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Globalization;
+namespace AvaloniaApplication5.Converters
+{
+    internal class DateConverter : IValueConverter
+    {
+        public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {
+            if (value is DateTime)
+            {
+                return new DateTimeOffset((DateTime)value, TimeSpan.Zero);
+            }
+            return null;
+        }
+
+        public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
+        {
+            if (value is DateTimeOffset myDateTimeOffset)
+            {
+                return new DateTime(myDateTimeOffset.Year, myDateTimeOffset.Month, myDateTimeOffset.Day);
+            }
+            return null;
+        }
+    }
+}

+ 23 - 12
AvaloniaApplication5/ViewModels/AddPageViewModel.cs

@@ -5,33 +5,39 @@ using System.Data;
 using AvaloniaApplication5.Models;
 using Microsoft.EntityFrameworkCore;
 using ReactiveUI;
+using System.Text;
+using System.Threading.Tasks;
 
 namespace AvaloniaApplication5.ViewModels
 {
 	internal class AddPageViewModel : ViewModelBase
     {
 		Teacher? _newTeacher;
-
-		public List<TeacherCourse> TeacherCourses { get => _teacherCourses; set => this.RaiseAndSetIfChanged(ref _teacherCourses, value); }
-        public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); }
-	    public AddPageViewModel()
+         public Teacher? NewTeacher { get => _newTeacher; set => this.RaiseAndSetIfChanged(ref _newTeacher, value); }
+	   
+	     public List<TeacherCourse> TeacherCourses { get => _teacherCourses; set => this.RaiseAndSetIfChanged(ref _teacherCourses, value); }
+      public AddPageViewModel()
 		{
 			_newTeacher = new Teacher()
 			{
 				Gender = new Gender() 
 			};
 		}
-		public AddPageViewModel(int id)
+		public AddPageViewModel(int idt)
 		{
-			_newTeacher = MainWindowViewModel.myConnection.Teachers.Include(x=>x.Gender).Include(x=>x.TeacherCourses).ThenInclude(x=>x.Course).Include(x=>x.TeacherDisciplines).ThenInclude(x=>x.Discipline).FirstOrDefault(x=>x.Id == id);
+			_newTeacher = MainWindowViewModel.myConnection.Teachers.Include(x=>x.Gender).Include(x=>x.TeacherCourses).ThenInclude(x=>x.Course).Include(x=>x.TeacherDisciplines).ThenInclude(x=>x.Discipline).FirstOrDefault(x=>x.Id == idt);
             TeacherCourses = NewTeacher.TeacherCourses.ToList();
+			TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
 		}
 	    public List<Gender> Genders => MainWindowViewModel.myConnection.Genders.ToList();
-	    public List<Discipline> Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList();
-	    public List<Course> Courses => MainWindowViewModel.myConnection.Courses.ToList();
+	    public List<Discipline> Disciplines => MainWindowViewModel.myConnection.Disciplines.ToList().Except(_newTeacher.TeacherDisciplines.Select(x =>x.Discipline)).ToList();
+	    public List<Course> Courses => MainWindowViewModel.myConnection.Courses.ToList().Except(_newTeacher.TeacherCourses.Select(x => x.Course)).ToList();
+
+		public List<TeacherDiscipline> TeacherDisciplines { get => _teacherDisciplines; set => this.RaiseAndSetIfChanged(ref _teacherDisciplines, value); }
+	
 		List<TeacherCourse>? _teacherCourses;
 		List<TeacherDiscipline>? _teacherDisciplines;
-		public List<TeacherDiscipline> TeacherDisciplines { get => _teacherDisciplines; set => this.RaiseAndSetIfChanged(ref _teacherDisciplines, value); }
+		
 		Course? selectedCourse;
 		Discipline? selectedDiscipline;
 	    public Course? SelectedCourse
@@ -69,13 +75,18 @@ namespace AvaloniaApplication5.ViewModels
 
        
 
-		public void DeleteDiscipline(int id)
+		public void DeleteDiscipline(TeacherDiscipline teacherDiscipline)
 		{
-			NewTeacher.TeacherDisciplines.Remove(NewTeacher.TeacherDisciplines.FirstOrDefault(x => x.Id == id));
+			NewTeacher.TeacherDisciplines.Remove(teacherDiscipline);
 			TeacherDisciplines = NewTeacher.TeacherDisciplines.ToList();
             this.RaisePropertyChanged(nameof(Disciplines));
         }
-
+        public void DeleteCourse(TeacherCourse teacherCourse)
+        {
+            NewTeacher.TeacherCourses.Remove(teacherCourse);
+            TeacherCourses = NewTeacher.TeacherCourses.ToList();
+            this.RaisePropertyChanged(nameof(Courses));
+        }
         public void AddTeacher()
 		{
 			if(NewTeacher.Id == 0)

+ 17 - 4
AvaloniaApplication5/Views/AddPage.axaml

@@ -6,9 +6,10 @@
              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
              x:Class="AvaloniaApplication5.AddPage"
 			 x:DataType="vm:AddPageViewModel" FontSize="20"
+			 xmlns:conv="using:AvaloniaApplication5.Converters"
 			 >
 	<UserControl.Resources>
-		
+		<conv:DateConverter x:Key="myConverter"/>
 	</UserControl.Resources>
 	<Grid>
 		<ScrollViewer>
@@ -24,6 +25,7 @@
 				<TextBox Watermark="Почта" Width="300" Margin="0 10 0 0"></TextBox>
 				<TextBox Watermark="Телефон" Width="300" Margin="0 10 0 0"></TextBox>
 				<TextBox Watermark="Стаж" Width="300" Margin="0 10 0 0"></TextBox>
+				<DatePicker SelectedDate="{Binding NewTeacher.DateOfBirth, Converter={StaticResource myConverter}} "></DatePicker>
 				<ComboBox PlaceholderText="Выберите пол" ItemsSource="{Binding Genders}" Width="300" Margin="0 10 0 0" SelectedItem="{Binding NewTeacher.Gender}">
 					<ComboBox.ItemTemplate>
 						<DataTemplate>
@@ -54,13 +56,24 @@
 						<DataTemplate>
 							<StackPanel>
 								<TextBlock Text="{Binding Discipline.Name}"></TextBlock>
-								<Button Command="{Binding $parent[UserControl].((vm:AddPageViewModel)DataContext).DeleteDiscipline}" CommandParameter="{Binding Id}">Удалить</Button>
+								<Button Command="{Binding $parent[UserControl].((vm:AddPageViewModel)DataContext).DeleteDiscipline}" CommandParameter="{Binding}">Удалить</Button>
 							</StackPanel>
 						</DataTemplate>
 					</ListBox.ItemTemplate>
 				</ListBox>
-				
-				
+				<TextBlock>Выбранные курсы</TextBlock>
+				<ListBox ItemsSource="{Binding TeacherCourses}">
+					<ListBox.ItemTemplate>
+						<DataTemplate>
+							<StackPanel>
+								<TextBlock Text="{Binding Course.Name}"></TextBlock>
+								<Button Command="{Binding $parent[UserControl].((vm:AddPageViewModel)DataContext).DeleteCourse}" CommandParameter="{Binding}">Удалить</Button>
+							</StackPanel>
+						</DataTemplate>
+					</ListBox.ItemTemplate>
+				</ListBox>
+				<Button Command="{Binding AddTeacher}">Добавить</Button>
+
 			</StackPanel>
 				</Border>
 		</ScrollViewer>

+ 5 - 0
AvaloniaApplication5/Views/AddPage.axaml.cs

@@ -11,4 +11,9 @@ public partial class AddPage : UserControl
         InitializeComponent();
         DataContext = new AddPageViewModel();
     }
+    public AddPage(int idt)
+    {
+        InitializeComponent();
+        DataContext = new AddPageViewModel();
+    }
 }