PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenDatabaseImplement/Models/Order.cs

115 lines
3.7 KiB
C#
Raw Normal View History

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
public int TablewareId { get; set; }
public int CountTablewares { get; set; }
[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
public double? Sum { get; private set; }
private Dictionary<int, (IDishModel, int)>? _orderDishes = null;
[NotMapped]
public Dictionary<int, (IDishModel, int)> OrderDishes
{
get
{
if (_orderDishes == null)
{
_orderDishes = Dishes
2023-04-09 00:34:25 +04:00
.ToDictionary(recOD => recOD.DishId, recOD => (recOD.Dish as IDishModel, recOD.CountDishes));
2023-04-07 23:35:33 +04:00
}
return _orderDishes;
}
}
2023-04-07 10:55:40 +04:00
[ForeignKey("OrderId")]
2023-04-07 23:35:33 +04:00
public virtual List<OrderDish> Dishes { get; set; } = new();
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();
public virtual Tableware Tableware { get; set; }
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,
Sum = model.Sum
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
VisitorId = model.VisitorId;
Description = model.Description;
Sum = model.Sum;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
VisitorId = VisitorId,
Description = Description,
Sum = Sum,
OrderDishes = OrderDishes
};
2023-04-09 00:34:25 +04:00
public void UpdateOrderDish(CanteenDatabase context, OrderBindingModel model)
2023-04-07 23:35:33 +04:00
{
var orderDish = context.OrderDish.Where(rec => rec.DishId == model.Id).ToList();
if (orderDish != null && (orderDish.Count > 0))
{
2023-04-09 00:34:25 +04:00
context.OrderDish.RemoveRange(orderDish.Where(rec => !model.OrderDishes.ContainsKey(rec.DishId)));
2023-04-07 23:35:33 +04:00
context.SaveChanges();
foreach (var updateDish in orderDish)
{
2023-04-09 00:34:25 +04:00
updateDish.CountDishes = model.OrderDishes[updateDish.DishId].Item2;
model.OrderDishes.Remove(updateDish.DishId);
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);
foreach (var dp in model.OrderDishes)
{
context.OrderDish.Add(new OrderDish
2023-04-07 23:35:33 +04:00
{
2023-04-09 00:34:25 +04:00
Order = order,
Dish = context.Dishes.First(x => x.Id == dp.Key),
CountDishes = dp.Value.Item2
2023-04-07 23:35:33 +04:00
});
context.SaveChanges();
}
_orderDishes = null;
}
2023-04-07 10:55:40 +04:00
}
}