в общем, создала модели Магазин и МагазинСуши для бд
This commit is contained in:
parent
a9e6e02533
commit
57c68d01a8
13
SushiBarDatabaseImplement/Implements/ShopStorage.cs
Normal file
13
SushiBarDatabaseImplement/Implements/ShopStorage.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using SushiBarContracts.StoragesContracts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
}
|
||||
}
|
112
SushiBarDatabaseImplement/Models/Shop.cs
Normal file
112
SushiBarDatabaseImplement/Models/Shop.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using SushiBarContracts.BindingModel;
|
||||
using SushiBarContracts.ViewModels;
|
||||
using SushiBarDataModels;
|
||||
using SushiBarDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string ShopName { get; private set; }
|
||||
|
||||
[Required]
|
||||
public string Address { get; private set; }
|
||||
|
||||
[Required]
|
||||
public DateTime DateOpening { get; private set; }
|
||||
|
||||
[Required]
|
||||
public int MaxCountSushis { get; private set; }
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public List<ShopSushi> Sushis { get; private set; } = new();
|
||||
|
||||
private Dictionary<int, (ISushiModel, int)>? _shopSushis = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (ISushiModel, int)> ShopSushis
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopSushis == null)
|
||||
{
|
||||
_shopSushis = Sushis.ToDictionary(recPC => recPC.SushiId, recPC => (recPC.Sushi as ISushiModel, recPC.Count));
|
||||
}
|
||||
return _shopSushis;
|
||||
}
|
||||
}
|
||||
|
||||
public static Shop Create(SushiBarDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
MaxCountSushis = model.MaxCountSushis,
|
||||
Sushis = model.ShopSushis.Select(x => new ShopSushi
|
||||
{
|
||||
Sushi = context.Sushis.First(y => y.Id == x.Key), Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
MaxCountSushis = model.MaxCountSushis;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
MaxCountSushis = MaxCountSushis,
|
||||
ShopSushis = ShopSushis
|
||||
};
|
||||
public void UpdateComponents(SushiBarDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (ShopSushis != null && ShopSushis.Count > 0)
|
||||
{
|
||||
// удалили те, которых нет в модели
|
||||
context.ShopSushis.RemoveRange(ShopSushis.Where(rec => !model.ShopSushis.ContainsKey(rec.SushiId)));
|
||||
context.SaveChanges();
|
||||
ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateSushi in ShopSushis)
|
||||
{
|
||||
updateSushi.Count = model.ShopSushis[updateSushi.SushiId].Item2;
|
||||
model.ShopSushis.Remove(updateSushi.SushiId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var elem in model.ShopSushis)
|
||||
{
|
||||
context.ShopSushis.Add(new ShopSushi
|
||||
{
|
||||
Shop = shop,
|
||||
Sushi = context.Sushis.First(x => x.Id == elem.Key),
|
||||
Count = elem.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopSushis = null;
|
||||
}
|
||||
}
|
||||
}
|
27
SushiBarDatabaseImplement/Models/ShopSushi.cs
Normal file
27
SushiBarDatabaseImplement/Models/ShopSushi.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using SushiBarListImplements.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Runtime.ConstrainedExecution;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SushiBarDatabaseImplement.Models
|
||||
{
|
||||
public class ShopSushi
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int SushiId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
public virtual Sushi Sushi { get; set; } = new();
|
||||
}
|
||||
}
|
@ -10,11 +10,15 @@ namespace SushiBarDatabaseImplement.Models
|
||||
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
|
||||
{
|
||||
|
@ -24,5 +24,7 @@ namespace SushiBarDatabaseImplement
|
||||
public virtual DbSet<Sushi> Sushis { set; get; }
|
||||
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
public virtual DbSet<ShopSushi> ShopSushis { set; get; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user