laba3hard

This commit is contained in:
Дмитрий Блохин 2024-06-17 22:38:17 +04:00
parent 2717ece433
commit e852d86b6c
7 changed files with 694 additions and 0 deletions

View File

@ -22,5 +22,7 @@ namespace FishFactoryDatabaseImplement
public virtual DbSet<Canned> Canneds { set; get; }
public virtual DbSet<CannedComponent> CannedComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopCanned> ShopCanned { set; get; }
}
}

View File

@ -0,0 +1,155 @@
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryDatabaseImplement.Models;
using FishFactoryDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FishFactoryDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new FishFactoryDatabase();
return context.Shops
.Include(x => x.ListCannedFk)
.ThenInclude(x => x.Canned)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new FishFactoryDatabase();
return context.Shops
.Include(x => x.ListCannedFk)
.ThenInclude(x => x.Canned)
.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 FishFactoryDatabase();
return context.Shops
.Include(x => x.ListCannedFk)
.ThenInclude(x => x.Canned)
.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 FishFactoryDatabase();
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 FishFactoryDatabase();
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.UpdateCanned(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new FishFactoryDatabase();
var element = context.Shops
.Include(x => x.ListCannedFk)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellCanned(ICannedModel model, int count)
{
using var context = new FishFactoryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops
.Include(x => x.ListCannedFk)
.ThenInclude(x => x.Canned)
.ToList()
.Where(x => x.ListCanned.ContainsKey(model.Id)))
{
int countInCurrentShop = shop.ListCanned[model.Id].Item2;
if (countInCurrentShop <= count)
{
var elem = context.ShopCanned
.Where(x => x.CannedId == model.Id)
.FirstOrDefault(x => x.ShopId == shop.Id);
context.ShopCanned.Remove(elem);
shop.ListCanned.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ListCanned[model.Id] = (shop.ListCanned[model.Id].Item1, countInCurrentShop - count);
count = 0;
shop.UpdateCanned(context, new()
{
Id = shop.Id,
ListCanned = shop.ListCanned,
});
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,246 @@
// <auto-generated />
using System;
using FishFactoryDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FishFactoryDatabaseImplement.Migrations
{
[DbContext(typeof(FishFactoryDatabase))]
[Migration("20240617130014_lab3hard")]
partial class lab3hard
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.16")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("CannedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Canneds");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CannedId")
.HasColumnType("int");
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CannedId");
b.HasIndex("ComponentId");
b.ToTable("CannedComponents");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CannedId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("CannedId");
b.ToTable("Orders");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxCountCanned")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CannedId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CannedId");
b.HasIndex("ShopId");
b.ToTable("ShopCanned");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b =>
{
b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned")
.WithMany("Components")
.HasForeignKey("CannedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component")
.WithMany("CannedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Canned");
b.Navigation("Component");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b =>
{
b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null)
.WithMany("Orders")
.HasForeignKey("CannedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b =>
{
b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned")
.WithMany()
.HasForeignKey("CannedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop")
.WithMany("ListCannedFk")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Canned");
b.Navigation("Shop");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b =>
{
b.Navigation("CannedComponents");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b =>
{
b.Navigation("ListCannedFk");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FishFactoryDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class lab3hard : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCountCanned = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopCanned",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CannedId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopCanned", x => x.Id);
table.ForeignKey(
name: "FK_ShopCanned_Canneds_CannedId",
column: x => x.CannedId,
principalTable: "Canneds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopCanned_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopCanned_CannedId",
table: "ShopCanned",
column: "CannedId");
migrationBuilder.CreateIndex(
name: "IX_ShopCanned_ShopId",
table: "ShopCanned",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopCanned");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace FishFactoryDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<int>("MaxCountCanned")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CannedId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CannedId");
b.HasIndex("ShopId");
b.ToTable("ShopCanned");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b =>
{
b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned")
@ -149,6 +202,25 @@ namespace FishFactoryDatabaseImplement.Migrations
.IsRequired();
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b =>
{
b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned")
.WithMany()
.HasForeignKey("CannedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop")
.WithMany("ListCannedFk")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Canned");
b.Navigation("Shop");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b =>
{
b.Navigation("Components");
@ -160,6 +232,11 @@ namespace FishFactoryDatabaseImplement.Migrations
{
b.Navigation("CannedComponents");
});
modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b =>
{
b.Navigation("ListCannedFk");
});
#pragma warning restore 612, 618
}
}

View File

@ -0,0 +1,109 @@
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels.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 FishFactoryDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
[Required]
public string ShopName { get; private set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
[Required]
public DateTime DateOpening { get; private set; }
[Required]
public int MaxCountCanned { get; private set; }
private Dictionary<int, (ICannedModel, int)>? _shopCanned = null;
[NotMapped]
public Dictionary<int, (ICannedModel, int)> ListCanned
{
get
{
if (_shopCanned == null)
{
_shopCanned = ListCannedFk
.ToDictionary(recPC => recPC.CannedId, recPC => (recPC.Canned as ICannedModel, recPC.Count));
}
return _shopCanned;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopCanned> ListCannedFk { get; set; } = new();
public static Shop Create(FishFactoryDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
MaxCountCanned = model.MaxCountCanned,
ListCannedFk = model.ListCanned.Select(x => new ShopCanned
{
Canned = context.Canneds.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;
MaxCountCanned = model.MaxCountCanned;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
MaxCountCanned = MaxCountCanned,
ListCanned = ListCanned
};
public void UpdateCanned(FishFactoryDatabase context, ShopBindingModel model)
{
var shopCanned = context.ShopCanned.Where(rec => rec.ShopId == model.Id).ToList();
if (shopCanned != null && shopCanned.Count > 0)
{ // удалили те, которых нет в модели
context.ShopCanned.RemoveRange(shopCanned.Where(rec => !model.ListCanned.ContainsKey(rec.CannedId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateCanned in shopCanned)
{
updateCanned.Count = model.ListCanned[updateCanned.CannedId].Item2;
model.ListCanned.Remove(updateCanned.CannedId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ss in model.ListCanned)
{
context.ShopCanned.Add(new ShopCanned
{
Shop = shop,
Canned = context.Canneds.First(x => x.Id == ss.Key),
Count = ss.Value.Item2
});
context.SaveChanges();
}
_shopCanned = null;
}
}
}

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 FishFactoryDatabaseImplement.Models
{
public class ShopCanned
{
public int Id { get; set; }
[Required]
public int CannedId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Canned Canned { get; set; } = new();
}
}