demo_trade/Domain/OrderClientUseCase.cs

41 lines
2.1 KiB
C#
Raw Permalink Normal View History

2024-10-04 12:41:04 +00:00
using demo_trade.Data.RemoteData.Entity;
using demo_trade.Data.Repository;
using demo_trade.UI.Presenters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace demo_trade.Domain
{
public class OrderClientUseCase
{
private IOrderRepository _orderRepository;
private IPickupRepository _pickupRepository;
public OrderClientUseCase(IOrderRepository orderRepository, IPickupRepository pickupRepository) {
_orderRepository = orderRepository;
_pickupRepository = pickupRepository;
}
public List<PickupPoint> GetPickupPoints() => _pickupRepository.GetAllPickupPoints();
public int GetNewOrderNumber() => _orderRepository.GetNumberForNewOrder();
public Models.Order? GenerateNewOrder(PickupPoint orderPickupPoint, List<OrderProductPresenter> products,decimal orderSumCost, string? clientName = null) {
int daysOrderCount = CheckProductsQuntity(products) ? 3 : 6;
Order order = new Order { Orderdeliverydate = DateOnly.FromDateTime(DateTime.Now.AddDays(daysOrderCount)), Orderpickuppoint = orderPickupPoint.PickupPointId, Orderstatus = "Новый", ClientName= clientName, OrderSumCost=orderSumCost};
List<Orderproduct> orderproducts = products.Select(it => new Orderproduct { Productarticlenumber = it.ArticleNumber, ProductCount = it.ProductCount }).ToList();
var resultOrder = _orderRepository.CreateOrder(order, orderproducts);
if (resultOrder != null) return new demo_trade.Models.Order { Orderid = resultOrder.Orderid, Orderdeliverydate = resultOrder.Orderdeliverydate, PickupPointName = orderPickupPoint.PickupPointName, Orderstatus = resultOrder.Orderstatus, ProductList =products, OrderClient=clientName, OrderSumCost=orderSumCost };
return null;
}
private bool CheckProductsQuntity(List<OrderProductPresenter> orderProducts) {
foreach (var product in orderProducts) {
if (product.QuantityInStock <= 3) return false;
}
return true;
}
}
}