Добавлена сущность магазин и ее связь с изделием многие ко многим
This commit is contained in:
parent
2ae833bcd5
commit
c346aa79df
@ -24,5 +24,6 @@ namespace ConfectioneryDatabaseImplement
|
||||
public virtual DbSet<Pastry> Pastries { set; get; }
|
||||
public virtual DbSet<PastryComponent> PastryComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
}
|
||||
}
|
||||
|
262
ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs
generated
Normal file
262
ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs
generated
Normal file
@ -0,0 +1,262 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using ConfectioneryDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(ConfectioneryDatabase))]
|
||||
[Migration("20230228200344_create_shop")]
|
||||
partial class create_shop
|
||||
{
|
||||
/// <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("ConfectioneryDatabaseImplement.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("ConfectioneryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime?>("DateImplement")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("PastryName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Pastries");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("ComponentId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.ToTable("PastryComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.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>("MaxCountPastries")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int?>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPastry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("PastryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Pastry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("PastryComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("PastryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Pastry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null)
|
||||
.WithMany("Shops")
|
||||
.HasForeignKey("PastryId");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
|
||||
.WithMany()
|
||||
.HasForeignKey("PastryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("ShopPastries")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Pastry");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("PastryComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("ShopPastries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class create_shop : 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),
|
||||
MaxCountPastries = table.Column<int>(type: "int", nullable: false),
|
||||
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
PastryId = table.Column<int>(type: "int", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Shops_Pastries_PastryId",
|
||||
column: x => x.PastryId,
|
||||
principalTable: "Pastries",
|
||||
principalColumn: "Id");
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopPastry",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
PastryId = 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_ShopPastry", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPastry_Pastries_PastryId",
|
||||
column: x => x.PastryId,
|
||||
principalTable: "Pastries",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPastry_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPastry_PastryId",
|
||||
table: "ShopPastry",
|
||||
column: "PastryId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPastry_ShopId",
|
||||
table: "ShopPastry",
|
||||
column: "ShopId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Shops_PastryId",
|
||||
table: "Shops",
|
||||
column: "PastryId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopPastry");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,64 @@ namespace ConfectioneryDatabaseImplement.Migrations
|
||||
b.ToTable("PastryComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.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>("MaxCountPastries")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int?>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PastryId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PastryId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPastry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
|
||||
@ -151,6 +209,32 @@ namespace ConfectioneryDatabaseImplement.Migrations
|
||||
b.Navigation("Pastry");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null)
|
||||
.WithMany("Shops")
|
||||
.HasForeignKey("PastryId");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b =>
|
||||
{
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
|
||||
.WithMany()
|
||||
.HasForeignKey("PastryId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("ShopPastries")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Pastry");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("PastryComponents");
|
||||
@ -161,6 +245,13 @@ namespace ConfectioneryDatabaseImplement.Migrations
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("ShopPastries");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
|
@ -39,6 +39,8 @@ namespace ConfectioneryDatabaseImplement.Models
|
||||
public virtual List<PastryComponent> Components { get; set; } = new();
|
||||
[ForeignKey("PastryId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
[ForeignKey("PastryId")]
|
||||
public virtual List<Shop> Shops { get; set; } = new();
|
||||
|
||||
public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model)
|
||||
{
|
||||
|
84
ConfectioneryDatabaseImplement/Shop.cs
Normal file
84
ConfectioneryDatabaseImplement/Shop.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDatabaseImplement;
|
||||
using ConfectioneryDataModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Models
|
||||
{
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int MaxCountPastries { get; private set; }
|
||||
|
||||
public DateTime DateOpening { get; private set; }
|
||||
|
||||
private Dictionary<int, (IPastryModel, int)>? _cachedPastries = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IPastryModel, int)> Pastries
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_cachedPastries == null)
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
_cachedPastries = ShopPastries
|
||||
.ToDictionary(x => x.Id, x => (context.Pastries
|
||||
.FirstOrDefault(y => y.Id == x.Id)! as IPastryModel, x.Count));
|
||||
}
|
||||
return _cachedPastries;
|
||||
}
|
||||
}
|
||||
|
||||
public int Id { get; private set; }
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopPastry> ShopPastries { get; set; } = new();
|
||||
|
||||
public static Shop? Create(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
MaxCountPastries = model.MaxCountPastries,
|
||||
};
|
||||
}
|
||||
public void Update(ShopBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
// TODO update pastries
|
||||
_cachedPastries = null;
|
||||
}
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Address = Address,
|
||||
Pastries = Pastries,
|
||||
DateOpening = DateOpening,
|
||||
MaxCountPastries = MaxCountPastries,
|
||||
};
|
||||
}
|
||||
}
|
24
ConfectioneryDatabaseImplement/ShopPastry.cs
Normal file
24
ConfectioneryDatabaseImplement/ShopPastry.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using ConfectioneryDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ConfectioneryDatabaseImplement.Models
|
||||
{
|
||||
public class ShopPastry
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int PastryId { get; set; }
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
public virtual Pastry Pastry { get; set; } = new();
|
||||
}
|
||||
}
|
116
ConfectioneryDatabaseImplement/ShopStorage.cs
Normal file
116
ConfectioneryDatabaseImplement/ShopStorage.cs
Normal file
@ -0,0 +1,116 @@
|
||||
using ConfectioneryContracts.BindingModels;
|
||||
using ConfectioneryContracts.SearchModels;
|
||||
using ConfectioneryContracts.StoragesContract;
|
||||
using ConfectioneryContracts.ViewModels;
|
||||
using ConfectioneryDatabaseImplement.Models;
|
||||
using ConfectioneryDataModels;
|
||||
using ConfectioneryDataModels.Models;
|
||||
|
||||
namespace ConfectioneryDatabaseImplement
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
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 (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ConfectioneryDatabase();
|
||||
return context.Shops.FirstOrDefault(x => 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 ConfectioneryDatabase();
|
||||
return context.Shops
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.Name.Contains(model.Name ?? string.Empty))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
return context.Shops
|
||||
.Select(shop => shop.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
var newShop = Shop.Create(model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new ConfectioneryDatabase();
|
||||
context.Shops.Add(newShop);
|
||||
context.SaveChanges();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (shop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
shop.Update(model);
|
||||
context.SaveChanges();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
|
||||
public bool HasNeedPastries(IPastryModel pastry, int needCount)
|
||||
{
|
||||
using var context = new ConfectioneryDatabase();
|
||||
var resultCount = context.Shops
|
||||
.Select(shop => shop.Pastries
|
||||
.FirstOrDefault(x => x.Key == pastry.Id).Value.Item2)
|
||||
.Sum();
|
||||
return resultCount >= needCount;
|
||||
}
|
||||
|
||||
public bool SellPastries(IPastryModel pastry, int needCount)
|
||||
{
|
||||
if (!HasNeedPastries(pastry, needCount))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
using var context = new ConfectioneryDatabase();
|
||||
foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id)))
|
||||
{
|
||||
var tuple = shop.Pastries[pastry.Id];
|
||||
var diff = Math.Min(tuple.Item2, needCount);
|
||||
shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff);
|
||||
|
||||
needCount -= diff;
|
||||
if (needCount <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user