85 lines
2.9 KiB
C#
85 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using AvaloniaApplication14_Inventory_300326.Models.DataBase;
|
|
using AvaloniaApplication14_Inventory_300326.Models.Factoryes;
|
|
using AvaloniaApplication14_Inventory_300326.Models.Models;
|
|
using AvaloniaApplication14_Inventory_300326.Views;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace AvaloniaApplication14_Inventory_300326.ViewModels;
|
|
|
|
public partial class EquipmentEditingWindowViewModel : ViewModelBase
|
|
{
|
|
private EquipmentEditingWindow _currentWindow;
|
|
private bool _isEditing;
|
|
private EquipmentVisual _equipment;
|
|
private IServiceProvider _serviceProvider;
|
|
|
|
[ObservableProperty] private string _name;
|
|
[ObservableProperty] private string _invNumber;
|
|
[ObservableProperty] private DateTimeOffset? _date;
|
|
[ObservableProperty] private decimal _cost;
|
|
[ObservableProperty] private bool _isWrittenOff;
|
|
[ObservableProperty] private Employee _currentEmployee;
|
|
[ObservableProperty] private ObservableCollection<Employee> _employees;
|
|
|
|
[RelayCommand]
|
|
private void Confrm()
|
|
{
|
|
_equipment.InvNumber = InvNumber;
|
|
_equipment.Date = Date;
|
|
_equipment.Cost = Cost;
|
|
_equipment.IsWrittenOff = IsWrittenOff;
|
|
_equipment.CurrentEmployee = CurrentEmployee;
|
|
_equipment.Name = Name;
|
|
|
|
using (var repo = _serviceProvider.GetService<EquipmentRepository>())
|
|
{
|
|
if (_isEditing)
|
|
{
|
|
repo.Update(EquipmentFactory.CreateFromVisual(_equipment));
|
|
}
|
|
else
|
|
{
|
|
repo.Add(EquipmentFactory.CreateFromVisual(_equipment));
|
|
}
|
|
}
|
|
_currentWindow.Close();
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Close()
|
|
{
|
|
_currentWindow.Close();
|
|
}
|
|
|
|
public EquipmentEditingWindowViewModel(IServiceProvider serviceProvider, Equipment equipment)
|
|
{
|
|
_serviceProvider = serviceProvider;
|
|
_isEditing = !equipment.IsNew();
|
|
using (var repo = _serviceProvider.GetService<EmployeeRepository>())
|
|
{
|
|
_equipment = EquipmentVisualFactory.CreateFromEquipment(equipment);
|
|
_equipment.CurrentEmployee = repo.GetById(equipment.CurrentEmployeeId);
|
|
}
|
|
Name = _equipment.Name;
|
|
InvNumber = _equipment.InvNumber;
|
|
Date = _equipment.Date;
|
|
Cost = _equipment.Cost;
|
|
IsWrittenOff = _equipment.IsWrittenOff;
|
|
using (var repo = _serviceProvider.GetService<EmployeeRepository>())
|
|
{
|
|
Employees = new ObservableCollection<Employee>(repo.GetAll());
|
|
}
|
|
|
|
CurrentEmployee = Employees.FirstOrDefault(o => o.Id == equipment.CurrentEmployeeId);
|
|
}
|
|
public void SetWindow(EquipmentEditingWindow window)
|
|
{
|
|
_currentWindow = window;
|
|
_currentWindow.OkButton.Content = _isEditing ? "Изменить" : "Добавить";
|
|
}
|
|
} |