вроде готовая сложная третья лаба для показа, но напрашивается еще добавить норм отображение ошибки о количестве суши в магазинах

This commit is contained in:
Елена Бакальская 2024-04-21 23:51:28 +04:00
parent 57c68d01a8
commit 824d6c2af8
7 changed files with 467 additions and 6 deletions

View File

@ -5,7 +5,7 @@ using SushiBarBusinessLogic;
using SushiBarBusinessLogic.BusinessLogic;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.StoragesContracts;
using SushiBarFileImplement.Implements;
using SushiBarDatabaseImplement.Implements;
using SushiBarView;
using SushiBarView.Shops;

View File

@ -1,4 +1,10 @@
using SushiBarContracts.StoragesContracts;
using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using SushiBarDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,5 +15,246 @@ namespace SushiBarDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.Where(x => x.ShopName.Contains(model.ShopName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var newShop = Shop.Create(context, model);
if (newShop == null)
{
return null;
}
context.Shops.Add(newShop);
context.SaveChanges();
return newShop.GetViewModel;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Update(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
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.UpdateSushis(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Shops
.Include(x => x.Sushis)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
/// <summary>
/// Продажа изделий
/// </summary>
/// <param name="model"></param>
/// <param name="count"></param>
/// <returns></returns>
public bool SellSushis(ISushiModel model, int count)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var shops = context.Shops
.Include(x => x.Sushis)
.ThenInclude(x => x.Sushi)
.ToList()
.Where(x => x.ShopSushis.ContainsKey(model.Id));
foreach (var shop in shops)
{
int countInCurrentShop = shop.ShopSushis[model.Id].Item2;
if (countInCurrentShop <= count)
{
var elem = context.ShopSushis
.Where(x => x.SushiId == model.Id)
.FirstOrDefault(x => x.ShopId == shop.Id);
context.ShopSushis.Remove(elem);
shop.ShopSushis.Remove(model.Id);
count -= countInCurrentShop;
}
else
{
shop.ShopSushis[model.Id] = (shop.ShopSushis[model.Id].Item1, countInCurrentShop - count);
count = 0;
shop.UpdateSushis(context, new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopSushis = shop.ShopSushis,
MaxCountSushis = shop.MaxCountSushis
});
}
if (count <= 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Проверка наличия в нужном количестве
/// </summary>
/// <param name="model"></param>
/// <param name="count"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public bool CheckCountSushi(ISushiModel model, int count)
{
throw new NotImplementedException();
}
/*public bool CheckCountSushi(ISushiModel model, int count)
{
int store = _source.Shops.Select(x => x.ShopSushis.Select(y =>
(y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)).Sum()).Sum();
return store >= count;
}
public bool SellSushis(ISushiModel model, int count)
{
var sushi = _source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (sushi == null || !CheckCountSushi(model, count))
{
throw new ArgumentNullException("Такого количества суш нет, продать столько низя ");
}
foreach (var shop in _source.Shops)
{
var sushis = shop.ShopSushis;
foreach (var elem in sushis.Where(x => x.Value.Item1.Id == sushi.Id))
{
var selling = Math.Min(elem.Value.Item2, count);
sushis[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling);
count -= selling;
if (count <= 0)
{
break;
}
}
shop.Update(new ShopBindingModel
{
Id = model.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOpening = shop.DateOpening,
ShopSushis = sushis,
MaxCountSushis = shop.MaxCountSushis
});
}
_source.SaveShops();
return true;
}*/
}
}

View File

@ -12,7 +12,7 @@ using SushiBarDatabaseImplement;
namespace SushiBarDatabaseImplement.Migrations
{
[DbContext(typeof(SushiBarDatabase))]
[Migration("20240310073215_InitCreate")]
[Migration("20240421182209_InitCreate")]
partial class InitCreate
{
/// <inheritdoc />
@ -78,6 +78,59 @@ namespace SushiBarDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.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>("MaxCountSushis")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SushiId");
b.ToTable("ShopSushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
@ -133,6 +186,25 @@ namespace SushiBarDatabaseImplement.Migrations
.IsRequired();
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop")
.WithMany("Sushis")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany()
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component")
@ -157,6 +229,11 @@ namespace SushiBarDatabaseImplement.Migrations
b.Navigation("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Sushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Components");

View File

@ -25,6 +25,22 @@ namespace SushiBarDatabaseImplement.Migrations
table.PrimaryKey("PK_Components", x => x.Id);
});
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),
MaxCountSushis = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Sushis",
columns: table => new
@ -63,6 +79,33 @@ namespace SushiBarDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopSushis",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SushiId = 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_ShopSushis", x => x.Id);
table.ForeignKey(
name: "FK_ShopSushis_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopSushis_Sushis_SushiId",
column: x => x.SushiId,
principalTable: "Sushis",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SushiComponents",
columns: table => new
@ -95,6 +138,16 @@ namespace SushiBarDatabaseImplement.Migrations
table: "Orders",
column: "SushiId");
migrationBuilder.CreateIndex(
name: "IX_ShopSushis_ShopId",
table: "ShopSushis",
column: "ShopId");
migrationBuilder.CreateIndex(
name: "IX_ShopSushis_SushiId",
table: "ShopSushis",
column: "SushiId");
migrationBuilder.CreateIndex(
name: "IX_SushiComponents_ComponentId",
table: "SushiComponents",
@ -112,9 +165,15 @@ namespace SushiBarDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopSushis");
migrationBuilder.DropTable(
name: "SushiComponents");
migrationBuilder.DropTable(
name: "Shops");
migrationBuilder.DropTable(
name: "Components");

View File

@ -75,6 +75,59 @@ namespace SushiBarDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.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>("MaxCountSushis")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.Property<int>("SushiId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ShopId");
b.HasIndex("SushiId");
b.ToTable("ShopSushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
@ -130,6 +183,25 @@ namespace SushiBarDatabaseImplement.Migrations
.IsRequired();
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.ShopSushi", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Shop", "Shop")
.WithMany("Sushis")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany()
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Shop");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component")
@ -154,6 +226,11 @@ namespace SushiBarDatabaseImplement.Migrations
b.Navigation("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Sushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Components");

View File

@ -56,6 +56,7 @@ namespace SushiBarDatabaseImplement.Models
ShopName = model.ShopName,
Address = model.Address,
MaxCountSushis = model.MaxCountSushis,
DateOpening = model.DateOpening,
Sushis = model.ShopSushis.Select(x => new ShopSushi
{
Sushi = context.Sushis.First(y => y.Id == x.Key), Count = x.Value.Item2
@ -78,7 +79,7 @@ namespace SushiBarDatabaseImplement.Models
MaxCountSushis = MaxCountSushis,
ShopSushis = ShopSushis
};
public void UpdateComponents(SushiBarDatabase context, ShopBindingModel model)
public void UpdateSushis(SushiBarDatabase context, ShopBindingModel model)
{
var ShopSushis = context.ShopSushis.Where(rec => rec.ShopId == model.Id).ToList();
if (ShopSushis != null && ShopSushis.Count > 0)

View File

@ -10,8 +10,8 @@ namespace SushiBarDatabaseImplement
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-78EBD1B\SQLEXPRESS;
Initial Catalog=SushiBarDatabase;
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;
Initial Catalog=SushiBarDatabaseLC3;
Integrated Security=True;
MultipleActiveResultSets=True;;
TrustServerCertificate=True"