114 lines
3.8 KiB
C#
114 lines
3.8 KiB
C#
using RestaurantContracts.BindingModels;
|
||
using RestaurantContracts.ViewModels;
|
||
using RestaurantDataModels.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 RestaurantDatabaseImplement.Models
|
||
{
|
||
public class Order : IOrderModel
|
||
{
|
||
[Required]
|
||
public int ClientId { get; set; }
|
||
public virtual Client Client { get; set; } = new();
|
||
|
||
[Required]
|
||
public double Price { get; set; }
|
||
|
||
[Required]
|
||
public DateTime Date { get; set; }
|
||
|
||
private Dictionary<int, (IProductModel, int)>? _orderProducts = null;
|
||
|
||
[NotMapped]
|
||
public Dictionary<int, (IProductModel, int)> OrderProducts
|
||
{
|
||
get
|
||
{
|
||
if (_orderProducts == null)
|
||
{
|
||
_orderProducts = Products.ToDictionary(recPC => recPC.ProductId, recPC =>
|
||
(recPC.Product as IProductModel, recPC.Count));
|
||
}
|
||
return _orderProducts;
|
||
}
|
||
}
|
||
|
||
[ForeignKey("OrderId")]
|
||
public virtual List<OrderProduct> Products { get; set; } = new();
|
||
|
||
public int Id { get; set; }
|
||
|
||
public static Order? Create(RestaurantDatabase context, OrderBindingModel? model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
return new Order()
|
||
{
|
||
Id = model.Id,
|
||
ClientId = model.ClientId,
|
||
Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId),
|
||
Price = model.Price,
|
||
Date = model.Date,
|
||
Products = model.OrderProducts.Select(x => new OrderProduct
|
||
{
|
||
Product = context.Products.First(y => y.Id == x.Key),
|
||
Count = x.Value.Item2
|
||
}).ToList()
|
||
};
|
||
}
|
||
|
||
public void Update(OrderBindingModel? model)
|
||
{
|
||
Date = model.Date;
|
||
Price = model.Price;
|
||
}
|
||
|
||
public OrderViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
ClientId = ClientId,
|
||
ClientFirstName = Client.FirstName,
|
||
ClientLastName = Client.LastName,
|
||
Price = Price,
|
||
Date = Date,
|
||
};
|
||
|
||
public void UpdateComponents(RestaurantDatabase context, OrderBindingModel model)
|
||
{
|
||
var OrderProducts = context.OrderProducts.Where(rec => rec.OrderId == model.Id).ToList();
|
||
if (OrderProducts != null && OrderProducts.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.OrderProducts.RemoveRange(OrderProducts.Where(rec => !model.OrderProducts.ContainsKey(rec.ProductId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateComponent in OrderProducts)
|
||
{
|
||
updateComponent.Count = model.OrderProducts[updateComponent.ProductId].Item2;
|
||
model.OrderProducts.Remove(updateComponent.ProductId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var Order = context.Orders.First(x => x.Id == Id);
|
||
foreach (var pc in model.OrderProducts)
|
||
{
|
||
context.OrderProducts.Add(new OrderProduct
|
||
{
|
||
Order = Order,
|
||
Product = context.Products.First(x => x.Id == pc.Key),
|
||
Count = pc.Value.Item2
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_orderProducts = null;
|
||
}
|
||
}
|
||
}
|