Compare commits

..

3 Commits

Author SHA1 Message Date
parap
f80eb3c08c lab3 full 2023-04-24 15:56:53 +04:00
parap
205cc3df40 Merge branch 'lab2_hard' of http://student.git.athene.tech/maxKarme/PIbd-22_Karamushko_M_K_Pizzeria into lab3_hard 2023-04-23 20:52:44 +04:00
parap
d4b269564b refactoring 2023-04-23 20:52:05 +04:00
10 changed files with 449 additions and 68 deletions

View File

@ -22,7 +22,6 @@
<ProjectReference Include="..\PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj" />
<ProjectReference Include="..\PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj" />
<ProjectReference Include="..\PizzeriaDataModels\PizzeriaDataModels.csproj" />
<ProjectReference Include="..\PizzeriaShopFileImplement\PizzeriaFileImplement.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -12,13 +12,13 @@ namespace PizzeriaBusinessLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IPizzaStorage _pizzaStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage, IPizzaStorage pizzaStorage)
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPizzaStorage pizzaStorage)
{
_logger = logger;
_orderStorage = orderStorage;
_shopStorage = shopStorage;
_shopLogic = shopLogic;
_pizzaStorage = pizzaStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
@ -74,7 +74,7 @@ namespace PizzeriaBusinessLogic
{
var pizza = _pizzaStorage.GetElement(new PizzaSearchModel { Id = model.PizzaId });
if(!_shopStorage.TryRestoreShops(pizza, model.Count))
if(!TryRestoreShops(pizza, model.Count))
{
_logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Incorrect count.");
throw new ArgumentException("Количество изделий слишком велико");
@ -124,5 +124,38 @@ namespace PizzeriaBusinessLogic
_logger.LogInformation("Sum:{ Sum}. Id: { Id}", model.Sum, model.Id);
}
private bool TryRestoreShops(IPizzaModel pizza, int count)
{
int freePlaces = 0;
_shopLogic.ReadList(null).ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var pair in x.ShopPizzas)
{
currentFreePlaces -= pair.Value.Item2;
}
freePlaces += currentFreePlaces;
});
if (freePlaces < count) return false;
_shopLogic.ReadList(null).ForEach(x =>
{
if (count <= 0) return;
int currentFreePlaces = x.Capacity;
foreach (var elem in x.ShopPizzas)
{
currentFreePlaces -= elem.Value.Item2;
}
_shopLogic.AddPizza(new ShopSearchModel { Id = x.Id }, pizza, Math.Min(currentFreePlaces, count));
count -= currentFreePlaces;
});
return true;
}
}
}

View File

@ -141,6 +141,7 @@ namespace PizzeriaBusinessLogic
ShopName = model.ShopName,
OpenTime = model.OpenTime,
Addres = model.Addres,
Capacity = model.Capacity,
ShopPizzas = model.ShopPizzas,
});

View File

@ -11,7 +11,6 @@ namespace PizzeriaContracts.StoragesContracts
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
ShopViewModel? GetElement(ShopSearchModel model);
bool TrySellPizza(IPizzaModel pizza, int count);
bool TryRestoreShops(IPizzaModel pizza, int count);
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);

View File

@ -0,0 +1,248 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using PizzeriaDatabaseImplement;
#nullable disable
namespace PizzeriaDatabaseImplement.Migrations
{
[DbContext(typeof(PizzeriaDatabase))]
[Migration("20230423174224_AddShop")]
partial class AddShop
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Cost")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<DateTime>("DateCreate")
.HasColumnType("timestamp with time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp with time zone");
b.Property<int>("PizzaId")
.HasColumnType("integer");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.ToTable("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("PizzaName")
.IsRequired()
.HasColumnType("text");
b.Property<double>("Price")
.HasColumnType("double precision");
b.HasKey("Id");
b.ToTable("Pizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ComponentId")
.HasColumnType("integer");
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("PizzaId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("PizzaId");
b.ToTable("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Addres")
.IsRequired()
.HasColumnType("text");
b.Property<int>("Capacity")
.HasColumnType("integer");
b.Property<DateTime>("OpenTime")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.ShopPizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("PizzaId")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.HasIndex("ShopId");
b.ToTable("ShopPizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b =>
{
b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza")
.WithMany("Orders")
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.PizzaComponent", b =>
{
b.HasOne("PizzeriaDatabaseImplement.models.Component", "Component")
.WithMany("PizzaComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza")
.WithMany("Components")
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.ShopPizza", b =>
{
b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza")
.WithMany()
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.models.Shop", "Shop")
.WithMany("Pizzas")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
b.Navigation("Shop");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b =>
{
b.Navigation("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Pizza", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Shop", b =>
{
b.Navigation("Pizzas");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,79 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace PizzeriaDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class AddShop : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
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),
Addres = table.Column<string>(type: "text", nullable: false),
OpenTime = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
Capacity = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ShopPizzas",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
PizzaId = 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_ShopPizzas", x => x.Id);
table.ForeignKey(
name: "FK_ShopPizzas_Pizzas_PizzaId",
column: x => x.PizzaId,
principalTable: "Pizzas",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopPizzas_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_ShopPizzas_PizzaId",
table: "ShopPizzas",
column: "PizzaId");
migrationBuilder.CreateIndex(
name: "IX_ShopPizzas_ShopId",
table: "ShopPizzas",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ShopPizzas");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace PizzeriaDatabaseImplement.Migrations
b.ToTable("PizzaComponents");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Addres")
.IsRequired()
.HasColumnType("text");
b.Property<int>("Capacity")
.HasColumnType("integer");
b.Property<DateTime>("OpenTime")
.HasColumnType("timestamp with time zone");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.ShopPizza", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("integer");
b.Property<int>("PizzaId")
.HasColumnType("integer");
b.Property<int>("ShopId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PizzaId");
b.HasIndex("ShopId");
b.ToTable("ShopPizzas");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Order", b =>
{
b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza")
@ -151,6 +204,25 @@ namespace PizzeriaDatabaseImplement.Migrations
b.Navigation("Pizza");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.ShopPizza", b =>
{
b.HasOne("PizzeriaDatabaseImplement.models.Pizza", "Pizza")
.WithMany()
.HasForeignKey("PizzaId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PizzeriaDatabaseImplement.models.Shop", "Shop")
.WithMany("Pizzas")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pizza");
b.Navigation("Shop");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Component", b =>
{
b.Navigation("PizzaComponents");
@ -162,6 +234,11 @@ namespace PizzeriaDatabaseImplement.Migrations
b.Navigation("Orders");
});
modelBuilder.Entity("PizzeriaDatabaseImplement.models.Shop", b =>
{
b.Navigation("Pizzas");
});
#pragma warning restore 612, 618
}
}

View File

@ -3,13 +3,11 @@ using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace PizzeriaDatabaseImplement.models
{
public class Shop : IShopModel
{
[Required]
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
@ -43,8 +41,7 @@ namespace PizzeriaDatabaseImplement.models
Addres = model.Addres,
OpenTime = model.OpenTime,
Capacity = model.Capacity,
Pizzas = model.ShopPizzas.Select(x => new
ShopPizza
Pizzas = model.ShopPizzas.Select(x => new ShopPizza
{
Pizza = context.Pizzas.First(y => y.Id == x.Key),
Count = x.Value.Item2

View File

@ -2,6 +2,7 @@
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels;
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement.Implements
@ -112,5 +113,10 @@ namespace PizzeriaListImplement.Implements
return null;
}
public bool TrySellPizza(IPizzaModel pizza, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -42,64 +42,6 @@ namespace PizzeriaFileImplement.Implements
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public bool TryRestoreShops(IPizzaModel pizza, int count)
{
int freePlaces = 0;
source.Shops.ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var pair in x.ShopPizzas)
{
currentFreePlaces -= pair.Value.Item2;
}
freePlaces += currentFreePlaces;
});
if (freePlaces < count) return false;
source.Shops.ForEach(x =>
{
int currentFreePlaces = x.Capacity;
foreach (var elem in x.ShopPizzas)
{
currentFreePlaces -= elem.Value.Item2;
}
if(!x.ShopPizzas.TryGetValue(pizza.Id, out var check))
{
x.ShopPizzas.Add(pizza.Id, (pizza, 0));
}
x.ShopPizzas.TryGetValue(pizza.Id, out var pair);
if (count >= currentFreePlaces)
{
count -= currentFreePlaces;
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + currentFreePlaces);
}
else
{
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 + count);
count = 0;
}
x.Update(new ShopBindingModel
{
Id = x.Id,
Addres = x.Addres,
Capacity = x.Capacity,
OpenTime = x.OpenTime,
ShopName = x.ShopName,
ShopPizzas = x.ShopPizzas
});
});
source.SaveShops();
return true;
}
public bool TrySellPizza(IPizzaModel pizza, int count)
{
int hasCount = 0;