Добавление ListImplement

This commit is contained in:
Ino 2023-01-30 15:02:38 +04:00
parent 310ce2d3a6
commit cb970c1ad7
12 changed files with 575 additions and 16 deletions

View File

@ -89,7 +89,7 @@ namespace IceCreamBusinessLogic.BusinessLogics
return true;
}
public void CheckModel(IceCreamBindingModel model, bool withParams = true)
private void CheckModel(IceCreamBindingModel model, bool withParams = true)
{
if(model == null)
{
@ -105,15 +105,14 @@ namespace IceCreamBusinessLogic.BusinessLogics
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("IceCream. IceCreamName:{IceCreamName}. Price:{Price}. Id: { Id}", model.IceCreamName, model.Price, model.Id);
var element = _iceCreamStorage.GetElement(new IceCreamSearchModel { IceCreamName = model.IceCreamName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Изделие с таким названием уже есть");
throw new InvalidOperationException("Изделие с таким названием уже есть");
}
}
}
}

View File

@ -13,28 +13,33 @@ using AbstractIceCreamShopDataModels.Enums;
namespace IceCreamBusinessLogic.BusinessLogics
{
internal class OrderLogic : IOrderLogic
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(IOrderStorage orderStorage)
public OrderLogic(IOrderStorage orderStorage, ILogger<OrderLogic> logger)
{
_orderStorage = orderStorage;
_logger = logger;
}
public bool CreateOrder(OrderBindingModel model)
{
_orderStorage.Insert(new OrderBindingModel
CheckModel(model);
if(model.Status != OrderStatus.Неизвестен)
{
IceCreamId = model.IceCreamId,
Count = model.Count,
Sum = model.Sum,
Status = OrderStatus.Принят,
DateCreate = DateTime.Now,
DateImplement = model.DateImplement
});
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Неизвестен;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
@ -56,5 +61,30 @@ namespace IceCreamBusinessLogic.BusinessLogics
{
throw new NotImplementedException();
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.IceCreamId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.IceCreamId));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. DocumentId: { DocumentId}", model.Id, model.Sum, model.IceCreamId);
}
}
}

View File

@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShopContracts", "Ic
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamBusinessLogic", "IceCreamBusinessLogic\IceCreamBusinessLogic.csproj", "{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4C64BCD-22DF-43B5-8324-AE64F9237DFF}.Release|Any CPU.Build.0 = Release|Any CPU
{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBB0BAF6-8DEE-42EF-8AF5-571B4E0DE95E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCreamShopListImplement.Models;
namespace IceCreamShopListImplement
{
public class DataListSingleton
{
private static DataListSingleton? _instance;
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<IceCream> iceCreams { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
iceCreams = new List<IceCream>();
}
public static DataListSingleton GetInstance()
{
if (_instance == null)
{
_instance = new DataListSingleton();
}
return _instance;
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\IceCreamShopContracts\IceCreamShopContracts.csproj" />
<ProjectReference Include="..\IceCreamShopDataModels\IceCreamShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopListImplement.Models;
namespace IceCreamShopListImplement.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;
}
}
}

View File

@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopListImplement.Models;
namespace IceCreamShopListImplement.Implements
{
public class IceCreamStorage : IIceCreamStorage
{
private readonly DataListSingleton _source;
public IceCreamStorage()
{
_source = DataListSingleton.GetInstance();
}
public IceCreamViewModel? Delete(IceCreamBindingModel model)
{
for (int i = 0; i < _source.iceCreams.Count; ++i)
{
if (_source.iceCreams[i].Id == model.Id)
{
var element = _source.iceCreams[i];
_source.iceCreams.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public IceCreamViewModel? GetElement(IceCreamSearchModel model)
{
if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) return null;
foreach (var iceCream in _source.iceCreams)
{
if ((!string.IsNullOrEmpty(model.IceCreamName) && iceCream.IceCreamName == model.IceCreamName) ||
(model.Id.HasValue && iceCream.Id == model.Id))
{
return iceCream.GetViewModel;
}
}
return null;
}
public List<IceCreamViewModel> GetFilteredList(IceCreamSearchModel model)
{
var result = new List<IceCreamViewModel>();
if (string.IsNullOrEmpty(model.IceCreamName)) return result;
foreach(var iceCream in _source.iceCreams)
{
if (iceCream.IceCreamName.Contains(model.IceCreamName))
result.Add(iceCream.GetViewModel);
}
return result;
}
public List<IceCreamViewModel> GetFullList()
{
var result = new List<IceCreamViewModel>();
foreach (var iceCream in _source.iceCreams) result.Add(iceCream.GetViewModel);
return result;
}
public IceCreamViewModel? Insert(IceCreamBindingModel model)
{
model.Id = 1;
foreach(var iceCream in _source.iceCreams)
{
if(model.Id <= iceCream.Id) model.Id = iceCream.Id + 1;
}
var newIceCream = IceCream.Create(model);
if (newIceCream == null) return null;
_source.iceCreams.Add(newIceCream);
return newIceCream.GetViewModel;
}
public IceCreamViewModel? Update(IceCreamBindingModel model)
{
foreach (var iceCream in _source.iceCreams)
{
if (iceCream.Id == model.Id)
{
iceCream.Update(model);
return iceCream.GetViewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,103 @@
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IceCreamShopListImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataListSingleton _source;
public OrderStorage()
{
_source = DataListSingleton.GetInstance();
}
public OrderViewModel? Delete(OrderBindingModel model)
{
for (int i = 0; i < _source.Orders.Count; ++i)
{
if (_source.Orders[i].Id == model.Id)
{
var element = _source.Orders[i];
_source.Orders.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue) return null;
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
return order.GetViewModel;
}
return null;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.Id.HasValue) return result;
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
result.Add(order.GetViewModel);
}
return result;
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(order.GetViewModel);
}
return result;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = 1;
foreach (var order in _source.Orders)
{
if (model.Id <= order.Id)
{
model.Id = order.Id + 1;
}
}
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
order.Update(model);
return order.GetViewModel;
}
}
return null;
}
}
}

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models;
namespace IceCreamShopListImplement.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
};
}
}

View File

@ -0,0 +1,55 @@
using AbstractIceCreamShopDataModels.Models;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IceCreamShopListImplement.Models
{
public class IceCream : IIceCreamModel
{
public int Id { get; private set; }
public string IceCreamName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, (IComponentModel, int)> IceCreamComponents
{
get;
private set;
} = new Dictionary<int, (IComponentModel, int)>();
public static IceCream? Create(IceCreamBindingModel? model)
{
if (model == null)
{
return null;
}
return new IceCream()
{
Id = model.Id,
IceCreamName = model.IceCreamName,
Price = model.Price,
IceCreamComponents = model.IceCreamComponents
};
}
public void Update(IceCreamBindingModel? model)
{
if (model == null)
{
return;
}
IceCreamName = model.IceCreamName;
Price = model.Price;
IceCreamComponents = model.IceCreamComponents;
}
public IceCreamViewModel GetViewModel => new()
{
Id = Id,
IceCreamName = IceCreamName,
Price = Price,
IceCreamComponents = IceCreamComponents
};
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using AbstractIceCreamShopDataModels.Models;
using AbstractIceCreamShopDataModels.Enums;
using System.Data;
namespace IceCreamShopListImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int IceCreamId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order? Create (OrderBindingModel? model)
{
if (model == null) return null;
return new Order
{
Id = model.Id,
IceCreamId = model.IceCreamId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null) return;
Id = model.Id;
IceCreamId = model.IceCreamId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
IceCreamId = IceCreamId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}