PIbd-22. Fedorenko G.Y. Lab Work 3 HARD #12
@ -17,5 +17,7 @@ namespace CarpentryWorkshopDatabaseImplement
|
||||
public virtual DbSet<Wood> Woods { set; get; }
|
||||
public virtual DbSet<WoodComponent> WoodComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
public virtual DbSet<Shop> Shops { get; set; }
|
||||
public virtual DbSet<ShopWood> ShopWoods { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,148 @@
|
||||
using CarpentryWorkshopContracts.BindingModels;
|
||||
using CarpentryWorkshopContracts.SearchModels;
|
||||
using CarpentryWorkshopContracts.StoragesContracts;
|
||||
using CarpentryWorkshopContracts.ViewModels;
|
||||
using CarpentryWorkshopDatabaseImplement.Models;
|
||||
using CarpentryWorkshopDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CarpentryWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
var element = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Woods)
|
||||
.ThenInclude(x => x.Wood)
|
||||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Woods)
|
||||
.ThenInclude(x => x.Wood)
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.ShopName.Contains(model.ShopName ?? string.Empty))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Woods)
|
||||
.ThenInclude(x => x.Wood)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
try
|
||||
{
|
||||
var newShop = Shop.Create(context, model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (context.Shops.Any(x => x.ShopName == newShop.ShopName))
|
||||
{
|
||||
throw new Exception("Не должно быть два магазина с одним названием");
|
||||
}
|
||||
context.Shops.Add(newShop);
|
||||
context.SaveChanges();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id)))
|
||||
{
|
||||
throw new Exception("Не должно быть два магазина с одним названием");
|
||||
}
|
||||
shop.Update(model);
|
||||
shop.UpdateWoods(context, model);
|
||||
context.SaveChanges();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public bool SellWoods(IWoodModel model, int count)
|
||||
{
|
||||
if (model == null)
|
||||
return false;
|
||||
using var context = new CarpentryWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
List<ShopWood> lst = new List<ShopWood>();
|
||||
foreach (var el in context.ShopWoods.Where(x => x.WoodId == model.Id))
|
||||
{
|
||||
int dif = count;
|
||||
if (el.Count < dif)
|
||||
dif = el.Count;
|
||||
el.Count -= dif;
|
||||
count -= dif;
|
||||
if (el.Count == 0)
|
||||
{
|
||||
lst.Add(el);
|
||||
}
|
||||
if (count == 0)
|
||||
break;
|
||||
}
|
||||
if (count > 0)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
foreach (var el in lst)
|
||||
{
|
||||
context.ShopWoods.Remove(el);
|
||||
}
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
using CarpentryWorkshopContracts.BindingModels;
|
||||
using CarpentryWorkshopContracts.ViewModels;
|
||||
using CarpentryWorkshopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace CarpentryWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public DateTime DateOpen { get; set; }
|
||||
|
||||
[Required]
|
||||
public int WoodMaxCount { get; set; }
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public List<ShopWood> Woods { get; set; } = new();
|
||||
|
||||
private Dictionary<int, (IWoodModel, int)>? _shopWoods = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IWoodModel, int)> ShopWoods
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopWoods == null)
|
||||
{
|
||||
_shopWoods = Woods
|
||||
.ToDictionary(x => x.WoodId, x =>
|
||||
(x.Wood as IWoodModel, x.Count));
|
||||
}
|
||||
return _shopWoods;
|
||||
}
|
||||
}
|
||||
|
||||
public static Shop? Create(CarpentryWorkshopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
return null;
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpen = model.DateOpen,
|
||||
WoodMaxCount = model.WoodMaxCount,
|
||||
Woods = model.ShopWoods.Select(x => new ShopWood
|
||||
{
|
||||
Wood = context.Woods.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpen = model.DateOpen;
|
||||
WoodMaxCount = model.WoodMaxCount;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpen = DateOpen,
|
||||
ShopWoods = ShopWoods,
|
||||
WoodMaxCount = WoodMaxCount
|
||||
};
|
||||
|
||||
public void UpdateWoods(CarpentryWorkshopDatabase context,
|
||||
ShopBindingModel model)
|
||||
{
|
||||
var shopWoods = context.ShopWoods.Where(rec =>
|
||||
rec.ShopId == model.Id).ToList();
|
||||
if (shopWoods != null && shopWoods.Count > 0)
|
||||
{
|
||||
context.ShopWoods.RemoveRange(shopWoods.Where(rec => !model.ShopWoods.ContainsKey(rec.WoodId)));
|
||||
|
||||
context.SaveChanges();
|
||||
foreach (var updateWood in shopWoods)
|
||||
{
|
||||
updateWood.Count =
|
||||
model.ShopWoods[updateWood.WoodId].Item2;
|
||||
model.ShopWoods.Remove(updateWood.WoodId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var pc in model.ShopWoods)
|
||||
{
|
||||
context.ShopWoods.Add(new ShopWood
|
||||
{
|
||||
Shop = shop,
|
||||
Wood = context.Woods.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopWoods = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -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 CarpentryWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class ShopWood
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int WoodId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
|
||||
public virtual Wood Wood { get; set; } = new();
|
||||
}
|
||||
}
|
@ -33,8 +33,10 @@ namespace CarpentryWorkshopDatabaseImplement.Models
|
||||
public virtual List<WoodComponent> Components { get; set; } = new();
|
||||
[ForeignKey("WoodId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
[ForeignKey("WoodId")]
|
||||
public virtual List<ShopWood> ShopWoods { get; set; } = new();
|
||||
|
||||
public static Wood? Create(CarpentryWorkshopDatabase context, WoodBindingModel model)
|
||||
public static Wood? Create(CarpentryWorkshopDatabase context, WoodBindingModel model)
|
||||
{
|
||||
var components = context.Components;
|
||||
return new Wood()
|
||||
|
Loading…
Reference in New Issue
Block a user