Contractor: BusinessLogic and Fixes in Contracts and DataModels

This commit is contained in:
Safgerd 2023-04-07 13:10:34 +04:00
parent a31e821be3
commit ff3957a457
18 changed files with 507 additions and 43 deletions

View File

@ -0,0 +1,105 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class ConsignmentLogic : IConsignmentLogic
{
private readonly ILogger _logger;
private readonly IConsignmentStorage _consignmentStorage;
public ConsignmentLogic(ILogger<ConsignmentLogic> logger, IConsignmentStorage consignmentStorage)
{
_logger = logger;
_consignmentStorage = consignmentStorage;
}
public bool Create(ConsignmentBindingModel model)
{
CheckModel(model);
if (_consignmentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ConsignmentBindingModel model)
{
CheckModel(model);
if (_consignmentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ConsignmentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_consignmentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ConsignmentViewModel? ReadElement(ConsignmentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Consignment ID:{ ID}", model.ID);
var element = _consignmentStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Consignment ID:{ID}", element.ID);
return element;
}
public List<ConsignmentViewModel>? ReadList(ConsignmentSearchModel? model)
{
_logger.LogInformation("ReadList. Consignment ID:{ ID}", model?.ID);
var list = model == null ? _consignmentStorage.GetFullList() : _consignmentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(ConsignmentBindingModel model, bool withParams = true)
{
if (model == null) { return; }
if (!withParams) { return; }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid consignment's price", nameof(model)); }
if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Consignment's order ID", nameof(model)); }
_logger.LogInformation("Consignment. Consignment ID:{ ID}. Order ID:{ OrderID}.", model.ID, model.OrderID);
}
}
}

View File

@ -0,0 +1,148 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Unknown)
{
_logger.LogWarning("Insert operation failed. Order status incorrect.");
return false;
}
model.Status = OrderStatus.Accepted;
if (_orderStorage.Insert(model) == null)
{
model.Status = OrderStatus.Unknown;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus)
{
var viewModel = _orderStorage.GetElement(new OrderSearchModel
{
ID = rawModel.ID
});
if (viewModel == null)
{
_logger.LogWarning("Order model not found");
return false;
}
OrderBindingModel model = new OrderBindingModel
{
ID = viewModel.ID,
ConsignmentID = viewModel.ConsignmentID,
RequestID = viewModel.RequestID,
Status = viewModel.Status,
DateCreate = viewModel.DateCreate,
DateImplement = viewModel.DateImplement,
Price = viewModel.Price
};
CheckModel(model, false);
if (model.Status + 1 != newStatus)
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Status = newStatus;
if (model.Status == OrderStatus.Given) model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
model.Status--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInProcess(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.InProcess);
}
public bool TakeOrderReady(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Ready);
}
public bool TakeOrderGiven(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Given);
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("Order. OrderID:{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;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.RequestID.ToString()) && string.IsNullOrEmpty(model.ConsignmentID.ToString()))
{
throw new ArgumentNullException("Incorrect consignment or request ID", nameof(model.ConsignmentID));
}
if (string.IsNullOrEmpty(model.RequestID.ToString()) && model.ConsignmentID < 0)
{
throw new ArgumentNullException("Incorrect consignment ID", nameof(model.ConsignmentID));
}
if (string.IsNullOrEmpty(model.ConsignmentID.ToString()) && model.RequestID < 0)
{
throw new ArgumentNullException("Incorrect request ID", nameof(model.RequestID));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Incorrect Price", nameof(model.Price));
}
if (string.IsNullOrEmpty(model.SellerID.ToString()) && model.SellerID < 0)
{
throw new ArgumentNullException("Incorrect seller ID", nameof(model.SellerID));
}
_logger.LogInformation("Order. OrderID:{ ID}.RequestID:{ RequestID}. ConsignmentID: { ConsignmentID}. SellerID: { SellerID}", model.ID, model.RequestID, model.ConsignmentID, model.SellerID);
}
}
}

View File

@ -0,0 +1,105 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class RequestLogic : IRequestLogic
{
private readonly ILogger _logger;
private readonly IRequestStorage _requestStorage;
public RequestLogic(ILogger<RequestLogic> logger, IRequestStorage requestStorage)
{
_logger = logger;
_requestStorage = requestStorage;
}
public bool Create(RequestBindingModel model)
{
CheckModel(model);
if (_requestStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(RequestBindingModel model)
{
CheckModel(model);
if (_requestStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(RequestBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_requestStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public RequestViewModel? ReadElement(RequestSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Request ID:{ ID}. Order ID:{ OrderID}", model.ID, model.OrderID);
var element = _requestStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<RequestViewModel>? ReadList(RequestSearchModel? model)
{
_logger.LogInformation("ReadList. Request ID:{ ID}. Order ID: { OrderID}", model?.ID, model?.OrderID);
var list = model == null ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(RequestBindingModel model, bool withParams = true)
{
if (model == null) { return; }
if (!withParams) { return; }
if (model.Price <= 0) { throw new ArgumentNullException("Invalid request's price", nameof(model)); }
if (string.IsNullOrEmpty(model.OrderID.ToString())) { throw new ArgumentNullException("Invalid Request's order ID", nameof(model)); }
_logger.LogInformation("Request. Request ID:{ ID}. Order ID: { OrderID}", model.ID, model.OrderID);
}
}
}

View File

@ -0,0 +1,122 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreBusinessLogic.BusinessLogic
{
public class SellerLogic : ISellerLogic
{
private readonly ILogger _logger;
private readonly ISellerStorage _sellerStorage;
public SellerLogic(ILogger<SellerLogic> logger, ISellerStorage sellerStorage)
{
_logger = logger;
_sellerStorage = sellerStorage;
}
public bool Create(SellerBindingModel model)
{
CheckSeller(model);
if (_sellerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(SellerBindingModel model)
{
CheckSeller(model);
if (_sellerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(SellerBindingModel model)
{
CheckSeller(model, false);
_logger.LogInformation("Delete. ID:{ID}", model.ID);
if (_sellerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public SellerViewModel? ReadElement(SellerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Username:{Username}.ID:{ ID}", model.Username, model.ID);
var element = _sellerStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. ID:{ID}", element.ID);
return element;
}
public List<SellerViewModel>? ReadList(SellerSearchModel? model)
{
_logger.LogInformation("ReadList. Username:{Username}. ID:{ ID}", model?.Username, model?.ID);
var list = model == null ? _sellerStorage.GetFullList() : _sellerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public void CheckSeller(SellerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Username))
{
throw new ArgumentNullException("Invalid username of user", nameof(model.Username));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Invalid password of user", nameof(model.Password));
}
_logger.LogInformation("Client. Username:{ Username}. ID: { ID} ", model.Username, model.ID);
var element = _sellerStorage.GetElement(new SellerSearchModel
{
Username = model.Username
});
if (element != null && element.ID != model.ID)
{
throw new InvalidOperationException("Seller with such username already exists.");
}
}
}
}

View File

@ -9,9 +9,8 @@ namespace ComputerStoreContracts.BindingModels
{
public class ConsignmentBindingModel : IConsignmentModel
{
public int ID { get; set; }
public int OrderID { get; set; }
public int ProductID { get; set; }
public double Price { get; set; }
public int Count { get; set; }
}
}

View File

@ -12,7 +12,8 @@ namespace ComputerStoreContracts.BindingModels
{
public int ID { get; set; }
public double Price { get; set; }
public OrderType Type { get; set; } = OrderType.Unknown;
public int? ConsignmentID { get; set; }
public int? RequestID { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Unknown;
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }

View File

@ -12,6 +12,5 @@ namespace ComputerStoreContracts.BindingModels
public int ID { get; set; }
public int OrderID { get; set; }
public double Price { get; set; }
public int Count { get; set; }
}
}

View File

@ -12,9 +12,9 @@ namespace ComputerStoreContracts.BusinessLogicContracts
public interface IOrderLogic
{
List<OrderViewModel>? ReadList(OrderSearchModel? model);
OrderViewModel? ReadElement(OrderSearchModel model);
bool Create(OrderBindingModel model);
bool Update(OrderBindingModel model);
bool Delete(OrderBindingModel model);
bool CreateOrder(OrderBindingModel model);
bool TakeOrderInProcess(OrderBindingModel model);
bool TakeOrderReady(OrderBindingModel model);
bool TakeOrderGiven(OrderBindingModel model);
}
}

View File

@ -8,6 +8,7 @@ namespace ComputerStoreContracts.SearchModels
{
public class ConsignmentSearchModel
{
public int? ID { get; set; }
public int? OrderID { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using System;
using ComputerStoreDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -9,6 +10,8 @@ namespace ComputerStoreContracts.SearchModels
public class OrderSearchModel
{
public int? ID { get; set; }
public int? ConsignmentID { get; set; }
public int? RequestID { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? SellerID { get; set; }

View File

@ -10,13 +10,11 @@ namespace ComputerStoreContracts.ViewModels
{
public class ConsignmentViewModel : IConsignmentModel
{
[DisplayName("Consignment ID")]
public int ID { get; set; }
[DisplayName("Order ID")]
public int OrderID { get; }
[DisplayName("Product ID")]
public int ProductID { get; }
public int OrderID { get; set; }
[DisplayName("Price")]
public double Price { get; }
[DisplayName("Count")]
public int Count { get; }
public double Price { get; set; }
}
}

View File

@ -15,8 +15,10 @@ namespace ComputerStoreContracts.ViewModels
public int ID { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
[DisplayName("Type of order")]
public OrderType Type { get; set; }
[DisplayName("Consignment ID")]
public int? ConsignmentID { get; set; }
[DisplayName("Request ID")]
public int? RequestID { get; set; }
[DisplayName("Status")]
public OrderStatus Status { get; set; }
[DisplayName("Creation date")]

View File

@ -16,7 +16,5 @@ namespace ComputerStoreContracts.ViewModels
public int OrderID { get; set; }
[DisplayName("Price")]
public double Price { get; set; }
[DisplayName("Count")]
public int Count { get; set; }
}
}

View File

@ -14,13 +14,13 @@ namespace ComputerStoreContracts.ViewModels
public int ID { get; set; }
[DisplayName("Username")]
public string Username { get; set; } = string.Empty;
[DisplayName("Password")]
public string Password { get; set; } = string.Empty;
[DisplayName("FirstName")]
public string FirstName { get; set; } = string.Empty;
public string? FirstName { get; set; } = string.Empty;
[DisplayName("LastName")]
public string LastName { get; set; } = string.Empty;
public string? LastName { get; set; } = string.Empty;
[DisplayName("MiddleName")]
public string MiddleName { get; set; } = string.Empty;
public string? MiddleName { get; set; } = string.Empty;
}
}

View File

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDataModels.Enums
{
public enum OrderType
{
Unknown = -1,
Consigment = 0,
Request = 1
}
}

View File

@ -6,11 +6,9 @@ using System.Threading.Tasks;
namespace ComputerStoreDataModels.Models
{
public interface IConsignmentModel
public interface IConsignmentModel : IID
{
int OrderID { get; }
int ProductID { get; }
double Price { get; }
int Count { get; }
}
}

View File

@ -10,7 +10,8 @@ namespace ComputerStoreDataModels.Models
public interface IOrderModel : IID
{
double Price { get; }
OrderType Type { get; }
int? ConsignmentID { get; }
int? RequestID { get; }
OrderStatus Status { get; }
DateTime DateCreate { get; }
DateTime? DateImplement { get; }

View File

@ -10,6 +10,5 @@ namespace ComputerStoreDataModels.Models
{
int OrderID { get; }
double Price { get; }
int Count { get; }
}
}