103 lines
3.6 KiB
C#
103 lines
3.6 KiB
C#
|
using ConfectioneryContracts.BindingModels;
|
|||
|
using ConfectioneryContracts.ViewModels;
|
|||
|
using ConfectioneryDataModels.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;
|
|||
|
|
|||
|
namespace ConfectioneryDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Sweets : ISweetsModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string SweetsName { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public double Price { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, (IIngredientModel, int)>? _sweetsIngredients = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, (IIngredientModel, int)> SweetsIngredients
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_sweetsIngredients == null)
|
|||
|
{
|
|||
|
_sweetsIngredients = Ingredients
|
|||
|
.ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
|
|||
|
}
|
|||
|
return _sweetsIngredients;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("SweetsId")]
|
|||
|
public virtual List<SweetsIngredient> Ingredients { get; set; } = new();
|
|||
|
[ForeignKey("SweetsId")]
|
|||
|
public virtual List<Order> Orders { get; set; } = new();
|
|||
|
|
|||
|
public static Sweets Create(ConfectioneryDatabase context, SweetsBindingModel model)
|
|||
|
{
|
|||
|
return new Sweets()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
SweetsName = model.SweetsName,
|
|||
|
Price = model.Price,
|
|||
|
Ingredients = model.SweetsIngredients.Select(x => new SweetsIngredient
|
|||
|
{
|
|||
|
Ingredient = context.Ingredients.First(y => y.Id == x.Key),
|
|||
|
Count = x.Value.Item2
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(SweetsBindingModel model)
|
|||
|
{
|
|||
|
SweetsName = model.SweetsName;
|
|||
|
Price = model.Price;
|
|||
|
}
|
|||
|
|
|||
|
public SweetsViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
SweetsName = SweetsName,
|
|||
|
Price = Price,
|
|||
|
SweetsIngredients = SweetsIngredients
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateIngredients(ConfectioneryDatabase context, SweetsBindingModel model)
|
|||
|
{
|
|||
|
var sweetsIngredients = context.SweetsIngredients.Where(rec => rec.SweetsId == model.Id).ToList();
|
|||
|
if (sweetsIngredients != null && sweetsIngredients.Count > 0)
|
|||
|
{
|
|||
|
context.SweetsIngredients.RemoveRange(sweetsIngredients.Where(rec => !model.SweetsIngredients.ContainsKey(rec.IngredientId)));
|
|||
|
context.SaveChanges();
|
|||
|
foreach (var updateIngredient in sweetsIngredients)
|
|||
|
{
|
|||
|
updateIngredient.Count = model.SweetsIngredients[updateIngredient.IngredientId].Item2;
|
|||
|
model.SweetsIngredients.Remove(updateIngredient.IngredientId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var sweets = context.ListSweets.First(x => x.Id == Id);
|
|||
|
foreach (var si in model.SweetsIngredients)
|
|||
|
{
|
|||
|
context.SweetsIngredients.Add(new SweetsIngredient
|
|||
|
{
|
|||
|
Sweets = sweets,
|
|||
|
Ingredient = context.Ingredients.First(x => x.Id == si.Key),
|
|||
|
Count = si.Value.Item2
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_sweetsIngredients = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|