в общем, создала модели Магазин и МагазинСуши для бд

This commit is contained in:
Елена Бакальская 2024-04-21 17:49:53 +04:00
parent a9e6e02533
commit 57c68d01a8
5 changed files with 158 additions and 0 deletions

View 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
{
}
}

View 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;
}
}
}

View 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();
}
}

View File

@ -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
{

View File

@ -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; }
}
}