Создание класса хранилища заказов.

This commit is contained in:
Programmist73 2023-02-14 09:48:56 +04:00
parent c0fa37bd4b
commit 660ea2894f
3 changed files with 195 additions and 1 deletions

View File

@ -0,0 +1,97 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopFileImplement.Implements
{
//реализация интерфейса хранилища изделий
public class ManufactureStorage : IManufactureStorage
{
private readonly DataFileSingleton source;
public ManufactureStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ManufactureViewModel> GetFullList()
{
return source.Manufactures.Select(x => x.GetViewModel).ToList();
}
public List<ManufactureViewModel> GetFilteredList(ManufactureSearchModel model)
{
if (string.IsNullOrEmpty(model.ManufactureName))
{
return new();
}
return source.Manufactures.Where(x => x.ManufactureName.Contains(model.ManufactureName)).Select(x => x.GetViewModel).ToList();
}
public ManufactureViewModel? GetElement(ManufactureSearchModel model)
{
if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue)
{
return null;
}
return source.Manufactures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ManufactureViewModel? Insert(ManufactureBindingModel model)
{
model.Id = source.Manufactures.Count > 0 ? source.Manufactures.Max(x => x.Id) + 1 : 1;
var newManufacture = Manufacture.Create(model);
if (newManufacture == null)
{
return null;
}
source.Manufactures.Add(newManufacture);
source.SaveWorkPieces();
return newManufacture.GetViewModel;
}
public ManufactureViewModel? Update(ManufactureBindingModel model)
{
var manufacture = source.Manufactures.FirstOrDefault(x => x.Id == model.Id);
if (manufacture == null)
{
return null;
}
manufacture.Update(model);
source.SaveWorkPieces();
return manufacture.GetViewModel;
}
public ManufactureViewModel? Delete(ManufactureBindingModel model)
{
var element = source.Manufactures.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Manufactures.Remove(element);
source.SaveWorkPieces();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,96 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopFileImplement.Implements
{
//реализация интерфейса хранилища заказов
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Orders.FirstOrDefault(x =>(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
source.Orders.Add(newOrder);
source.SaveWorkPieces();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
source.SaveWorkPieces();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Orders.Remove(element);
source.SaveWorkPieces();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -76,6 +76,7 @@ namespace BlacksmithWorkshopFileImplement.Implements
workPiece.Update(model);
source.SaveWorkPieces();
return workPiece.GetViewModel;
}
@ -87,11 +88,11 @@ namespace BlacksmithWorkshopFileImplement.Implements
{
source.WorkPieces.Remove(element);
source.SaveWorkPieces();
return element.GetViewModel;
}
return null;
}
}
}