Добавлены классы SushiComponent & Sushi
This commit is contained in:
parent
dc07d5272d
commit
2510a97130
@ -14,7 +14,7 @@ namespace SushiBarDatabaseImplement.Models
|
||||
[Required]
|
||||
public double Cost { get; set; }
|
||||
[ForeignKey("ComponentId")]
|
||||
public virtual List<SushiComponent> ProductComponents { get; set; } = new();
|
||||
public virtual List<SushiComponent> SushiComponents { get; set; } = new();
|
||||
public static Component? Create(ComponentBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Models
|
||||
{
|
||||
internal class Order
|
||||
{
|
||||
}
|
||||
}
|
@ -1,12 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SushiBarDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using SushiBarContracts.BindingModel;
|
||||
using SushiBarContracts.ViewModels;
|
||||
|
||||
|
||||
namespace SushiBarDatabaseImplement.Models
|
||||
{
|
||||
internal class Sushi
|
||||
public class Sushi : ISushiModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string SushiName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> SushiComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sushiComponents == null)
|
||||
{
|
||||
_sushiComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||
(recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _sushiComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("SushiId")]
|
||||
public virtual List<SushiComponent> Components { get; set; } = new();
|
||||
[ForeignKey("SushiId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
|
||||
{
|
||||
return new Sushi()
|
||||
{
|
||||
Id = model.Id,
|
||||
SushiName = model.SushiName,
|
||||
Price = model.Price,
|
||||
Components = model.SushiComponents.Select(x => new SushiComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(SushiBindingModel model)
|
||||
{
|
||||
SushiName = model.SushiName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public SushiViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
SushiName = SushiName,
|
||||
Price = Price,
|
||||
SushiComponents = SushiComponents
|
||||
};
|
||||
public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model)
|
||||
{
|
||||
var SushiComponents = context.SushiComponents.Where(rec => rec.SushiId == model.Id).ToList();
|
||||
if (SushiComponents != null && SushiComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.SushiComponents.RemoveRange(SushiComponents.Where(rec
|
||||
=> !model.SushiComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in SushiComponents)
|
||||
{
|
||||
updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2;
|
||||
model.SushiComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var Sushi = context.Sushis.First(x => x.Id == Id);
|
||||
foreach (var pc in model.SushiComponents)
|
||||
{
|
||||
context.SushiComponents.Add(new SushiComponent
|
||||
{
|
||||
Sushi = Sushi,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_sushiComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
SushiBarDatabaseImplement/Models/SushiComponent.cs
Normal file
19
SushiBarDatabaseImplement/Models/SushiComponent.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
|
||||
namespace SushiBarDatabaseImplement.Models
|
||||
{
|
||||
public class SushiComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int SushiId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Sushi Sushi { get; set; } = new();
|
||||
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SushiBarContracts\SushiBarContracts.csproj" />
|
||||
<ProjectReference Include="..\SushiBarDataModels\SushiBarDataModels.csproj" />
|
||||
<ProjectReference Include="..\SushiBarListImplements\SushiBarListImplements.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Loading…
Reference in New Issue
Block a user