BathhouseManagment/ViewModels/WindowViewModels/Adds/AddEmployeeWindowViewModel.cs

66 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using BathHouseManagmet.Database;
using BathHouseManagmet.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
namespace BathHouseManagmet.ViewModels;
public partial class AddEmployeeWindowViewModel : ViewModelBase
{
private readonly IServiceProvider _serviceProvider;
[ObservableProperty] private string _employeeName;
[ObservableProperty] private string _employeeSurname;
[ObservableProperty] private Position _employeePosition;
[ObservableProperty] private List<Employee> _employees;
[ObservableProperty] private List<Position> _positions;
[ObservableProperty] private Position _selectedPosition;
public AddEmployeeWindowViewModel(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
using (var rep = serviceProvider.GetRequiredService<EmployeeRepository>())
Employees = new List<Employee>(rep.GetPage());
}
[RelayCommand]
public void AddEmployee()
{
var emp = new Employee()
{
Name = EmployeeName,
Surname = EmployeeSurname,
Position = EmployeePosition
};
using (var rep = _serviceProvider.GetRequiredService<EmployeeRepository>())
rep.Add(emp);
CloseWindow();
}
/* Прикольная штука, пользоваться можно. Но как мать его работать с MessageBox??
private bool Validate()
{
if (string.IsNullOrWhiteSpace(_employeeName) || string.IsNullOrWhiteSpace(_employeeSurname))
return false;
return true;
}
*/
[RelayCommand]
public void CloseWindow()
{
close();
}
private Action close;
public void SetClose(Action close)
{
this.close = close;
}
}