Eliseev E.E. LabWork05_Hard #11
@ -28,5 +28,9 @@ namespace BlacksmithWorkshopDatabaseImplement
|
||||
public virtual DbSet<ManufactureWorkPiece> ManufactureWorkPieces { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
|
||||
public virtual DbSet<ShopManufacture> ShopManufactures { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Manufactures)
|
||||
.ThenInclude(x => x.Manufacture)
|
||||
@ -33,7 +33,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Manufactures)
|
||||
@ -51,7 +51,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
return context.Shops
|
||||
.Include(x => x.Manufactures)
|
||||
@ -63,7 +63,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var newShop = Shop.Create(context, model);
|
||||
|
||||
if (newShop == null)
|
||||
@ -79,7 +79,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
|
||||
try
|
||||
@ -93,7 +93,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
|
||||
shop.Update(model);
|
||||
context.SaveChanges();
|
||||
shop.UpdateIceCreams(context, model);
|
||||
shop.UpdateManufactures(context, model);
|
||||
transaction.Commit();
|
||||
|
||||
return shop.GetViewModel;
|
||||
@ -108,7 +108,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
|
||||
var element = context.Shops
|
||||
.Include(x => x.Manufactures)
|
||||
@ -127,15 +127,15 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
|
||||
public bool SellManufactures(IManufactureModel model, int count)
|
||||
{
|
||||
using var context = new ManufactureShopDatabase();
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
var shops = context.ShopManufactures
|
||||
.Include(x => x.Shop)
|
||||
.ToList()
|
||||
.Where(rec => rec.ManufactureId == model.Id);
|
||||
.Include(x => x.Shop)
|
||||
.ToList()
|
||||
.Where(rec => rec.ManufactureId == model.Id);
|
||||
|
||||
if (shops == null)
|
||||
{
|
||||
|
@ -1,34 +0,0 @@
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement
|
||||
{
|
||||
public class ManufactureShopDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=ManufactureShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public virtual DbSet<WorkPiece> WorkPieces { set; get; }
|
||||
|
||||
public virtual DbSet<Manufacture> Manufactures { set; get; }
|
||||
|
||||
public virtual DbSet<ManufactureWorkPiece> ManufactureWorkPieces { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
|
||||
public virtual DbSet<ShopManufacture> ShopManufactures { get; set; }
|
||||
}
|
||||
}
|
@ -44,7 +44,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopManufacture> Manufactures { get; set; } = new();
|
||||
|
||||
public static Shop? Create(ManufactureShopDatabase context, ShopBindingModel model)
|
||||
public static Shop? Create(BlacksmithWorkshopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
@ -89,20 +89,20 @@ namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
ShopManufactures = ShopManufactures
|
||||
};
|
||||
|
||||
public void UpdateIceCreams(ManufactureShopDatabase context, ShopBindingModel model)
|
||||
public void UpdateManufactures(BlacksmithWorkshopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopIceCreams = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
var shopManufactures = context.ShopManufactures.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
|
||||
if (shopIceCreams != null && shopIceCreams.Count > 0)
|
||||
if (shopManufactures != null && shopManufactures.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.ShopManufactures.RemoveRange(shopIceCreams.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId)));
|
||||
context.ShopManufactures.RemoveRange(shopManufactures.Where(rec => !model.ShopManufactures.ContainsKey(rec.ManufactureId)));
|
||||
context.SaveChanges();
|
||||
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateIceCream in shopIceCreams)
|
||||
foreach (var _shopManufactures in shopManufactures)
|
||||
{
|
||||
updateIceCream.Count = model.ShopManufactures[updateIceCream.ManufactureId].Item2;
|
||||
model.ShopManufactures.Remove(updateIceCream.ManufactureId);
|
||||
_shopManufactures.Count = model.ShopManufactures[_shopManufactures.ManufactureId].Item2;
|
||||
model.ShopManufactures.Remove(_shopManufactures.ManufactureId);
|
||||
}
|
||||
|
||||
context.SaveChanges();
|
||||
|
Loading…
Reference in New Issue
Block a user