ISEbd-22. Baygulov A.A. Lab work 06 #9

Closed
tellsense wants to merge 11 commits from lab6 into lab5
10 changed files with 273 additions and 236 deletions
Showing only changes of commit 86067ecb57 - Show all commits

View File

@ -16,142 +16,151 @@ namespace SushiBarBusinessLogic.BusinessLogics
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
static readonly object _locker = new object();
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
public OrderLogic(ILogger<SushiLogic> Logger, IOrderStorage OrderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
_logger = Logger;
_orderStorage = OrderStorage;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
public List<OrderViewModel>? ReadList(OrderSearchModel? Model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model.ClientId, model.Status, model.ImplementerId, model.DateFrom, model.DateTo, model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
_logger.LogInformation("ReadList. ClientId: {ClientId}. Status: {Status}. ImplementerId: {ImplementerId}. DateFrom: {DateFrom}. DateTo: {DateTo}. OrderId: {Id}",
Model?.ClientId, Model?.Status, Model?.ImplementerId, Model?.DateFrom, Model?.DateTo, Model?.Id);
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. ClientId:{ClientId}.Status:{Status}.ImplementerId:{ImplementerId}.DateFrom:{DateFrom}.DateTo:{DateTo}OrderId:{Id}",
model?.ClientId, model?.Status, model?.ImplementerId, model?.DateFrom, model?.DateTo, model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
var List = Model is null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(Model);
if (List is null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
_logger.LogInformation("ReadList. Count: {Count}", List.Count);
return List;
}
public bool CreateOrder(OrderBindingModel model)
public bool CreateOrder(OrderBindingModel Model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
CheckModel(Model);
if (Model.Status != OrderStatus.Неизвестен)
{
_logger.LogWarning("Invalid order status");
return false;
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
}
Model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(Model) is null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
private bool ChangeOrderStatus(OrderBindingModel Model, OrderStatus NewStatus)
{
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 != NewStatus)
{
_logger.LogWarning("Change status operation failed. Incorrect new status: {NewStatus}. Current status: {currStatus}",
NewStatus, Order.Status);
return false;
}
Model.SushiId = Order.SushiId;
Model.ClientId = Order.ClientId;
if (!Model.ImplementerId.HasValue) Model.ImplementerId = Order.ImplementerId;
Model.Count = Order.Count;
Model.Sum = Order.Sum;
Model.DateCreate = Order.DateCreate;
Model.Status = NewStatus;
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;
}
public bool TakeOrderInWork(OrderBindingModel Model)
{
lock (_locker)
{
return ChangeStatus(model, OrderStatus.Выполняется);
return ChangeOrderStatus(Model, OrderStatus.Выполняется);
}
}
public bool FinishOrder(OrderBindingModel model)
public bool FinishOrder(OrderBindingModel Model)
{
return ChangeStatus(model, OrderStatus.Готов);
return ChangeOrderStatus(Model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
public bool DeliveryOrder(OrderBindingModel Model)
{
return ChangeStatus(model, OrderStatus.Выдан);
return ChangeOrderStatus(Model, OrderStatus.Выдан);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
private void CheckModel(OrderBindingModel Model, bool WithParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
if (Model == null)
throw new ArgumentNullException(nameof(Model));
if (!WithParams)
return;
}
if (model.Count <= 0)
{
throw new ArgumentException("Колличество пиццы в заказе не может быть меньше 1", nameof(model.Count));
}
if (model.Sum <= 0)
{
throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum));
}
if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate)
{
throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}");
}
_logger.LogInformation("Sushi. SushiId:{SushiId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.SushiId, model.Count, model.Sum, model.Id);
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. SushiId:{SushiId}. Count:{Count}. Sum:{Sum}. " +
"Status:{Status}. DateCreate:{DateCreate}. DateImplement:{DateImplement}. Id: {Id}",
Model.SushiId, Model.Count, Model.Sum, Model.Status, Model.DateCreate,
Model.DateImplement, Model.Id);
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus)
public OrderViewModel? ReadElement(OrderSearchModel Model)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel()
if (Model == null)
{
Id = model.Id
});
if (element == null)
{
throw new InvalidOperationException(nameof(element));
throw new ArgumentNullException(nameof(Model));
}
model.DateCreate = element.DateCreate;
model.SushiId = element.SushiId;
model.DateImplement = element.DateImplement;
model.ClientId = element.ClientId;
if (!model.ImplementerId.HasValue)
_logger.LogInformation("ReadElement. ClientId: {ClientId}. Status: {Status}. ImplementerId: {ImplementerId}. DateFrom: {DateFrom}. DateTo: {DateTo}. OrderId: {Id}",
Model.ClientId, Model.Status, Model.ImplementerId, Model.DateFrom, Model.DateTo, Model.Id);
var Order = _orderStorage.GetElement(Model);
if (Order == null)
{
model.ImplementerId = element.ImplementerId;
_logger.LogWarning("ReadElement element not found");
return null;
}
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (requiredStatus - model.Status == 1)
{
model.Status = requiredStatus;
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
}
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new InvalidOperationException($"Невозможно приствоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
_logger.LogInformation("ReadElement find. Id: {Id}", Order.Id);
return Order;
}
}
}

View File

@ -26,32 +26,25 @@ namespace SushiBarBusinessLogic
_rnd = new Random(1000);
}
public void DoWork(IImplementerLogic ImplementerLogic, IOrderLogic OrderLogic)
public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic)
{
_orderLogic = OrderLogic;
var Implementers = ImplementerLogic.ReadList(null);
if (Implementers == null)
_orderLogic = orderLogic;
var implementers = implementerLogic.ReadList(null);
if (implementers == null)
{
_logger.LogWarning("DoWork. Implementers is null");
return;
}
var Orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
var BeingProcessedOrders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Выполняется});
if (Orders == null || BeingProcessedOrders == null || (Orders.Count == 0 && BeingProcessedOrders.Count == 0))
var orders = _orderLogic.ReadList(new OrderSearchModel { Status = OrderStatus.Принят });
if (orders == null || orders.Count == 0)
{
_logger.LogWarning("DoWork. Orders is null or empty");
return;
}
_logger.LogDebug("DoWork for {Count} orders", Orders.Count);
foreach (var Implementer in Implementers)
_logger.LogDebug("DoWork for {Count} orders", orders.Count);
foreach (var implementer in implementers)
{
Task.Run(() => WorkerWorkAsync(Implementer, Orders));
Task.Run(() => WorkerWorkAsync(implementer, orders));
}
}

View File

@ -1,4 +1,4 @@
using SushiBarDataModels;
 using SushiBarDataModels;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@ -9,12 +9,12 @@ namespace SushiBarContracts.SearchModels
{
public class OrderSearchModel
{
public int? Id { get; set; }
public OrderStatus? Status { get; set; }
public int? Id { get; set; }
public int? ClientId { get; set; }
public int? ImplementerId { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}
public DateTime? DateTo { get; set; }
public OrderStatus? Status { get; set; }
public int? ImplementerId { get; set; }
}
}

View File

@ -32,8 +32,10 @@ namespace SushiBarDatabaseImplement.Models
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new();
public static Order Create(SushiBarDatabase context, OrderBindingModel model)
public static Order Create(SushiBarDatabase context, OrderBindingModel model)
{
return new Order()
{

View File

@ -18,25 +18,28 @@ namespace SushiBarDatabaseImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new SushiBarDatabase();
if (model.DateFrom.HasValue)
if ((!model.DateFrom.HasValue || !model.DateTo.HasValue) && !model.ClientId.HasValue && !model.Status.HasValue)
{
return context.Orders.Include(x => x.Sushi).Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList();
return new();
}
if (model.ClientId.HasValue)
{
return context.Orders.Include(x => x.Sushi).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
}
return context.Orders.Include(x => x.Sushi).Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).Include(x => x.Implementer).Where(x =>
(model.DateFrom.HasValue && model.DateTo.HasValue && x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) ||
(model.ClientId.HasValue && x.ClientId == model.ClientId) ||
(model.Status.HasValue && x.Status == model.Status))
.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
if (!model.Id.HasValue && (!model.ImplementerId.HasValue || !model.Status.HasValue))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Orders.Include(x => x.Sushi).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return context.Orders.Include(x => x.Sushi).Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x =>
(model.Id.HasValue && x.Id == model.Id) ||
(model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && x.Status == model.Status))
?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)

View File

@ -11,87 +11,92 @@ using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
internal class Order : IOrderModel
{
public int Id { get; private set; }
internal class Order : IOrderModel
{
public int Id { get; private set; }
public int ClientId { get; private set; }
public int? ImplementerId { get; 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; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { 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,
SushiId = model.SushiId,
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SushiId = model.SushiId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
string dateImplement = element.Element("DateImplement")!.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SushiId = Convert.ToInt32(element.Element("SushiId")!.Value),
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
string dateImplement = element.Element("DateImplement")!.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SushiId = Convert.ToInt32(element.Element("SushiId")!.Value),
ClientId = Convert.ToInt32(element.Element("ClientId")!.Value),
ImplementerId = Convert.ToInt32(element.Element("ImplementerId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement)
};
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement)
};
}
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
SushiId = SushiId,
public OrderViewModel GetViewModel => new()
{
Id = Id,
SushiId = SushiId,
ClientId = ClientId,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SushiId", SushiId.ToString()),
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SushiId", SushiId.ToString()),
new XElement("ClientId", ClientId.ToString()),
new XElement("ImplementerId", ImplementerId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString()));
}
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString()));
}
}

View File

@ -33,7 +33,11 @@ namespace SushiBarFileImplement.Implements
{
return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
}
if (!model.ImplementerId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList();
}
if (model.Id.HasValue)
{
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
@ -43,6 +47,10 @@ namespace SushiBarFileImplement.Implements
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (model.ImplementerId.HasValue)
{
return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
}
if (!model.Id.HasValue)
{
return new();

View File

@ -10,56 +10,59 @@ using System.Threading.Tasks;
namespace SushiBarListImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int SushiId { get; private set; }
public class Order : IOrderModel
{
public int Id { get; private set; }
public int SushiId { get; private set; }
public int ClientId { 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 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 int? ImplementerId { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SushiId = model.SushiId,
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SushiId = model.SushiId,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
SushiId = SushiId,
public OrderViewModel GetViewModel => new()
{
Id = Id,
SushiId = SushiId,
ClientId = ClientId,
ImplementerId = ImplementerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}

View File

@ -54,6 +54,16 @@ namespace SushiBarListImplement
}
}
}
else if (model.ImplementerId.HasValue && !model.Id.HasValue)
{
foreach (var order in _source.Orders)
{
if (order.ImplementerId == model.ImplementerId)
{
result.Add(GetViewModel(order));
}
}
}
else if (model.Id.HasValue)
{
foreach (var order in _source.Orders)
@ -79,6 +89,10 @@ namespace SushiBarListImplement
{
return AttachSushiName(order.GetViewModel);
}
else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId)
{
return GetViewModel(order);
}
}
return null;
}