89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
using SushiBarContracts.BindingModels;
|
|
using SushiBarContracts.ViewModels;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
namespace SushiBarDatabaseImplement.Models
|
|
{
|
|
public class Cheque
|
|
{
|
|
[Key]
|
|
public int Id { get; set; }
|
|
|
|
public int? CustomerId { get; set; }
|
|
|
|
public virtual Customer? Customer { get; set; }
|
|
|
|
[Required]
|
|
public DateTime OrderDate { get; set; }
|
|
|
|
[Required]
|
|
public double TotalSum { get; set; }
|
|
|
|
public int? PromotionId { get; set; }
|
|
|
|
public virtual Promotion? Promotion { get; set; }
|
|
|
|
[ForeignKey("ChequeId")]
|
|
public virtual List<ChequeItem> ChequeItems { get; set; } = new();
|
|
|
|
public static Cheque Create(SushiBarDatabase Context, ChequeBindingModel Model)
|
|
{
|
|
return new Cheque()
|
|
{
|
|
CustomerId = Model.CustomerId,
|
|
OrderDate = Model.OrderDate,
|
|
TotalSum = Model.TotalSum,
|
|
PromotionId = Model.PromotionId,
|
|
ChequeItems = Model.ChequeItems.Select(x => new ChequeItem
|
|
{
|
|
Dish = Context.Dishes.First(y => y.Id == x.DishId),
|
|
Cook = Context.Cooks.First(y => y.Id == x.CookId),
|
|
Count = x.Count,
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(ChequeBindingModel Model)
|
|
{
|
|
return;
|
|
}
|
|
|
|
public ChequeViewModel ViewModel => new()
|
|
{
|
|
Id = Id,
|
|
CustomerId = CustomerId,
|
|
CustomerFio = Customer?.Fio,
|
|
OrderDate = OrderDate,
|
|
TotalSum = TotalSum,
|
|
PromotionId = PromotionId,
|
|
Discount = Promotion?.Discount,
|
|
ChequeItems = ChequeItems.ToDictionary(x => x.DishId, x => new ChequeItemViewModel
|
|
{
|
|
CheuqueId = x.ChequeId,
|
|
DishId = x.DishId,
|
|
CookId = x.CookId,
|
|
CookFio = x.Cook.Fio,
|
|
Count = x.Count,
|
|
}),
|
|
};
|
|
|
|
public void UpdateChequeItems(SushiBarDatabase Context, ChequeBindingModel Model)
|
|
{
|
|
var Cheque = Context.Cheques.First(x => x.Id == Id);
|
|
|
|
foreach (var ChequeItem in Model.ChequeItems)
|
|
{
|
|
Context.ChequeItems.Add(new ChequeItem
|
|
{
|
|
Cheque = Cheque,
|
|
Dish = Context.Dishes.First(x => x.Id == ChequeItem.DishId),
|
|
Count = ChequeItem.Count
|
|
});
|
|
|
|
Context.SaveChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|