Complete hard lab 3

This commit is contained in:
Viltskaa 2023-03-27 19:12:26 +04:00
parent e19bbc11d1
commit c46aebdc08
5 changed files with 260 additions and 1 deletions

View File

@ -4,7 +4,7 @@ using NLog.Extensions.Logging;
using SushiBarBusinessLogic.BusinessLogics;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.StoragesContracts;
using SushiBarDatabaseImplement.Implements;
using SushiBarFileImplement.Implements;
namespace SushiBar
{

View File

@ -0,0 +1,128 @@
using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using SushiBarDataModels.Models;
namespace SushiBarDatabaseImplement.Implements;
public class StoreStorage : IStoreStorage
{
public List<StoreViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Stores
.Include(x => x.StoreSushi)
.ThenInclude(x => x.Sushi)
.Select(x => x.GetViewModel)
.ToList();
}
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
{
if (string.IsNullOrEmpty(model.StoreName))
{
return new List<StoreViewModel>();
}
using var context = new SushiBarDatabase();
return context.Stores
.Include(x => x.StoreSushi)
.ThenInclude(x => x.Sushi)
.Select(x => x.GetViewModel)
.Where(x => x.StoreName.Contains(model.StoreName ?? string.Empty))
.ToList();
}
public StoreViewModel? GetElement(StoreSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Stores
.Include(x => x.StoreSushi)
.ThenInclude(x => x.Sushi)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public StoreViewModel? Insert(StoreBindingModel model)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var store = Store.Create(context, model);
if (store == null)
{
return null;
}
if (context.Stores.Any(x => x.StoreName == store.StoreName))
{
throw new Exception("Name already exists!");
}
context.Stores.Add(store);
context.SaveChanges();
transaction.Commit();
return store.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public StoreViewModel? Update(StoreBindingModel model)
{
using var context = new SushiBarDatabase();
var shop = context.Stores.FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
shop.UpdateSushi(context, model);
context.SaveChanges();
return shop.GetViewModel;
}
public StoreViewModel? Delete(StoreBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Stores.FirstOrDefault(x => x.Id == model.Id);
if (element == null) return null;
context.Stores.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
public bool SellSushi(ISushiModel model, int quantity)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var sp in
context.StoreSushis.Where(x => x.SushiId == model.Id))
{
var res = Math.Min(quantity, sp.Count);
sp.Count -= res;
quantity -= res;
if (sp.Count == 0) context.StoreSushis.Remove(sp);
if (quantity == 0)
break;
}
if (quantity == 0)
{
context.SaveChanges();
transaction.Commit();
}
else
{
transaction.Rollback();
}
return quantity == 0;
}
}

View File

@ -0,0 +1,112 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
namespace SushiBarDatabaseImplement.Models;
public class Store : IStoreModel
{
public int Id { get; private init; }
[Required]
public string StoreName { get; private set; } = string.Empty;
[Required]
public string StoreAddress { get; private set; } = string.Empty;
[Required]
public DateTime OpeningDate { get; private set; }
private Dictionary<int, (ISushiModel, int)> _sushis = null;
[NotMapped]
public Dictionary<int, (ISushiModel, int)> Sushis
{
get
{
if (_sushis == null)
{
using var context = new SushiBarDatabase();
_sushis = StoreSushi
.ToDictionary(x => x.SushiId, x => (context.Sushi
.FirstOrDefault(y => y.Id == x.SushiId)! as ISushiModel, x.Count));
}
return _sushis;
}
}
[Required]
public int maxSushi { get; private set; }
[ForeignKey("StoreId")]
public virtual List<StoreSushi> StoreSushi { get; set; } = new();
public static Store? Create(SushiBarDatabase context, StoreBindingModel? model)
{
if (model == null) return null;
return new Store
{
Id = model.Id,
StoreName = model.StoreName,
StoreAddress = model.StoreAddress,
OpeningDate = model.OpeningDate,
maxSushi = model.maxSushi,
StoreSushi = model.Sushis.Select(x => new StoreSushi()
{
Sushi = context.Sushi.FirstOrDefault(y => y.Id == x.Key)!,
Count = x.Value.Item2
}).ToList()
};
}
public void Update(StoreBindingModel? model)
{
if (model == null)
return;
StoreName = model.StoreName;
StoreAddress = model.StoreAddress;
OpeningDate = model.OpeningDate;
}
public StoreViewModel GetViewModel => new()
{
Id = Id,
StoreName = StoreName,
StoreAddress = StoreAddress,
Sushis = Sushis,
OpeningDate = OpeningDate,
maxSushi = maxSushi
};
public void UpdateSushi(SushiBarDatabase context, StoreBindingModel model)
{
var StoreSushi = context.StoreSushis
.Where(rec => rec.StoreId == model.Id)
.ToList();
if (StoreSushi.Count > 0)
{
context.StoreSushis
.RemoveRange(StoreSushi
.Where(rec => !model.Sushis
.ContainsKey(rec.SushiId)));
foreach (var updateSushi in
StoreSushi.Where(x => model.Sushis.ContainsKey(x.SushiId)))
{
updateSushi.Count = model.Sushis[updateSushi.SushiId].Item2;
model.Sushis.Remove(updateSushi.SushiId);
}
}
var shop = context.Stores.First(x => x.Id == model.Id);
shop.StoreSushi.AddRange(model.Sushis.Select(x => new StoreSushi()
{
Sushi = context.Sushi.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(StoreSushi));
context.SaveChanges();
_sushis = null;
}
}

View File

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models;
public class StoreSushi
{
public int Id { get; set; }
[Required]
public int SushiId { get; set; }
[Required]
public int StoreId { get; set; }
[Required]
public int Count { get; set; }
public virtual Store Store { get; set; } = new();
public virtual Sushi Sushi { get; set; } = new();
}

View File

@ -17,5 +17,7 @@ namespace SushiBarDatabaseImplement
public virtual DbSet<Sushi> Sushi { set; get; }
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Store> Stores { get; set; }
public virtual DbSet<StoreSushi> StoreSushis { get; set; }
}
}