WIP: ПИбд-23 Тихоненков Алексей Евгеньевич Лабораторная работа №4 (Усложненная) #13

Closed
YourDax wants to merge 11 commits from Lab4HARD into Lab4
8 changed files with 589 additions and 1 deletions
Showing only changes of commit b6d15ab948 - Show all commits

View File

@ -22,5 +22,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; }
}
}

View File

@ -0,0 +1,209 @@
using CarpentryWorkshopContracts.BindingModels;
using CarpentryWorkshopContracts.SearchModels;
using CarpentryWorkshopContracts.StoragesContracts;
using CarpentryWorkshopContracts.ViewModels;
using CarpentryWorkshopDatabaseImplement.Models;
using CarpentryWorkshopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 RestockingShops(SupplyBindingModel model)
{
using var context = new CarpentryWorkshopDatabase();
var transaction = context.Database.BeginTransaction();
var Shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList().
Where(x => x.WoodMaxCount > x.ShopWoods.Select(x => x.Value.Item2).Sum()).ToList();
if (model == null)
{
return false;
}
try
{
foreach (Shop shop in Shops)
{
int difference = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum();
int refill = Math.Min(difference, model.Count);
model.Count -= refill;
if (shop.ShopWoods.ContainsKey(model.WoodId))
{
var datePair = shop.ShopWoods[model.WoodId];
datePair.Item2 += refill;
shop.ShopWoods[model.WoodId] = datePair;
}
else
{
var Wood = context.Woods.First(x => x.Id == model.WoodId);
shop.ShopWoods.Add(model.WoodId, (Wood, refill));
}
shop.WoodsDictionatyUpdate(context);
if (model.Count == 0)
{
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
public bool Sale(SupplySearchModel model)
{
using var context = new CarpentryWorkshopDatabase();
var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops.Include(x => x.Woods).ThenInclude(x => x.Wood).ToList().
Where(x => x.ShopWoods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.ShopWoods[model.WoodId.Value].Item2).ToList();
foreach (var shop in shops)
{
int residue = model.Count.Value - shop.ShopWoods[model.WoodId.Value].Item2;
if (residue > 0)
{
shop.ShopWoods.Remove(model.WoodId.Value);
shop.WoodsDictionatyUpdate(context);
context.SaveChanges();
model.Count = residue;
}
else
{
if (residue == 0)
shop.ShopWoods.Remove(model.WoodId.Value);
else
{
var dataPair = shop.ShopWoods[model.WoodId.Value];
dataPair.Item2 = -residue;
shop.ShopWoods[model.WoodId.Value] = dataPair;
}
shop.WoodsDictionatyUpdate(context);
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace CarpentryWorkshopDatabaseImplement.Migrations
{
[DbContext(typeof(CarpentryWorkshopDatabase))]
[Migration("20240319095810_InitialCreate")]
[Migration("20240511223517_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -82,6 +82,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("DateOpen")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WoodMaxCount")
.HasColumnType("integer");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.Property<int>("WoodId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("WoodId");
b.ToTable("ShopWoods");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b =>
{
b.Property<int>("Id")
@ -137,6 +190,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
.IsRequired();
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b =>
{
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Woods")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood")
.WithMany("ShopWoods")
.HasForeignKey("WoodId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Wood");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b =>
{
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component")
@ -161,11 +233,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
b.Navigation("WoodComponents");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Woods");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopWoods");
});
#pragma warning restore 612, 618
}

View File

@ -26,6 +26,22 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ShopName = table.Column<string>(type: "text", nullable: false),
Address = table.Column<string>(type: "text", nullable: false),
DateOpen = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
WoodMaxCount = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Woods",
columns: table => new
@ -65,6 +81,33 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopWoods",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
WoodId = table.Column<int>(type: "integer", nullable: false),
ShopId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopWoods", x => x.Id);
table.ForeignKey(
name: "FK_ShopWoods_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopWoods_Woods_WoodId",
column: x => x.WoodId,
principalTable: "Woods",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "WoodComponents",
columns: table => new
@ -97,6 +140,16 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
table: "Orders",
column: "WoodId");
migrationBuilder.CreateIndex(
name: "IX_ShopWoods_ShopId",
table: "ShopWoods",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ShopWoods_WoodId",
table: "ShopWoods",
column: "WoodId");
migrationBuilder.CreateIndex(
name: "IX_WoodComponents_ComponentId",
table: "WoodComponents",
@ -114,9 +167,15 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopWoods");
migrationBuilder.DropTable(
name: "WoodComponents");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropTable(
name: "Components");

View File

@ -79,6 +79,59 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("DateOpen")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WoodMaxCount")
.HasColumnType("integer");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.Property<int>("WoodId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("WoodId");
b.ToTable("ShopWoods");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b =>
{
b.Property<int>("Id")
@ -134,6 +187,25 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
.IsRequired();
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.ShopWood", b =>
{
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Woods")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Wood", "Wood")
.WithMany("ShopWoods")
.HasForeignKey("WoodId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Wood");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.WoodComponent", b =>
{
b.HasOne("CarpentryWorkshopDatabaseImplement.Models.Component", "Component")
@ -158,11 +230,18 @@ namespace CarpentryWorkshopDatabaseImplement.Migrations
b.Navigation("WoodComponents");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Woods");
});
modelBuilder.Entity("CarpentryWorkshopDatabaseImplement.Models.Wood", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopWoods");
});
#pragma warning restore 612, 618
}

View File

@ -0,0 +1,131 @@
using CarpentryWorkshopContracts.BindingModels;
using CarpentryWorkshopContracts.ViewModels;
using CarpentryWorkshopDataModels.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 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;
}
public void WoodsDictionatyUpdate(CarpentryWorkshopDatabase context)
{
UpdateWoods(context, new ShopBindingModel
{
Id = Id,
ShopWoods = ShopWoods,
});
}
}
}

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 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();
}
}

View File

@ -31,6 +31,8 @@ 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)
{
return new Wood()