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

169 lines
5.9 KiB
C#
Raw Normal View History

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.Schema;
using System.ComponentModel.DataAnnotations;
2023-04-07 10:55:40 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2023-04-07 18:58:07 +04:00
using CanteenContracts.BindingModels;
using CanteenContracts.View;
2023-04-09 00:34:25 +04:00
using CanteenDataModels.Enums;
2023-04-07 10:55:40 +04:00
namespace CanteenDatabaseImplement.Models
{
public class Lunch : ILunchModel
{
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 LunchName { get; private set; } = string.Empty;
2023-04-09 00:34:25 +04:00
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; }
2023-04-07 18:58:07 +04:00
private Dictionary<int, (IProductModel, int)>? _lunchProducts = null;
2023-04-09 00:34:25 +04:00
[NotMapped]
2023-04-07 18:58:07 +04:00
public Dictionary<int, (IProductModel, int)> LunchProducts
{
get
{
if (_lunchProducts == null)
{
2023-04-09 00:34:25 +04:00
_lunchProducts = Products.ToDictionary(record => record.ProductId, record => (record.Product as IProductModel, record.CountProducts));
2023-04-07 18:58:07 +04:00
}
return _lunchProducts;
}
}
2023-04-09 00:34:25 +04:00
2023-05-20 00:18:48 +04:00
private Dictionary<int, IOrderModel>? _lunchOrders = null;
2023-05-19 03:39:58 +04:00
[NotMapped]
2023-05-20 00:18:48 +04:00
public Dictionary<int, IOrderModel> LunchOrders
2023-05-19 03:39:58 +04:00
{
get
{
if (_lunchOrders == null)
{
2023-05-20 00:18:48 +04:00
_lunchOrders = Orders.ToDictionary(record => record.OrderId, record => record.Order as IOrderModel);
2023-05-19 03:39:58 +04:00
}
return _lunchOrders;
}
}
2023-04-07 18:58:07 +04:00
[ForeignKey("LunchId")]
public virtual List<LunchProduct> Products { get; set; } = new();
2023-04-09 00:34:25 +04:00
[ForeignKey("LunchId")]
public virtual List<LunchOrder> Orders { get; set; } = new();
public virtual Visitor Visitor { get; set; }
2023-04-07 18:58:07 +04:00
public static Lunch? Create(CanteenDatabase context, LunchBindingModel model)
{
return new Lunch()
{
Id = model.Id,
VisitorId = model.VisitorId,
LunchName = model.LunchName,
2023-04-09 00:34:25 +04:00
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
2023-04-07 18:58:07 +04:00
Products = model.LunchProducts.Select(x => new LunchProduct
{
2023-04-09 00:34:25 +04:00
Product = context.Products.First(y => y.Id == x.Key),
CountProducts = x.Value.Item2
2023-05-19 03:39:58 +04:00
}).ToList(),
Orders = model.LunchOrders.Select(x => new LunchOrder
{
2023-05-20 00:18:48 +04:00
Order = context.Orders.First(y => y.Id == x.Key)
2023-04-07 18:58:07 +04:00
}).ToList()
};
}
2023-04-07 10:55:40 +04:00
2023-04-07 18:58:07 +04:00
public void Update(LunchBindingModel model)
{
LunchName = model.LunchName;
2023-04-09 00:34:25 +04:00
Sum = model.Sum;
Status = model.Status;
DateImplement = model.DateImplement;
2023-04-07 18:58:07 +04:00
}
2023-04-07 10:55:40 +04:00
2023-04-07 18:58:07 +04:00
public LunchViewModel GetViewModel => new()
{
Id = Id,
VisitorId = VisitorId,
LunchName = LunchName,
2023-04-09 00:34:25 +04:00
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
2023-05-20 12:11:48 +04:00
LunchProducts = LunchProducts,
LunchOrders = LunchOrders
2023-04-07 18:58:07 +04:00
};
2023-04-07 10:55:40 +04:00
2023-04-09 00:34:25 +04:00
public void UpdateProducts(CanteenDatabase context, LunchBindingModel model)
2023-04-07 18:58:07 +04:00
{
2023-04-09 00:34:25 +04:00
var lunchProducts = context.LunchProduct.Where(record => record.LunchId == model.Id).ToList();
2023-04-07 18:58:07 +04:00
if (lunchProducts != null && lunchProducts.Count > 0)
{
2023-04-09 00:34:25 +04:00
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);
}
2023-04-07 18:58:07 +04:00
context.SaveChanges();
}
2023-04-07 10:55:40 +04:00
2023-04-09 00:34:25 +04:00
var lunch = context.Lunches.First(x => x.Id == Id);
foreach (var pp in model.LunchProducts)
2023-04-07 18:58:07 +04:00
{
2023-04-09 00:34:25 +04:00
context.LunchProduct.Add(new LunchProduct
2023-04-07 18:58:07 +04:00
{
2023-04-09 00:34:25 +04:00
Lunch = lunch,
Product = context.Products.First(x => x.Id == pp.Key),
CountProducts = pp.Value.Item2
2023-04-07 18:58:07 +04:00
});
context.SaveChanges();
}
_lunchProducts = null;
}
2023-05-19 03:39:58 +04:00
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,
2023-05-20 00:18:48 +04:00
Order = context.Orders.First(x => x.Id == pp.Key)
2023-05-19 03:39:58 +04:00
});
context.SaveChanges();
}
_lunchOrders = null;
}
2023-04-07 10:55:40 +04:00
}
}