еще несколько изменений - создание библиотеки sushibarimplements (не готова)
This commit is contained in:
parent
35919d7015
commit
0eeceb5e57
@ -4,7 +4,7 @@ namespace SushiBarDataModels
|
|||||||
{
|
{
|
||||||
public interface IOrderModel : IId
|
public interface IOrderModel : IId
|
||||||
{
|
{
|
||||||
int ProductId { get; }
|
int SushiId { get; }
|
||||||
int Count { get; }
|
int Count { get; }
|
||||||
double Sum { get; }
|
double Sum { get; }
|
||||||
OrderStatus Status { get; }
|
OrderStatus Status { get; }
|
||||||
|
@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "..\Sus
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "..\SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "..\SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarListImplement", "..\SushiBarListImplement\SushiBarListImplement.csproj", "{99652DC5-71FC-4100-834F-4CD8D14A4AAA}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -33,6 +35,10 @@ Global
|
|||||||
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Release|Any CPU.Build.0 = Release|Any CPU
|
{5C6C02C5-88B1-4CCE-884D-415E40A94AD8}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{99652DC5-71FC-4100-834F-4CD8D14A4AAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{99652DC5-71FC-4100-834F-4CD8D14A4AAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{99652DC5-71FC-4100-834F-4CD8D14A4AAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{99652DC5-71FC-4100-834F-4CD8D14A4AAA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
139
SushiBarBusinessLogic/OrderLogic.cs
Normal file
139
SushiBarBusinessLogic/OrderLogic.cs
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.BusinessLogicsContracts;
|
||||||
|
using SushiBarContracts.SearchModel;
|
||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using SushiBarDataModels.Enums;
|
||||||
|
|
||||||
|
namespace SushiBarBusinessLogic.BusinessLogic
|
||||||
|
{
|
||||||
|
public class OrderLogic : IOrderLogic
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IOrderStorage _orderStorage;
|
||||||
|
|
||||||
|
public OrderLogic(ILogger<SushiLogic> logger, IOrderStorage orderStorage)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_orderStorage = orderStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
|
||||||
|
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
||||||
|
if (list == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadList return null list");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CreateOrder(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (model.Status == OrderStatus.Неизвестен) return false;
|
||||||
|
if (_orderStorage.Insert(model) == null)
|
||||||
|
{
|
||||||
|
model.Status = OrderStatus.Неизвестен;
|
||||||
|
_logger.LogWarning("Insert operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
model.Status = OrderStatus.Принят;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TakeOrderInWork(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
return ChangeStatus(model, OrderStatus.Выполняется);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool FinishOrder(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
return ChangeStatus(model, OrderStatus.Готов);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool DeliveryOrder(OrderBindingModel model)
|
||||||
|
{
|
||||||
|
return ChangeStatus(model, OrderStatus.Выдан);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool ChangeStatus(OrderBindingModel model, OrderStatus orderStatus)
|
||||||
|
{
|
||||||
|
CheckModel(model, false);
|
||||||
|
var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||||||
|
if (order == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Change status operation failed. Order not found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (order.Status + 1 != orderStatus)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Change status operation failed. Incorrect new status: {orderStatus}. Current status: {Status}",
|
||||||
|
orderStatus, order.Status);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
model.SushiId = order.SushiId;
|
||||||
|
model.Count = order.Count;
|
||||||
|
model.Sum = order.Sum;
|
||||||
|
model.DateCreate = order.DateCreate;
|
||||||
|
model.Status = orderStatus;
|
||||||
|
|
||||||
|
if (model.Status == OrderStatus.Готов)
|
||||||
|
{
|
||||||
|
model.DateImplement = DateTime.Now;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
model.DateImplement = order.DateImplement;
|
||||||
|
}
|
||||||
|
if (_orderStorage.Update(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Change status operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
|
||||||
|
if (!withParams) return;
|
||||||
|
|
||||||
|
if (model.Count < 0)
|
||||||
|
throw new ArgumentNullException("Количество суш в заказе не может быть меньше нуля", nameof(model.Count));
|
||||||
|
|
||||||
|
if (model.Sum < 0)
|
||||||
|
throw new ArgumentNullException("Стоимость заказа не может быть меньше нуля", nameof(model.Sum));
|
||||||
|
|
||||||
|
if (model.DateImplement.HasValue & model.DateImplement < model.DateCreate)
|
||||||
|
throw new ArithmeticException("Заказ должен быть выдан позже, чем был создан");
|
||||||
|
|
||||||
|
_logger.LogInformation("Sushi. SushiId:{SushiId}. Count:{ Count}. Sum:{ Sum}. Id: { Id}",
|
||||||
|
model.SushiId, model.Count, model.Sum, model.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public OrderViewModel? ReadElement(OrderSearchModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement. SushiName:{SushiName}.Id:{ Id}", model.SushiName, model.Id);
|
||||||
|
var element = _sushiStorage.GetElement(model);
|
||||||
|
if (element == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("ReadElement element not found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
||||||
|
return element;
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
@ -7,12 +7,12 @@ using Microsoft.Extensions.Logging;
|
|||||||
|
|
||||||
namespace SushiBarBusinessLogic.BusinessLogic
|
namespace SushiBarBusinessLogic.BusinessLogic
|
||||||
{
|
{
|
||||||
public class Sushi : ISushiLogic
|
public class SushiLogic : ISushiLogic
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly ISushiStorage _sushiStorage;
|
private readonly ISushiStorage _sushiStorage;
|
||||||
|
|
||||||
public Sushi(ILogger<Sushi> logger, ISushiStorage sushiStorage)
|
public SushiLogic(ILogger<SushiLogic> logger, ISushiStorage sushiStorage)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_sushiStorage = sushiStorage;
|
_sushiStorage = sushiStorage;
|
||||||
@ -95,13 +95,10 @@ namespace SushiBarBusinessLogic.BusinessLogic
|
|||||||
{
|
{
|
||||||
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
|
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
|
||||||
}
|
}
|
||||||
_logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id}",
|
_logger.LogInformation("Sushi. SushiName:{SushiName}. Price:{ Price}. Id: { Id}",
|
||||||
model.SushiName, model.Price, model.Id);
|
model.SushiName, model.Price, model.Id);
|
||||||
|
|
||||||
var element = _sushiStorage.GetElement(new SushiSearchModel
|
var element = _sushiStorage.GetElement(new SushiSearchModel{SushiName = model.SushiName});
|
||||||
{
|
|
||||||
SushiName = model.SushiName
|
|
||||||
});
|
|
||||||
|
|
||||||
if (element != null && element.Id != model.Id)
|
if (element != null && element.Id != model.Id)
|
||||||
{
|
{
|
@ -11,7 +11,7 @@ namespace SushiBarContracts.BindingModel
|
|||||||
public class OrderBindingModel : IOrderModel
|
public class OrderBindingModel : IOrderModel
|
||||||
{
|
{
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int ProductId { get; set; }
|
public int SushiId { get; set; }
|
||||||
public int Count { get; set; }
|
public int Count { get; set; }
|
||||||
public double Sum { get; set; }
|
public double Sum { get; set; }
|
||||||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||||||
|
@ -9,11 +9,11 @@ namespace SushiBarContracts.ViewModels
|
|||||||
{
|
{
|
||||||
[DisplayName("Номер")]
|
[DisplayName("Номер")]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int ProductId { get; set; }
|
public int SushiId { get; set; }
|
||||||
|
|
||||||
|
|
||||||
[DisplayName("Изделие")]
|
[DisplayName("Изделие")]
|
||||||
public string ProductName { get; set; } = string.Empty;
|
public string SushiName { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
|
||||||
[DisplayName("Количество")]
|
[DisplayName("Количество")]
|
||||||
|
41
SushiBarListImplement/Component.cs
Normal file
41
SushiBarListImplement/Component.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement.Models
|
||||||
|
{
|
||||||
|
public class Component : IComponentModel
|
||||||
|
{
|
||||||
|
public int Id { get; private set; }
|
||||||
|
public string ComponentName { get; private set; } = string.Empty;
|
||||||
|
public double Cost { get; set; }
|
||||||
|
public static Component? Create(ComponentBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new Component()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
ComponentName = model.ComponentName,
|
||||||
|
Cost = model.Cost
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void Update(ComponentBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ComponentName = model.ComponentName;
|
||||||
|
Cost = model.Cost;
|
||||||
|
}
|
||||||
|
public ComponentViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
ComponentName = ComponentName,
|
||||||
|
Cost = Cost
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
101
SushiBarListImplement/ComponentStorage.cs
Normal file
101
SushiBarListImplement/ComponentStorage.cs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.SearchModel;
|
||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarListImplement.Models;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement.Implements
|
||||||
|
{
|
||||||
|
public class ComponentStorage : IComponentStorage
|
||||||
|
{
|
||||||
|
private readonly DataListSingleton _source;
|
||||||
|
public ComponentStorage()
|
||||||
|
{
|
||||||
|
_source = DataListSingleton.GetInstance();
|
||||||
|
}
|
||||||
|
public List<ComponentViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
var result = new List<ComponentViewModel>();
|
||||||
|
foreach (var component in _source.Components)
|
||||||
|
{
|
||||||
|
result.Add(component.GetViewModel);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
||||||
|
{
|
||||||
|
var result = new List<ComponentViewModel>();
|
||||||
|
if (string.IsNullOrEmpty(model.ComponentName))
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
foreach (var component in _source.Components)
|
||||||
|
{
|
||||||
|
if (component.ComponentName.Contains(model.ComponentName))
|
||||||
|
{
|
||||||
|
result.Add(component.GetViewModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
foreach (var component in _source.Components)
|
||||||
|
{
|
||||||
|
if ((!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName) ||
|
||||||
|
(model.Id.HasValue && component.Id == model.Id))
|
||||||
|
{
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Insert(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
model.Id = 1;
|
||||||
|
foreach (var component in _source.Components)
|
||||||
|
{
|
||||||
|
if (model.Id <= component.Id)
|
||||||
|
{
|
||||||
|
model.Id = component.Id + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var newComponent = Component.Create(model);
|
||||||
|
if (newComponent == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_source.Components.Add(newComponent);
|
||||||
|
return newComponent.GetViewModel;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
foreach (var component in _source.Components)
|
||||||
|
{
|
||||||
|
if (component.Id == model.Id)
|
||||||
|
{
|
||||||
|
component.Update(model);
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _source.Components.Count; ++i)
|
||||||
|
{
|
||||||
|
if (_source.Components[i].Id == model.Id)
|
||||||
|
{
|
||||||
|
var element = _source.Components[i];
|
||||||
|
_source.Components.RemoveAt(i);
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
SushiBarListImplement/DataListSingleton.cs
Normal file
27
SushiBarListImplement/DataListSingleton.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using SushiBarListImplement.Models;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement
|
||||||
|
{
|
||||||
|
public class DataListSingleton
|
||||||
|
{
|
||||||
|
private static DataListSingleton? _instance;
|
||||||
|
public List<Component> Components { get; set; }
|
||||||
|
public List<Order> Orders { get; set; }
|
||||||
|
public List<Sushi> Sushis { get; set; }
|
||||||
|
private DataListSingleton()
|
||||||
|
{
|
||||||
|
Components = new List<Component>();
|
||||||
|
Orders = new List<Order>();
|
||||||
|
Sushis = new List<Sushi>();
|
||||||
|
}
|
||||||
|
public static DataListSingleton GetInstance()
|
||||||
|
{
|
||||||
|
if (_instance == null)
|
||||||
|
{
|
||||||
|
_instance = new DataListSingleton();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
63
SushiBarListImplement/Order.cs
Normal file
63
SushiBarListImplement/Order.cs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDataModels.Models;
|
||||||
|
using SushiBarDataModels;
|
||||||
|
using SushiBarDataModels.Enums;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement.Models
|
||||||
|
{
|
||||||
|
public class Order : IOrderModel
|
||||||
|
{
|
||||||
|
public int Id { get; private set; }
|
||||||
|
public int SushiId { get; private set; }
|
||||||
|
public int Count { get; private set; }
|
||||||
|
public double Sum { get; private set; }
|
||||||
|
public OrderStatus Status { get; private set; }
|
||||||
|
public DateTime DateCreate { get; private set; }
|
||||||
|
public DateTime? DateImplement { get; private set; }
|
||||||
|
|
||||||
|
public static Order? Create(OrderBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new Order()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
SushiId = model.SushiId,
|
||||||
|
Count = model.Count,
|
||||||
|
Sum = model.Sum,
|
||||||
|
Status = model.Status,
|
||||||
|
DateCreate = model.DateCreate,
|
||||||
|
DateImplement = model.DateImplement
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(OrderBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SushiId = model.SushiId;
|
||||||
|
Count = model.Count;
|
||||||
|
Sum = model.Sum;
|
||||||
|
Status = model.Status;
|
||||||
|
DateCreate = model.DateCreate;
|
||||||
|
DateImplement = model.DateImplement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrderViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
SushiId = SushiId,
|
||||||
|
Count = Count,
|
||||||
|
Sum = Sum,
|
||||||
|
Status = Status,
|
||||||
|
DateCreate = DateCreate,
|
||||||
|
DateImplement = DateImplement
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
49
SushiBarListImplement/Sushi.cs
Normal file
49
SushiBarListImplement/Sushi.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarDataModels.Models;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement.Models
|
||||||
|
{
|
||||||
|
public class Sushi : ISushiModel
|
||||||
|
{
|
||||||
|
public int Id { get; private set; }
|
||||||
|
public string SushiName { get; private set; } = string.Empty;
|
||||||
|
public double Price { get; private set; }
|
||||||
|
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
} = new Dictionary<int, (IComponentModel, int)>();
|
||||||
|
public static Sushi? Create(SushiBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new Sushi()
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
SushiName = model.SushiName,
|
||||||
|
Price = model.Price,
|
||||||
|
SushiComponents = model.SushiComponents
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public void Update(SushiBindingModel? model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SushiName = model.SushiName;
|
||||||
|
Price = model.Price;
|
||||||
|
SushiComponents = model.SushiComponents;
|
||||||
|
}
|
||||||
|
public SushiViewModel GetViewModel => new()
|
||||||
|
{
|
||||||
|
Id = Id,
|
||||||
|
SushiName = SushiName,
|
||||||
|
Price = Price,
|
||||||
|
SushiComponents = SushiComponents
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
14
SushiBarListImplement/SushiBarListImplement.csproj
Normal file
14
SushiBarListImplement/SushiBarListImplement.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\ClassLibrary1SushiBarDataModels\SushiBarDataModels.csproj" />
|
||||||
|
<ProjectReference Include="..\SushiBarContracts\SushiBarContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
108
SushiBarListImplement/SushiStorage.cs
Normal file
108
SushiBarListImplement/SushiStorage.cs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
using SushiBarContracts.BindingModel;
|
||||||
|
using SushiBarContracts.SearchModel;
|
||||||
|
using SushiBarContracts.StoragesContracts;
|
||||||
|
using SushiBarContracts.ViewModels;
|
||||||
|
using SushiBarListImplement.Models;
|
||||||
|
|
||||||
|
namespace SushiBarListImplement.Implements
|
||||||
|
{
|
||||||
|
public class SushiStorage : ISushiStorage
|
||||||
|
{
|
||||||
|
private DataListSingleton _source;
|
||||||
|
|
||||||
|
public SushiStorage()
|
||||||
|
{
|
||||||
|
_source = DataListSingleton.GetInstance();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SushiViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
var result = new List<SushiViewModel>();
|
||||||
|
foreach (var component in _source.Sushis)
|
||||||
|
{
|
||||||
|
result.Add(component.GetViewModel);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
|
||||||
|
{
|
||||||
|
var result = new List<SushiViewModel>();
|
||||||
|
if (string.IsNullOrEmpty(model.SushiName))
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
foreach (var sushi in _source.Sushis)
|
||||||
|
{
|
||||||
|
if (sushi.SushiName.Contains(model.SushiName))
|
||||||
|
{
|
||||||
|
result.Add(sushi.GetViewModel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SushiViewModel? GetElement(SushiSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
foreach (var sushi in _source.Sushis)
|
||||||
|
{
|
||||||
|
if ((!string.IsNullOrEmpty(model.SushiName) && sushi.SushiName == model.SushiName) ||
|
||||||
|
(model.Id.HasValue && sushi.Id == model.Id))
|
||||||
|
{
|
||||||
|
return sushi.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SushiViewModel? Insert(SushiBindingModel model)
|
||||||
|
{
|
||||||
|
model.Id = 1;
|
||||||
|
foreach (var sushi in _source.Sushis)
|
||||||
|
{
|
||||||
|
if (model.Id <= sushi.Id)
|
||||||
|
{
|
||||||
|
model.Id = sushi.Id + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var newSushi = Sushi.Create(model);
|
||||||
|
if (newSushi == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
_source.Sushis.Add(newSushi);
|
||||||
|
return newSushi.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SushiViewModel? Update(SushiBindingModel model)
|
||||||
|
{
|
||||||
|
foreach (var sushi in _source.Sushis)
|
||||||
|
{
|
||||||
|
if (sushi.Id == model.Id)
|
||||||
|
{
|
||||||
|
sushi.Update(model);
|
||||||
|
return sushi.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SushiViewModel? Delete(SushiBindingModel model)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _source.Sushis.Count; ++i)
|
||||||
|
{
|
||||||
|
if (_source.Sushis[i].Id == model.Id)
|
||||||
|
{
|
||||||
|
var element = _source.Sushis[i];
|
||||||
|
_source.Sushis.RemoveAt(i);
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user