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

80 lines
2.9 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.BusinessLogicContracts;
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 MaterialOrderStorage : IMaterialOrderStorage
{
private readonly ConstructionCompanyDatabase _source;
public MaterialOrderStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<MaterialOrderViewModel> GetFullList()
{
List<MaterialOrderViewModel> result = new List<MaterialOrderViewModel>();
foreach (var material in _source.MaterialOrders)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<MaterialOrderViewModel> GetFilteredList(MaterialOrderSearchModel model)
{
if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue)
{
return new();
}
List<MaterialOrderViewModel> result = new List<MaterialOrderViewModel>();
foreach (var material in _source.MaterialOrders)
{
if (material.MaterialId == model.MaterialId && material.OrderId == model.OrderId) result.Add(material.GetViewModel);
}
return result;
}
public MaterialOrderViewModel? GetElement(MaterialOrderSearchModel model)
{
if (model == null || !model.OrderId.HasValue || !model.MaterialId.HasValue)
{
return new();
}
return _source.MaterialOrders.FirstOrDefault(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId)?.GetViewModel;
}
public MaterialOrderViewModel? Insert(MaterialOrderBindingModel model)
{
var command = MaterialOrder.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newMaterialOrder = _source.MaterialOrders[_source.MaterialOrders.Count - 1];
return newMaterialOrder.GetViewModel;
}
public MaterialOrderViewModel? Delete(MaterialOrderBindingModel model)
{
var command = MaterialOrder.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedMaterialOrder = _source.MaterialOrders.First(x => x.MaterialId == model.MaterialId && x.OrderId == model.OrderId).GetViewModel;
_source.ExecuteSql(command);
return deletedMaterialOrder;
}
}
}