PIbd-22. Kozlova A.A. Lab work 05 hard #14
@ -0,0 +1,146 @@
|
||||
using PrecastConcretePlantContracts.BindingModels;
|
||||
using PrecastConcretePlantContracts.SearchModels;
|
||||
using PrecastConcretePlantContracts.StoragesContracts;
|
||||
using PrecastConcretePlantContracts.ViewModels;
|
||||
using PrecastConcretePlantDatabaseImplement.Models;
|
||||
using PrecastConcretePlantDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PrecastConcretePlantDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new PrecastConcretePlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Reinforceds)
|
||||
.ThenInclude(x => x.Reinforced)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new PrecastConcretePlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Reinforceds)
|
||||
.ThenInclude(x => x.Reinforced)
|
||||
.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 PrecastConcretePlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Reinforceds)
|
||||
.ThenInclude(x => x.Reinforced)
|
||||
.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 PrecastConcretePlantDatabase();
|
||||
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 PrecastConcretePlantDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
shop.Update(model);
|
||||
context.SaveChanges();
|
||||
shop.UpdateReinforceds(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new PrecastConcretePlantDatabase();
|
||||
var element = context.Shops
|
||||
.Include(x => x.Reinforceds)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool MakeSale(IReinforcedModel model, int count)
|
||||
{
|
||||
using var context = new PrecastConcretePlantDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var shop in context.Shops.Include(x => x.Reinforceds).ThenInclude(x => x.Reinforced)
|
||||
.Where(x => x.Reinforceds.Any(x => x.ReinforcedId == model.Id))
|
||||
.ToList())
|
||||
{
|
||||
var reinforced = shop.ShopReinforceds[model.Id];
|
||||
int min = Math.Min(reinforced.Item2, count);
|
||||
if (min == reinforced.Item2)
|
||||
{
|
||||
shop.ShopReinforceds.Remove(model.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.ShopReinforceds[model.Id] = (reinforced.Item1, reinforced.Item2 - min);
|
||||
}
|
||||
shop.UpdateReinforceds(context, new() { Id = shop.Id, ShopReinforceds = shop.ShopReinforceds });
|
||||
count -= min;
|
||||
if (count == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -40,6 +40,9 @@ namespace PrecastConcretePlantDatabaseImplement.Models
|
||||
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
[ForeignKey("ReinforcedId")]
|
||||
public virtual List<ShopReinforced> ShopReinforceds { get; set; } = new();
|
||||
|
||||
public static Reinforced Create(PrecastConcretePlantDatabase context, ReinforcedBindingModel model)
|
||||
{
|
||||
return new Reinforced()
|
||||
|
@ -0,0 +1,115 @@
|
||||
using PrecastConcretePlantContracts.BindingModels;
|
||||
using PrecastConcretePlantContracts.ViewModels;
|
||||
using PrecastConcretePlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PrecastConcretePlantDatabaseImplement.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 DateOpening { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ReinforcedsMax { get; set; }
|
||||
|
||||
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforceds = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IReinforcedModel, int)> ShopReinforceds
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopReinforceds == null)
|
||||
{
|
||||
_shopReinforceds = Reinforceds
|
||||
.ToDictionary(x => x.ReinforcedId, x => (x.Reinforced as IReinforcedModel, x.Count));
|
||||
}
|
||||
return _shopReinforceds;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopReinforced> Reinforceds { get; set; } = new();
|
||||
|
||||
public static Shop Create(PrecastConcretePlantDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
ReinforcedsMax = model.ReinforcedsMax,
|
||||
Reinforceds = model.ShopReinforceds.Select(x => new ShopReinforced
|
||||
{
|
||||
Reinforced = context.Reinforceds.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;
|
||||
ReinforcedsMax = model.ReinforcedsMax;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
ReinforcedsMax = ReinforcedsMax,
|
||||
ShopReinforceds = ShopReinforceds
|
||||
};
|
||||
|
||||
public void UpdateReinforceds(PrecastConcretePlantDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopReinforceds = context.ShopReinforceds.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (shopReinforceds != null && shopReinforceds.Count > 0)
|
||||
{
|
||||
context.ShopReinforceds.RemoveRange(shopReinforceds.Where(rec => !model.ShopReinforceds.ContainsKey(rec.ReinforcedId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateReinforced in shopReinforceds)
|
||||
{
|
||||
if (model.ShopReinforceds.ContainsKey(updateReinforced.ReinforcedId))
|
||||
{
|
||||
updateReinforced.Count = model.ShopReinforceds[updateReinforced.ReinforcedId].Item2;
|
||||
model.ShopReinforceds.Remove(updateReinforced.ReinforcedId);
|
||||
}
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var ic in model.ShopReinforceds)
|
||||
{
|
||||
context.ShopReinforceds.Add(new ShopReinforced
|
||||
{
|
||||
Shop = shop,
|
||||
Reinforced = context.Reinforceds.First(x => x.Id == ic.Key),
|
||||
Count = ic.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopReinforceds = 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 PrecastConcretePlantDatabaseImplement.Models
|
||||
{
|
||||
public class ShopReinforced
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ReinforcedId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Reinforced Reinforced { get; set; } = new();
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
}
|
||||
}
|
@ -23,6 +23,8 @@ namespace PrecastConcretePlantDatabaseImplement
|
||||
public virtual DbSet<Reinforced> Reinforceds { set; get; }
|
||||
public virtual DbSet<ReinforcedComponent> ReinforcedComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
public virtual DbSet<ShopReinforced> ShopReinforceds { set; get; }
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user