чё-то сделал мб работает

This commit is contained in:
Николай 2023-03-23 20:29:55 +04:00
parent d8da9efab2
commit 0157390a30
4 changed files with 309 additions and 0 deletions

View File

@ -21,5 +21,9 @@ namespace FoodOrdersDatabaseImplement
public virtual DbSet<DishComponent> DishComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopDish> ShopDishes { set; get; }
}
}

View File

@ -0,0 +1,154 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDatabaseImplement;
using FoodOrdersDataModels.Models;
using FoodOrdersDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace FoodOrdersDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new FoodOrdersDatabase();
return context.Shops
.Include(x => x.Dishes)
.ThenInclude(x => x.Dish)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new FoodOrdersDatabase();
return context.Shops
.Include(x => x.Dishes)
.ThenInclude(x => x.Dish)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new FoodOrdersDatabase();
return context.Shops
.Include(x => x.Dishes)
.ThenInclude(x => x.Dish)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new FoodOrdersDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new FoodOrdersDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var updateShop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
if (updateShop == null)
{
return null;
}
updateShop.Update(model);
context.SaveChanges();
updateShop.UpdateDish(context, model);
transaction.Commit();
return updateShop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new FoodOrdersDatabase();
var element = context.Shops
.Include(x => x.Dishes)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellDishes(IDishModel dish, int count)
{
using var context = new FoodOrdersDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
List<ShopDish> ListShopDish = context.ShopDishes
.Include(x => x.Shop)
.Where(y => y.DishId == dish.Id)
.ToList();
if (ListShopDish == null) return false;
foreach (var shopDish in ListShopDish)
{
if (count - shopDish.Count >= 0)
{
count -= shopDish.Count;
ListShopDish.Remove(shopDish);
}
else
{
shopDish.Count -= count;
count = 0;
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,124 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDatabaseImplement.Models;
using FoodOrdersDataModels.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 FoodOrdersDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
[Required]
public string ShopName { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public DateTime DateOfOpening { get; private set; } = DateTime.Now;
[Required]
public int Capacity { get; private set; } = 0;
[NotMapped]
public Dictionary<int, (IDishModel, int)>? _shopDishes = null;
[NotMapped]
public Dictionary<int, (IDishModel, int)> ShopDishes
{
get
{
if (_shopDishes == null)
{
_shopDishes = Dishes.ToDictionary(recSD => recSD.DishId, recSD => (recSD.Dish as IDishModel, recSD.Count));
}
return _shopDishes;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopDish> Dishes { get; set; } = new();
public static Shop? Create(FoodOrdersDatabase context, ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOfOpening = model.DateOfOpening,
Dishes = model.ShopDishes.Select(x => new ShopDish
{
Dish = context.Dishes.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Capacity = model.Capacity
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
DateOfOpening = model.DateOfOpening;
Capacity = model.Capacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOfOpening = DateOfOpening,
ShopDishes = ShopDishes,
Capacity = Capacity
};
public void UpdateDish(FoodOrdersDatabase context, ShopBindingModel model)
{
var shopDishes = context.ShopDishes.Where(rec => rec.ShopId == model.Id).ToList();
if (shopDishes != null && shopDishes.Count > 0)
{ // удалили те в бд, которых нет в модели
context.ShopDishes.RemoveRange(shopDishes.Where(rec => !model.ShopDishes.ContainsKey(rec.DishId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateDish in shopDishes)
{
updateDish.Count = model.ShopDishes[updateDish.DishId].Item2;
model.ShopDishes.Remove(updateDish.DishId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var sd in model.ShopDishes)
{
context.ShopDishes.Add(new ShopDish
{
Shop = shop,
Dish = context.Dishes.First(x => x.Id == sd.Key),
Count = sd.Value.Item2
});
context.SaveChanges();
}
_shopDishes = null;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodOrdersDatabaseImplement.Models
{
public class ShopDish
{
public int Id { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int DishId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Dish Dish { get; set; } = new();
}
}