Business logic 0.2

This commit is contained in:
Илья Федотов 2024-04-29 01:28:41 +04:00
parent 02d78da38f
commit 134f7d6cef
9 changed files with 172 additions and 105 deletions

View File

@ -1,6 +1,7 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
@ -14,18 +15,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public class CategoryProductLogic : ICategoryProductLogic
{
private readonly ILogger _logger;
//private readonly ICategoryProductStorage _storage;
private readonly ICategoryProductStorage _storage;
// todo нет интерфейса хранилища
public CategoryProductLogic(ILogger<CategoryProductLogic> logger) {
public CategoryProductLogic(ILogger<CategoryProductLogic> logger, ICategoryProductStorage storage) {
_logger = logger;
_storage = storage;
}
public bool Create(CategoryProductBindingModel model)
{
CheckModel(model);
// todo _storage.Insert(model) == null
if (model == null) {
if (_storage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
@ -36,8 +36,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
// todo _storage.Insert(model) == null
if (model == null) {
if (_storage.Insert(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
@ -46,8 +45,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public bool Update(CategoryProductBindingModel model)
{
CheckModel(model);
// todo _storage.Insert(model) == null
if (model == null) {
if (_storage.Insert(model) == null) {
_logger.LogWarning("Update operation failed");
return false;
}
@ -57,16 +55,13 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public List<CategoryProductViewModel>? ReadList(CategoryProductSearchModel? model)
{
_logger.LogInformation($"ReadList. ID:{model?.ID}");
// todo model == null _CategoryProductStorage.GetFullList() : _CategoryProductStorage.GetFilteredList(model);
var list = model;
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
// todo $"ReadList. Count:{list.count}"
_logger.LogInformation("ReadList. Count:{Count}");
// todo return list;
return null;
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
}
private void CheckModel(CategoryProductBindingModel model, bool withParams = true) {
@ -79,17 +74,16 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
if (string.IsNullOrEmpty(model.Name)) {
throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name));
}
_logger.LogInformation($"CategoryProduct. Name:{model.Name}");
/*
var element = _ CategoryProductStorage.GetElement(new CategoryProductSearchModel
_logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}");
var element = _storage.GetElement(new CategoryProductSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.ID)
{
if (element != null && element.ID != model.ID) {
throw new InvalidOperationException("Категория с таким названием уже есть");
}
*/
}
}
}

View File

@ -16,19 +16,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
//private readonly IOrderStorage _storage;
private readonly IOrderStorage _storage;
// todo нет интерфейса хранилища
public OrderLogic(ILogger<OrderLogic> logger) {
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage storage) {
_logger = logger;
//storage = _storage;
_storage = storage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
model.Status = OrderStatus.Принят;
// todo _orderStorage.Insert(model) == null
if (model == null) {
if (_storage.Insert(model) == null) {
_logger.LogInformation("Insert operation failed");
return false;
}
@ -53,16 +51,13 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation($"ReadList:ID:{model?.ID}");
// todo model == null? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
var list = model;
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
if (list == null) {
_logger.LogWarning("ReadList return null list");
return null;
}
// todo "$ReadList.Count:{list.count}"
_logger.LogInformation("ReadList.Count:{Count}");
//todo return list
return null;
_logger.LogInformation($"ReadList.Count:{list.Count}");
return list;
}
private void CheckModel(OrderBindingModel model, bool withParams = true) {
@ -79,37 +74,27 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.Status:{model.Status}.PaymeantOption:{model.PaymeantOption}." +
$"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplemet}");
$"DateCreate:{model.DateCreate}.DataImplement:{model.DateImplement}");
}
private bool StatusUpdate(OrderBindingModel model, OrderStatus newOrderStatus)
{
CheckModel(model, false);
//todo
var viewModel = model; // model == _orderStorage.GetElement(new OrderSearchModel { ID = model.ID });
if (viewModel == null)
{
var viewModel = _storage.GetElement(new OrderSearchModel { ID = model.ID });
if (viewModel == null) {
throw new ArgumentNullException(nameof(model));
}
if (viewModel.Status + 1 != newOrderStatus)
{
if (viewModel.Status + 1 != newOrderStatus) {
_logger.LogWarning("Status update to " + newOrderStatus.ToString() + " operation failed.");
return false;
}
model.Status = newOrderStatus;
if (model.Status == OrderStatus.Готов)
{
//todo подключить бд
//model.DateImplement = DateTime.Now;
if (model.Status == OrderStatus.Готов) {
model.DateImplement = DateTime.Now;
}
else
{
// todo подключить бд
//model.DateImplement = viewModel.DateImplement;
else {
model.DateImplement = viewModel.DateImplement;
}
// todo
// _orderStorage.Update(model) == null
if (model == null)
{
if (_storage.Update(model) == null) {
_logger.LogWarning("Update operarion failed");
return false;
}

View File

@ -0,0 +1,99 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopBusinessLogic.BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _storage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage) {
_logger = logger;
_storage = productStorage;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_storage.Insert(model) == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
if (_storage.Delete(model) == null) {
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_storage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public ProductViewModel? ReadElement(ProductSearchModel? model)
{
if (model == null) throw new ArgumentNullException(nameof(model));
_logger.LogInformation($"ReadElement. ProductName:{model.ProductName}. ID:{model.ID}");
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement. elementn not found");
return null;
}
_logger.LogInformation($"ReadElement find. ID:{element.ID}");
return element;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
_logger.LogInformation($"ReadList. ProductName:{model?.ProductName}. ID:{model?.ID}");
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
}
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null) throw new ArgumentNullException(nameof(model));
if (!withParams) return;
if (string.IsNullOrEmpty(model.ProductName))
throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName));
if (model.Price <= 0)
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
_logger.LogInformation($"Product. ID:{model.ID}.ProductName:{model.ProductName}.Price:{model.Price}.Count:{model.Count}" +
$".CategoryID:{model.CategoryID}");
var element = _storage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
if (element != null && element.ID != model.ID)
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}

View File

@ -1,6 +1,7 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
@ -14,19 +15,18 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public class RoleLogic : IRoleLogic
{
private readonly ILogger _logger;
//private readonly ICategoryProductStorage _storage;
private readonly IRoleStorage _storage;
// todo нет интерфейса хранилища
public RoleLogic(ILogger<RoleLogic> logger)
public RoleLogic(ILogger<RoleLogic> logger, IRoleStorage storage)
{
_logger = logger;
_storage = storage;
}
public bool Create(RoleBindingModel model)
{
CheckModel(model);
// todo _storage.Insert(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
@ -38,8 +38,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
CheckModel(model, false);
_logger.LogInformation($"Delete. ID:{model.ID}");
// todo _storage.Insert(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
@ -49,8 +48,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public bool Update(RoleBindingModel model)
{
CheckModel(model);
// todo _storage.Insert(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
@ -61,17 +59,14 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public List<RoleViewModel>? ReadList(RoleSearchModel? model)
{
_logger.LogInformation($"ReadList. ID:{model?.ID}");
// todo model == null _RoleStorage.GetFullList() : _RoleStorage.GetFilteredList(model);
var list = model;
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
// todo $"ReadList. Count:{list.count}"
_logger.LogInformation("ReadList. Count:{Count}");
// todo return list;
return null;
_logger.LogInformation($"ReadList. Count:{list.Count}");
return list;
}
private void CheckModel(RoleBindingModel model, bool withParams = true)
@ -88,17 +83,17 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
throw new ArgumentNullException("Нет названия категории продукта", nameof(model.Name));
}
_logger.LogInformation($"CategoryProduct. Name:{model.Name}");
/*
var element = _RoleStorage.GetElement(new RoleSearchModel
_logger.LogInformation($"CategoryProduct. ID:{model.ID}.Name:{model.Name}");
var element = _storage.GetElement(new RoleSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.ID)
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Такая роль уже есть");
}
*/
}
}
}

View File

@ -1,6 +1,7 @@
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
@ -14,20 +15,19 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
internal class UserLogic : IUserLogic
{
private readonly ILogger _logger;
//private readonly IClientStorage _storage;
private readonly IUserStorage _storage;
// todo нет интерфейса хранилища
public UserLogic(ILogger<UserLogic> logger)
public UserLogic(ILogger<UserLogic> logger, IUserStorage storage)
{
_logger = logger;
//storage = _storage;
_storage = storage;
}
public bool Add(UserBindingModel model)
{
CheckModel(model);
// todo логика добавления в _clientStorage:_clientStorage.Insert(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Add operation failed");
return false;
@ -38,8 +38,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
public bool Update(UserBindingModel model)
{
CheckModel(model);
// todo логика добавления в _clientStorage:_clientStorage.Update(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
@ -51,8 +50,7 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
CheckModel(model, false);
_logger.LogInformation($"Delete.ID:{model.ID}");
// todo логика добавления в _clientStorage:_clientStorage.Delete(model) == null
if (model == null)
if (_storage.Insert(model) == null)
{
_logger.LogWarning("Delete operation failes");
return false;
@ -68,33 +66,28 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation($"ReadElement: logint:{model.Login}.ID:{model.ID}");
// todo element = _clientStorage.GetElement(model);
var element = model;
var element = _storage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not fount");
return null;
}
_logger.LogInformation($"ReadElement: find.ID:{element.ID}");
// todo retun element;
return null;
return element;
}
public List<UserViewModel>? ReadList(UserSearchModel? model)
{
_logger.LogInformation($"ReadList: ClientID:{model?.ID}");
// todo получение списка из хранилища, model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
var list = model;
var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ;
if (list == null)
{
_logger.LogWarning("ReadList: return null list");
return null;
}
// toto ReadList:Count:{list.count},
_logger.LogInformation("ReadList:Count:{Count}");
// todo return list;
return null;
_logger.LogInformation($"ReadList:Count:{list.Count}");
return list;
}
private void CheckModel(UserBindingModel model, bool withParams = true)
@ -131,18 +124,19 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
{
throw new ArgumentNullException("Нет номер телефона пользователя", nameof(model.PhoneNumber));
}
_logger.LogInformation($"Client. Login:{model.Login}.FirstName:{model.FirstName}.LastName:{model.LastName}.Email:{model.Email}." +
$"Password:{model.Password}.PhoneNumber:{model.PhoneNumber}");
/*
var element = _clientStorage.GetElement(new ClientSearchModel
_logger.LogInformation($"Client. ID:{model.ID}.RoleID:{model.RoleID}.FirstName:{model.FirstName}." +
$"LastName:{model.LastName}.Password:{model.Password}.PhoneNumber:{model.PhoneNumber}.Login:{model.Login}." +
$"Email:{model.Email}");
var element = _storage.GetElement(new UserSearchModel
{
Login = model.Login
});
if (element != null && element.Id != model.ID)
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Клиент с таким логином уже есть");
}
*/
}
}
}

View File

@ -20,7 +20,7 @@ namespace ElectronicsShopContracts.BindingModels
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplemet { get; set; }
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();

View File

@ -12,10 +12,10 @@ namespace ElectronicsShopContracts.StorageContracts
public interface IProductStorage
{
List<ProductViewModel> GetFullList();
List<ProductViewModel> GetFilteredList(OrderSearchModel model);
ProductViewModel? GetElement(OrderSearchModel model);
ProductViewModel? Insert(OrderBindingModel model);
ProductViewModel? Update(OrderBindingModel model);
ProductViewModel? Delete(OrderBindingModel model);
List<ProductViewModel> GetFilteredList(ProductSearchModel model);
ProductViewModel? GetElement(ProductSearchModel model);
ProductViewModel? Insert(ProductBindingModel model);
ProductViewModel? Update(ProductBindingModel model);
ProductViewModel? Delete(ProductBindingModel model);
}
}

View File

@ -26,7 +26,7 @@ namespace ElectronicsShopContracts.ViewModels
public DateTime DateCreate { get; set; } = DateTime.Now;
[DisplayName("Дата выполнения")]
public DateTime? DateImplemet { get; set; }
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IProductModel, int)> ProductList { get; set; } = new();
}

View File

@ -13,7 +13,7 @@ namespace ElectronicsShopDataModels.Models
OrderStatus Status { get; }
PaymeantOption PaymeantOption { get; }
DateTime DateCreate { get; }
DateTime? DateImplemet { get; }
DateTime? DateImplement { get; }
//список товаров в заказе
Dictionary<int, (IProductModel, int)> ProductList { get; }
}