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

91 lines
2.8 KiB
C#

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 OrderStorage : IOrderStorage
{
private readonly ConstructionCompanyDatabase _source;
public OrderStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
List<OrderViewModel> result = new List<OrderViewModel>();
foreach (var material in _source.Orders)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
List<OrderViewModel> result = new List<OrderViewModel>();
foreach (var material in _source.Orders)
{
if (material.Id == model.Id) result.Add(material.GetViewModel);
}
return result;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
return _source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var command = Order.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newOrder = _source.Orders[_source.Orders.Count - 1];
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var command = Order.UpdateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var updatedOrder = _source.Orders.First(x => x.Id == model.Id);
return updatedOrder.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var command = Order.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedOrder = _source.Orders.First(x => x.Id == model.Id).GetViewModel;
_source.ExecuteSql(command);
return deletedOrder;
}
}
}