3 hard complete

This commit is contained in:
Denis 2023-06-14 00:01:27 +04:00
parent e5b5927e90
commit 76f57dd5c7
13 changed files with 784 additions and 81 deletions

View File

@ -13,9 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{C58840E6-D68D-476E-AB3A-EB13F1740FC8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87E9042D-E27B-4185-9FE2-92F15D68F8D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{87156689-2FDA-4065-90CC-749B2FE3C0D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -43,14 +43,14 @@ Global
{C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C58840E6-D68D-476E-AB3A-EB13F1740FC8}.Release|Any CPU.Build.0 = Release|Any CPU
{87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87E9042D-E27B-4185-9FE2-92F15D68F8D4}.Release|Any CPU.Build.0 = Release|Any CPU
{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99C4FA4A-EBAC-49E3-8D7F-C9177F9876B2}.Release|Any CPU.Build.0 = Release|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87156689-2FDA-4065-90CC-749B2FE3C0D6}.Release|Any CPU.Build.0 = Release|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77B5E3FA-7A48-41AD-BB77-6EBF273550AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

View File

@ -100,7 +100,7 @@ namespace FlowerShopBusinessLogic.BusinessLogics
return false;
}
if (!_shopLogic.CheckThenSupplyMany(bouquet, order.Count))
if (!CheckThenSupplyMany(bouquet, order.Count))
{
_logger.LogWarning("Status update to Готов operation failed. Shop supply error.");
throw new Exception("В магазинах не хватает места!");
@ -165,5 +165,72 @@ namespace FlowerShopBusinessLogic.BusinessLogics
throw new InvalidOperationException("Заказ с таким идентификатором уже есть");
}
}
public bool CheckThenSupplyMany(IBouquetModel bouquet, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Bouquet count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountBouquets;
foreach (var bouq in shop.ShopBouquets)
{
freeSpace -= bouq.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new bouquets in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountBouquets;
foreach (var bouq in shop.ShopBouquets)
{
freeSpace -= bouq.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.SupplyBouquets(new() { Id = shop.Id }, bouquet, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
}
}

View File

@ -199,71 +199,5 @@ namespace FlowerShopBusinessLogic.BusinessLogics
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
public bool CheckThenSupplyMany(IBouquetModel bouquet, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. Bouquet count < 0.");
return false;
}
int freeSpace = 0;
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace += shop.MaxCountBouquets;
foreach (var bouq in shop.ShopBouquets)
{
freeSpace -= bouq.Value.Item2;
}
}
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new bouquets in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCountBouquets;
foreach (var bouq in shop.ShopBouquets)
{
freeSpace -= bouq.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (SupplyBouquets(new() { Id = shop.Id }, bouquet, count)) count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (SupplyBouquets(new() { Id = shop.Id }, bouquet, freeSpace)) count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
}
}

View File

@ -14,6 +14,5 @@ namespace FlowerShopContracts.BusinessLogicsContracts
bool Delete(ShopBindingModel model);
bool SupplyBouquets(ShopSearchModel model, IBouquetModel bouquet, int count);
bool SellBouquet(IBouquetModel bouquet, int count);
bool CheckThenSupplyMany(IBouquetModel bouquet, int count);
}
}

View File

@ -1,6 +1,5 @@
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Client;
namespace FlowerShopDatabaseImplement
{
@ -10,7 +9,7 @@ namespace FlowerShopDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Server=DESKTOP-A68O3K0;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
@ -18,5 +17,7 @@ namespace FlowerShopDatabaseImplement
public virtual DbSet<Bouquet> Bouquets { set; get; }
public virtual DbSet<BouquetComponent> BouquetComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopBouquet> ShopBouquets { set; get; }
}
}

View File

@ -89,6 +89,7 @@ namespace FlowerShopDatabaseImplement.Implements
}
bouquet.Update(model);
bouquet.UpdateComponents(context, model);
context.SaveChanges();
context.Database.CommitTransaction();

View File

@ -0,0 +1,162 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using FlowerShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
namespace FlowerShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Shops.Include(x => x.Bouquets)
.ThenInclude(x => x.Bouquet)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new FlowerShopDatabase();
return context.Shops.Include(x => x.Bouquets).ThenInclude(x => x.Bouquet).Where(x => x.Name.Contains(model.Name)).ToList().Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new FlowerShopDatabase();
return context.Shops.Include(x => x.Bouquets).ThenInclude(x => x.Bouquet).ToList().Select(x => x.GetViewModel).ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
if (context.Shops.Any(x => x.Name == newShop.Name))
{
throw new Exception("Название магазина уже занято");
}
context.Shops.Add(newShop);
context.SaveChanges();
transaction.Commit();
return newShop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shop = context.Shops.Include(x => x.Bouquets).FirstOrDefault(x => x.Id == model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
context.SaveChanges();
if (model.ShopBouquets.Count > 0)
{
shop.UpdateBouquets(context, model);
}
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new FlowerShopDatabase();
var shop = context.Shops.Include(x => x.Bouquets).FirstOrDefault(x => x.Id == model.Id);
if (shop != null)
{
context.Shops.Remove(shop);
context.SaveChanges();
return shop.GetViewModel;
}
return null;
}
public bool SellBouquet(IBouquetModel model, int count)
{
using var context = new FlowerShopDatabase();
using var transaction = context.Database.BeginTransaction();
foreach (var shopBouquets in context.ShopBouquets.Where(x => x.BouquetId == model.Id))
{
var min = Math.Min(count, shopBouquets.Count);
shopBouquets.Count -= min;
count -= min;
if (count <= 0)
{
break;
}
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
}
else
{
transaction.Rollback();
}
if (count > 0)
{
return false;
}
return true;
}
}
}

View File

@ -0,0 +1,248 @@
// <auto-generated />
using System;
using FlowerShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
[DbContext(typeof(FlowerShopDatabase))]
[Migration("20230420224142_ShopsAdded")]
partial class ShopsAdded
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("BouquetName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Bouquets");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.HasColumnType("int");
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BouquetId");
b.HasIndex("ComponentId");
b.ToTable("BouquetComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.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("BouquetId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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<int>("MaxCountBouquets")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BouquetId");
b.HasIndex("ShopId");
b.ToTable("ShopBouquets");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany("Components")
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("BouquetComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
b.Navigation("Component");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany("Orders")
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany()
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Bouquets")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
b.Navigation("Shop");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("BouquetComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Bouquets");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,78 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class ShopsAdded : 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"),
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCountBouquets = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopBouquets",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
BouquetId = 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_ShopBouquets", x => x.Id);
table.ForeignKey(
name: "FK_ShopBouquets_Bouquets_BouquetId",
column: x => x.BouquetId,
principalTable: "Bouquets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopBouquets_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopBouquets_BouquetId",
table: "ShopBouquets",
column: "BouquetId");
migrationBuilder.CreateIndex(
name: "IX_ShopBouquets_ShopId",
table: "ShopBouquets",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopBouquets");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace FlowerShopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.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<int>("MaxCountBouquets")
.HasColumnType("int");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("OpeningDate")
.HasColumnType("datetime2");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BouquetId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BouquetId");
b.HasIndex("ShopId");
b.ToTable("ShopBouquets");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.BouquetComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
@ -151,6 +204,25 @@ namespace FlowerShopDatabaseImplement.Migrations
b.Navigation("Bouquet");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopBouquet", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Bouquet", "Bouquet")
.WithMany()
.HasForeignKey("BouquetId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("Bouquets")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Bouquet");
b.Navigation("Shop");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Bouquet", b =>
{
b.Navigation("Components");
@ -162,6 +234,11 @@ namespace FlowerShopDatabaseImplement.Migrations
{
b.Navigation("BouquetComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Bouquets");
});
#pragma warning restore 612, 618
}
}

View File

@ -0,0 +1,114 @@
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime OpeningDate { get; set; }
[ForeignKey("ShopId")]
public List<ShopBouquet> Bouquets { get; set; } = new();
private Dictionary<int, (IBouquetModel, int)>? _shopBouquets = null;
[NotMapped]
public Dictionary<int, (IBouquetModel, int)> ShopBouquets
{
get
{
if (_shopBouquets == null)
{
_shopBouquets = Bouquets.ToDictionary(recPC => recPC.BouquetId, recPC => (recPC.Bouquet as IBouquetModel, recPC.Count));
}
return _shopBouquets;
}
}
[Required]
public int MaxCountBouquets { get; set; }
public static Shop Create(FlowerShopDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
OpeningDate = model.OpeningDate,
Bouquets = model.ShopBouquets.Select(x => new ShopBouquet
{
Bouquet = context.Bouquets.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
MaxCountBouquets = model.MaxCountBouquets
};
}
public void Update(ShopBindingModel model)
{
Name = model.Name;
Address = model.Address;
OpeningDate = model.OpeningDate;
MaxCountBouquets = model.MaxCountBouquets;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
OpeningDate = OpeningDate,
ShopBouquets = ShopBouquets,
MaxCountBouquets = MaxCountBouquets
};
public void UpdateBouquets(FlowerShopDatabase context, ShopBindingModel model)
{
var ShopBouquets = context.ShopBouquets.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopBouquets != null && this.ShopBouquets.Count > 0)
{
context.ShopBouquets.RemoveRange(ShopBouquets.Where((ShopBouquet rec) => !model.ShopBouquets.ContainsKey(rec.BouquetId)));
context.SaveChanges();
ShopBouquets = context.ShopBouquets.Where((ShopBouquet rec) => rec.ShopId == model.Id).ToList();
foreach (var updateBouquet in ShopBouquets)
{
updateBouquet.Count = model.ShopBouquets[updateBouquet.BouquetId].Item2;
model.ShopBouquets.Remove(updateBouquet.BouquetId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var elem in model.ShopBouquets)
{
context.ShopBouquets.Add(new ShopBouquet
{
Shop = shop,
Bouquet = context.Bouquets.First(x => x.Id == elem.Key),
Count = elem.Value.Item2
});
context.SaveChanges();
}
_shopBouquets = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.Models
{
public class ShopBouquet
{
public int Id { get; set; }
[Required]
public int BouquetId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Bouquet Bouquet { get; set; } = new();
}
}