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

168 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CanteenDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CanteenContracts.BindingModels;
using CanteenContracts.View;
using CanteenDataModels.Enums;
namespace CanteenDatabaseImplement.Models
{
public class Lunch : ILunchModel
{
public int Id { get; private set; }
[Required]
public int VisitorId { get; private set; }
[Required]
public string LunchName { get; private set; } = string.Empty;
public double Sum { get; private set; }
[Required]
public LunchStatus Status { get; private set; } = LunchStatus.Создан;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
private Dictionary<int, (IProductModel, int)>? _lunchProducts = null;
[NotMapped]
public Dictionary<int, (IProductModel, int)> LunchProducts
{
get
{
if (_lunchProducts == null)
{
_lunchProducts = Products.ToDictionary(record => record.ProductId, record => (record.Product as IProductModel, record.CountProducts));
}
return _lunchProducts;
}
}
private Dictionary<int, IOrderModel>? _lunchOrders = null;
[NotMapped]
public Dictionary<int, IOrderModel> LunchOrders
{
get
{
if (_lunchOrders == null)
{
_lunchOrders = Orders.ToDictionary(record => record.OrderId, record => record.Order as IOrderModel);
}
return _lunchOrders;
}
}
[ForeignKey("LunchId")]
public virtual List<LunchProduct> Products { get; set; } = new();
[ForeignKey("LunchId")]
public virtual List<LunchOrder> Orders { get; set; } = new();
public virtual Visitor Visitor { get; set; }
public static Lunch? Create(CanteenDatabase context, LunchBindingModel model)
{
return new Lunch()
{
Id = model.Id,
VisitorId = model.VisitorId,
LunchName = model.LunchName,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Products = model.LunchProducts.Select(x => new LunchProduct
{
Product = context.Products.First(y => y.Id == x.Key),
CountProducts = x.Value.Item2
}).ToList(),
Orders = model.LunchOrders.Select(x => new LunchOrder
{
Order = context.Orders.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(LunchBindingModel model)
{
LunchName = model.LunchName;
Sum = model.Sum;
Status = model.Status;
DateImplement = model.DateImplement;
}
public LunchViewModel GetViewModel => new()
{
Id = Id,
VisitorId = VisitorId,
LunchName = LunchName,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
LunchProducts = LunchProducts
};
public void UpdateProducts(CanteenDatabase context, LunchBindingModel model)
{
var lunchProducts = context.LunchProduct.Where(record => record.LunchId == model.Id).ToList();
if (lunchProducts != null && lunchProducts.Count > 0)
{
context.LunchProduct.RemoveRange(lunchProducts.Where(record => !model.LunchProducts.ContainsKey(record.ProductId)));
context.SaveChanges();
foreach (var updateProduct in lunchProducts)
{
updateProduct.CountProducts = model.LunchProducts[updateProduct.ProductId].Item2;
model.LunchProducts.Remove(updateProduct.ProductId);
}
context.SaveChanges();
}
var lunch = context.Lunches.First(x => x.Id == Id);
foreach (var pp in model.LunchProducts)
{
context.LunchProduct.Add(new LunchProduct
{
Lunch = lunch,
Product = context.Products.First(x => x.Id == pp.Key),
CountProducts = pp.Value.Item2
});
context.SaveChanges();
}
_lunchProducts = null;
}
public void UpdateOrders(CanteenDatabase context, LunchBindingModel model)
{
var lunchOrders = context.LunchOrder.Where(record => record.LunchId == model.Id).ToList();
if (lunchOrders != null && lunchOrders.Count > 0)
{
context.LunchOrder.RemoveRange(lunchOrders.Where(record => !model.LunchOrders.ContainsKey(record.OrderId)));
context.SaveChanges();
foreach (var updateOrder in lunchOrders)
{
model.LunchOrders.Remove(updateOrder.OrderId);
}
context.SaveChanges();
}
var lunch = context.Lunches.First(x => x.Id == Id);
foreach (var pp in model.LunchOrders)
{
context.LunchOrder.Add(new LunchOrder
{
Lunch = lunch,
Order = context.Orders.First(x => x.Id == pp.Key)
});
context.SaveChanges();
}
_lunchOrders = null;
}
}
}