дай бог готово

This commit is contained in:
Полина Чубыкина 2024-06-21 12:12:48 +04:00
parent 41e18dbe53
commit cd826c1b51
7 changed files with 510 additions and 2 deletions

View File

@ -14,7 +14,7 @@ namespace ConfectioneryDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase3hard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
@ -22,5 +22,7 @@ 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; }
public virtual DbSet<ShopPastry> ShopPastries { set; get; }
}
}

View File

@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using ConfectioneryDataModels.Models;
using Microsoft.EntityFrameworkCore;
namespace ConfectioneryDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Shops
.Include(x => x.Pastries)
.ThenInclude(x => x.Pastry)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Shops
.Include(x => x.Pastries)
.ThenInclude(x => x.Pastry)
.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 ConfectioneryDatabase();
return context.Shops
.Include(x => x.Pastries)
.ThenInclude(x => x.Pastry)
.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 ConfectioneryDatabase();
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 ConfectioneryDatabase();
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.UpdatePastries(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Shops
.Include(x => x.Pastries)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool Sale(IPastryModel model, int count)
{
using var context = new ConfectioneryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
foreach (var shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry)
.Where(x => x.Pastries.Any(x => x.PastryId == model.Id))
.ToList())
{
var pastry = shop.ShopPastries[model.Id];
int min = Math.Min(pastry.Item2, count);
if (min == pastry.Item2)
{
shop.ShopPastries.Remove(model.Id);
}
else
{
shop.ShopPastries[model.Id] = (pastry.Item1, pastry.Item2 - min);
}
shop.UpdatePastries(context, new() { Id = shop.Id, ShopPastries = shop.ShopPastries });
count -= min;
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ConfectioneryDatabaseImplement.Migrations
{
[DbContext(typeof(ConfectioneryDatabase))]
[Migration("20240325181336_InitialCreate")]
[Migration("20240621080649_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -124,6 +124,59 @@ 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>("PastriesMaximum")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
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("ShopPastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
@ -154,6 +207,25 @@ namespace ConfectioneryDatabaseImplement.Migrations
b.Navigation("Pastry");
});
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("Pastries")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pastry");
b.Navigation("Shop");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
{
b.Navigation("ProductComponents");
@ -165,6 +237,11 @@ namespace ConfectioneryDatabaseImplement.Migrations
b.Navigation("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Pastries");
});
#pragma warning restore 612, 618
}
}

View File

@ -39,6 +39,22 @@ namespace ConfectioneryDatabaseImplement.Migrations
table.PrimaryKey("PK_Pastries", 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),
PastriesMaximum = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
@ -90,6 +106,33 @@ namespace ConfectioneryDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopPastries",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopId = table.Column<int>(type: "int", nullable: false),
PastryId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopPastries", x => x.Id);
table.ForeignKey(
name: "FK_ShopPastries_Pastries_PastryId",
column: x => x.PastryId,
principalTable: "Pastries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopPastries_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_PastryId",
table: "Orders",
@ -104,6 +147,16 @@ namespace ConfectioneryDatabaseImplement.Migrations
name: "IX_PastryComponents_PastryId",
table: "PastryComponents",
column: "PastryId");
migrationBuilder.CreateIndex(
name: "IX_ShopPastries_PastryId",
table: "ShopPastries",
column: "PastryId");
migrationBuilder.CreateIndex(
name: "IX_ShopPastries_ShopId",
table: "ShopPastries",
column: "ShopId");
}
/// <inheritdoc />
@ -115,11 +168,17 @@ namespace ConfectioneryDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "PastryComponents");
migrationBuilder.DropTable(
name: "ShopPastries");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Pastries");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ 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>("PastriesMaximum")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
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("ShopPastries");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry")
@ -151,6 +204,25 @@ namespace ConfectioneryDatabaseImplement.Migrations
b.Navigation("Pastry");
});
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("Pastries")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Pastry");
b.Navigation("Shop");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b =>
{
b.Navigation("ProductComponents");
@ -162,6 +234,11 @@ namespace ConfectioneryDatabaseImplement.Migrations
b.Navigation("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Pastries");
});
#pragma warning restore 612, 618
}
}

View File

@ -0,0 +1,115 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.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 ConfectioneryDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int PastriesMaximum { get; set; }
private Dictionary<int, (IPastryModel, int)>? _shopPastries = null;
[NotMapped]
public Dictionary<int, (IPastryModel, int)> ShopPastries
{
get
{
if (_shopPastries == null)
{
_shopPastries = Pastries
.ToDictionary(x => x.PastryId, x => (x.Pastry as IPastryModel, x.Count));
}
return _shopPastries;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopPastry> Pastries { get; set; } = new();
public static Shop Create(ConfectioneryDatabase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
PastriesMaximum = model.PastriesMaximum,
Pastries = model.ShopPastries.Select(x => new ShopPastry
{
Pastry = context.Pastries.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;
PastriesMaximum = model.PastriesMaximum;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOpening = DateOpening,
PastriesMaximum = PastriesMaximum,
ShopPastries = ShopPastries
};
public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model)
{
var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList();
if (shopPastries != null && shopPastries.Count > 0)
{
context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId)));
context.SaveChanges();
foreach (var updatePastry in shopPastries)
{
if (model.ShopPastries.ContainsKey(updatePastry.PastryId))
{
updatePastry.Count = model.ShopPastries[updatePastry.PastryId].Item2;
model.ShopPastries.Remove(updatePastry.PastryId);
}
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var ic in model.ShopPastries)
{
context.ShopPastries.Add(new ShopPastry
{
Shop = shop,
Pastry = context.Pastries.First(x => x.Id == ic.Key),
Count = ic.Value.Item2
});
context.SaveChanges();
}
_shopPastries = 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 ConfectioneryDatabaseImplement.Models
{
public class ShopPastry
{
public int Id { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int PastryId { get; set; }
[Required]
public int Count { get; set; }
public virtual Pastry Pastry { get; set; } = new();
public virtual Shop Shop { get; set; } = new();
}
}