154 lines
5.2 KiB
C#
154 lines
5.2 KiB
C#
using CanteenContracts.BindingModels;
|
|
using CanteenContracts.View;
|
|
using CanteenDataModels.Enums;
|
|
using CanteenDataModels.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CanteenDatabaseImplement.Models
|
|
{
|
|
public class Order : IOrderModel
|
|
{
|
|
public int Id { get; private set; }
|
|
[Required]
|
|
public int VisitorId { get; private set; }
|
|
[Required]
|
|
public string Description { get; private set; } = string.Empty;
|
|
public double? Sum { get; private set; }
|
|
|
|
private Dictionary<int, ICookModel>? _orderCooks = null;
|
|
|
|
[NotMapped]
|
|
public Dictionary<int, ICookModel> OrderCooks
|
|
{
|
|
get
|
|
{
|
|
if (_orderCooks == null)
|
|
{
|
|
_orderCooks = Cooks
|
|
.ToDictionary(recOD => recOD.CookId, recOD => recOD.Cook as ICookModel);
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
[ForeignKey("OrderId")]
|
|
public virtual List<LunchOrder> Lunches { get; set; } = new();
|
|
[ForeignKey("OrderId")]
|
|
public virtual List<OrderCook> Cooks { get; set; } = new();
|
|
[ForeignKey("OrderId")]
|
|
public virtual List<OrderTableware> Tablewares { get; set; } = new();
|
|
public virtual Visitor Visitor { get; set; }
|
|
|
|
public static Order Create(OrderBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
return new Order()
|
|
{
|
|
Id = model.Id,
|
|
VisitorId = model.VisitorId,
|
|
Description = model.Description,
|
|
Sum = model.Sum
|
|
};
|
|
}
|
|
|
|
public void Update(OrderBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
Description = model.Description;
|
|
Sum = model.Sum;
|
|
}
|
|
|
|
public OrderViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
VisitorId = VisitorId,
|
|
Description = Description,
|
|
Sum = Sum,
|
|
OrderCooks = OrderCooks,
|
|
OrderTablewares = OrderTablewares
|
|
};
|
|
public void UpdateOrderCook(CanteenDatabase context, OrderBindingModel model)
|
|
{
|
|
var orderCook = context.OrderCook.Where(rec => rec.CookId == model.Id).ToList();
|
|
if (orderCook != null && (orderCook.Count > 0))
|
|
{
|
|
context.OrderCook.RemoveRange(orderCook.Where(rec => !model.OrderCooks.ContainsKey(rec.CookId)));
|
|
context.SaveChanges();
|
|
foreach (var updateCook in orderCook)
|
|
{
|
|
model.OrderCooks.Remove(updateCook.CookId);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var order = context.Orders.First(x => x.Id == Id);
|
|
foreach (var dp in model.OrderCooks)
|
|
{
|
|
|
|
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)
|
|
{
|
|
var orderTableware = context.OrderTableware.Where(rec => rec.TablewareId == model.Id).ToList();
|
|
if (orderTableware != null && (orderTableware.Count > 0))
|
|
{
|
|
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
|
|
{
|
|
Order = order,
|
|
Tableware = context.Tablewares.First(x => x.Id == dp.Key),
|
|
CountTablewares = dp.Value.Item2
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_orderTablewares = null;
|
|
}
|
|
}
|
|
}
|