PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeOrderStorage.cs

79 lines
2.9 KiB
C#
Raw Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyPsqlImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyPsqlImplement.Implements
{
public class EmployeeOrderStorage : IEmployeeOrderStorage
{
private readonly ConstructionCompanyDatabase _source;
public EmployeeOrderStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<EmployeeOrderViewModel> GetFullList()
{
List<EmployeeOrderViewModel> result = new List<EmployeeOrderViewModel>();
foreach (var material in _source.EmployeeOrders)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<EmployeeOrderViewModel> GetFilteredList(EmployeeOrderSearchModel model)
{
if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue)
{
return new();
}
List<EmployeeOrderViewModel> result = new List<EmployeeOrderViewModel>();
foreach (var material in _source.EmployeeOrders)
{
if (material.EmployeeId == model.EmployeeId && material.OrderId == model.OrderId) result.Add(material.GetViewModel);
}
return result;
}
public EmployeeOrderViewModel? GetElement(EmployeeOrderSearchModel model)
{
if (model == null || !model.OrderId.HasValue || !model.EmployeeId.HasValue)
{
return new();
}
return _source.EmployeeOrders.FirstOrDefault(x => x.EmployeeId == model.EmployeeId && x.OrderId == model.OrderId)?.GetViewModel;
}
public EmployeeOrderViewModel? Insert(EmployeeOrderBindingModel model)
{
var command = EmployeeOrder.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newEmployeeOrder = _source.EmployeeOrders[_source.EmployeeOrders.Count - 1];
return newEmployeeOrder.GetViewModel;
}
public EmployeeOrderViewModel? Delete(EmployeeOrderBindingModel model)
{
var command = EmployeeOrder.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedEmployeeOrder = _source.EmployeeOrders.First(x => x.EmployeeId == model.EmployeeId && x.OrderId == model.OrderId).GetViewModel;
_source.ExecuteSql(command);
return deletedEmployeeOrder;
}
}
}