2023-04-07 23:35:33 +04:00
|
|
|
|
using CanteenContracts.BindingModels;
|
|
|
|
|
using CanteenContracts.View;
|
|
|
|
|
using CanteenDataModels.Enums;
|
2023-04-07 10:55:40 +04:00
|
|
|
|
using CanteenDataModels.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-04-07 18:58:07 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2023-04-07 10:55:40 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CanteenDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Order : IOrderModel
|
|
|
|
|
{
|
2023-04-07 18:58:07 +04:00
|
|
|
|
public int Id { get; private set; }
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[Required]
|
2023-04-07 18:58:07 +04:00
|
|
|
|
public int VisitorId { get; private set; }
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[Required]
|
2023-04-07 18:58:07 +04:00
|
|
|
|
public string Description { get; private set; } = string.Empty;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
|
2023-05-19 03:39:58 +04:00
|
|
|
|
private Dictionary<int, ICookModel>? _orderCooks = null;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
|
|
|
|
|
[NotMapped]
|
2023-05-19 03:39:58 +04:00
|
|
|
|
public Dictionary<int, ICookModel> OrderCooks
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-05-19 03:39:58 +04:00
|
|
|
|
if (_orderCooks == null)
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
2023-05-19 03:39:58 +04:00
|
|
|
|
_orderCooks = Cooks
|
|
|
|
|
.ToDictionary(recOD => recOD.CookId, recOD => recOD.Cook as ICookModel);
|
2023-04-07 23:35:33 +04:00
|
|
|
|
}
|
2023-05-19 03:39:58 +04:00
|
|
|
|
return _orderCooks;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, (ITablewareModel, int)>? _orderTablewares = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, (ITablewareModel, int)> OrderTablewares
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_orderTablewares == null)
|
|
|
|
|
{
|
|
|
|
|
_orderTablewares = Tablewares
|
|
|
|
|
.ToDictionary(recOD => recOD.TablewareId, recOD => (recOD.Tableware as ITablewareModel, recOD.CountTablewares));
|
|
|
|
|
}
|
|
|
|
|
return _orderTablewares;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-09 00:34:25 +04:00
|
|
|
|
[ForeignKey("OrderId")]
|
|
|
|
|
public virtual List<LunchOrder> Lunches { get; set; } = new();
|
|
|
|
|
[ForeignKey("OrderId")]
|
|
|
|
|
public virtual List<OrderCook> Cooks { get; set; } = new();
|
2023-05-19 03:39:58 +04:00
|
|
|
|
[ForeignKey("OrderId")]
|
|
|
|
|
public virtual List<OrderTableware> Tablewares { get; set; } = new();
|
2023-04-09 00:34:25 +04:00
|
|
|
|
public virtual Visitor Visitor { get; set; }
|
2023-04-07 23:35:33 +04:00
|
|
|
|
|
|
|
|
|
public static Order Create(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new Order()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
VisitorId = model.VisitorId,
|
|
|
|
|
Description = model.Description,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Description = model.Description;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
VisitorId = VisitorId,
|
|
|
|
|
Description = Description,
|
2023-05-19 03:39:58 +04:00
|
|
|
|
OrderCooks = OrderCooks,
|
|
|
|
|
OrderTablewares = OrderTablewares
|
2023-04-07 23:35:33 +04:00
|
|
|
|
};
|
2023-05-19 03:39:58 +04:00
|
|
|
|
public void UpdateOrderCook(CanteenDatabase context, OrderBindingModel model)
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
var orderCook = context.OrderCook.Where(rec => rec.OrderId == model.Id).ToList();
|
|
|
|
|
if (orderCook.Count > 0)
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
2023-05-19 03:39:58 +04:00
|
|
|
|
context.OrderCook.RemoveRange(orderCook.Where(rec => !model.OrderCooks.ContainsKey(rec.CookId)));
|
2023-04-07 23:35:33 +04:00
|
|
|
|
context.SaveChanges();
|
2023-05-19 03:39:58 +04:00
|
|
|
|
foreach (var updateCook in orderCook)
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
2023-05-19 03:39:58 +04:00
|
|
|
|
model.OrderCooks.Remove(updateCook.CookId);
|
2023-04-07 23:35:33 +04:00
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2023-04-09 00:34:25 +04:00
|
|
|
|
var order = context.Orders.First(x => x.Id == Id);
|
2023-05-19 03:39:58 +04:00
|
|
|
|
foreach (var dp in model.OrderCooks)
|
2023-04-09 00:34:25 +04:00
|
|
|
|
{
|
|
|
|
|
|
2023-05-19 03:39:58 +04:00
|
|
|
|
context.OrderCook.Add(new OrderCook
|
|
|
|
|
{
|
|
|
|
|
Order = order,
|
|
|
|
|
Cook = context.Cooks.First(x => x.Id == dp.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_orderCooks = null;
|
|
|
|
|
}
|
|
|
|
|
public void UpdateOrderTablewares(CanteenDatabase context, OrderBindingModel model)
|
|
|
|
|
{
|
2023-05-20 00:18:48 +04:00
|
|
|
|
var orderTableware = context.OrderTableware.Where(rec => rec.OrderId == model.Id).ToList();
|
|
|
|
|
if (orderTableware != null && orderTableware.Count > 0)
|
2023-05-19 03:39:58 +04:00
|
|
|
|
{
|
|
|
|
|
context.OrderTableware.RemoveRange(orderTableware.Where(rec => !model.OrderTablewares.ContainsKey(rec.TablewareId)));
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
foreach (var updateTableware in orderTableware)
|
|
|
|
|
{
|
|
|
|
|
updateTableware.CountTablewares = model.OrderTablewares[updateTableware.TablewareId].Item2;
|
|
|
|
|
model.OrderTablewares.Remove(updateTableware.TablewareId);
|
|
|
|
|
}
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
var order = context.Orders.First(x => x.Id == Id);
|
|
|
|
|
foreach (var dp in model.OrderTablewares)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
context.OrderTableware.Add(new OrderTableware
|
2023-04-07 23:35:33 +04:00
|
|
|
|
{
|
2023-04-09 00:34:25 +04:00
|
|
|
|
Order = order,
|
2023-05-19 03:39:58 +04:00
|
|
|
|
Tableware = context.Tablewares.First(x => x.Id == dp.Key),
|
|
|
|
|
CountTablewares = dp.Value.Item2
|
2023-04-07 23:35:33 +04:00
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2023-05-19 03:39:58 +04:00
|
|
|
|
_orderTablewares = null;
|
2023-04-07 23:35:33 +04:00
|
|
|
|
}
|
2023-04-07 10:55:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|