ПИбд-22 Боровков М В 3 лабораторная работа #3

Closed
bekodeg wants to merge 7 commits from labWork3 into labWork2
14 changed files with 999 additions and 3 deletions

View File

@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "Su
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{4F721F76-EF7A-4FF3-80B0-77A459D091D7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{4F721F76-EF7A-4FF3-80B0-77A459D091D7}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarDatabaseImplement", "SushiBarDatabaseImplement\SushiBarDatabaseImplement.csproj", "{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -45,6 +47,10 @@ Global
{4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Debug|Any CPU.Build.0 = Debug|Any CPU {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.ActiveCfg = Release|Any CPU {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.Build.0 = Release|Any CPU {4F721F76-EF7A-4FF3-80B0-77A459D091D7}.Release|Any CPU.Build.0 = Release|Any CPU
{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9C66AFC-1246-4A0C-AB70-8D5C0CF311D9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -5,7 +5,7 @@ using SushiBar.Forms;
using SushiBarBusinessLogic.BusinessLogics; using SushiBarBusinessLogic.BusinessLogics;
using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.StoragesContracts; using SushiBarContracts.StoragesContracts;
using SushiBarFileImplement.Implements; using SushiBarDatabaseImplement.Implements;
namespace SushiBar namespace SushiBar
{ {

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" internalLogLevel="Info"> autoReload="true" internalLogLevel="Info">
<targets> <targets>
<target xsi:type="File" name="tofile" fileName="carlog-${shortdate}.log" /> <target xsi:type="File" name="tofile" fileName="sushibar-${shortdate}.log" />
</targets> </targets>
<rules> <rules>
<logger name="*" minlevel="Debug" writeTo="tofile" /> <logger name="*" minlevel="Debug" writeTo="tofile" />

View File

@ -0,0 +1,82 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
namespace SushiBarDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Components
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Components
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Components
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ComponentName)
&& x.ComponentName == model.ComponentName)
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new SushiBarDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new SushiBarDatabase();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,92 @@
using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using System.Collections.Generic;
namespace SushiBarDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Orders
.Include(o => o.Sushi)
.Select(o => o.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
using var context = new SushiBarDatabase();
return context.Orders
.Include(o => o.Sushi)
.Where(o =>
(model.Id.HasValue && o.Id == model.Id))
.Select(o => o.GetViewModel)
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Orders
.Include (o => o.Sushi)
.FirstOrDefault(o => o.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
using var context = new SushiBarDatabase();
var newOrder = Order.Create(context, model);
if (newOrder == null)
{
return null;
}
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var order = context.Orders.Include(o => o.Sushi).FirstOrDefault(rec =>
rec.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
transaction.Commit();
return order.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Orders
.Include(o => o.Sushi)
.FirstOrDefault(o => o.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,105 @@
using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
namespace SushiBarDatabaseImplement.Implements
{
public class SushiStorage : ISushiStorage
{
public List<SushiViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Sushis
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName))
{
return new();
}
using var context = new SushiBarDatabase();
return context.Sushis
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.SushiName.Contains(model.SushiName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SushiViewModel? GetElement(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
{
return null;
}
using var context = new SushiBarDatabase();
return context.Sushis
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SushiName)
&& x.SushiName == model.SushiName)
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public SushiViewModel? Insert(SushiBindingModel model)
{
using var context = new SushiBarDatabase();
var newSushi = Sushi.Create(context, model);
if (newSushi == null)
{
return null;
}
context.Sushis.Add(newSushi);
context.SaveChanges();
return newSushi.GetViewModel;
}
public SushiViewModel? Update(SushiBindingModel model)
{
using var context = new SushiBarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var sushi = context.Sushis.FirstOrDefault(rec =>
rec.Id == model.Id);
if (sushi == null)
{
return null;
}
sushi.Update(model);
context.SaveChanges();
sushi.UpdateComponents(context, model);
transaction.Commit();
return sushi.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public SushiViewModel? Delete(SushiBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Sushis
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Sushis.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,171 @@
// <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 SushiBarDatabaseImplement;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
[DbContext(typeof(SushiBarDatabase))]
[Migration("20240424132544_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.16")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.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 without time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp without time zone");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<int>("SushiId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("SushiId");
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("SushiName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Sushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", 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>("SushiId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("SushiId");
b.ToTable("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Orders")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component")
.WithMany("SushiComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Components")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b =>
{
b.Navigation("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,126 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ComponentName = table.Column<string>(type: "text", nullable: false),
Cost = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Sushis",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SushiName = table.Column<string>(type: "text", nullable: false),
Price = table.Column<double>(type: "double precision", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Sushis", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Count = table.Column<int>(type: "integer", nullable: false),
Sum = table.Column<double>(type: "double precision", nullable: false),
Status = table.Column<int>(type: "integer", nullable: false),
DateCreate = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
DateImplement = table.Column<DateTime>(type: "timestamp without time zone", nullable: true),
SushiId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Sushis_SushiId",
column: x => x.SushiId,
principalTable: "Sushis",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SushiComponents",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SushiId = table.Column<int>(type: "integer", nullable: false),
ComponentId = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SushiComponents", x => x.Id);
table.ForeignKey(
name: "FK_SushiComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SushiComponents_Sushis_SushiId",
column: x => x.SushiId,
principalTable: "Sushis",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_SushiId",
table: "Orders",
column: "SushiId");
migrationBuilder.CreateIndex(
name: "IX_SushiComponents_ComponentId",
table: "SushiComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_SushiComponents_SushiId",
table: "SushiComponents",
column: "SushiId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "SushiComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Sushis");
}
}
}

View File

@ -0,0 +1,168 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using SushiBarDatabaseImplement;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
[DbContext(typeof(SushiBarDatabase))]
partial class SushiBarDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.16")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("SushiBarDatabaseImplement.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("SushiBarDatabaseImplement.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 without time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp without time zone");
b.Property<int>("Status")
.HasColumnType("integer");
b.Property<double>("Sum")
.HasColumnType("double precision");
b.Property<int>("SushiId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("SushiId");
b.ToTable("Orders");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("double precision");
b.Property<string>("SushiName")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Sushis");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", 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>("SushiId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("SushiId");
b.ToTable("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Orders")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b =>
{
b.HasOne("SushiBarDatabaseImplement.Models.Component", "Component")
.WithMany("SushiComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi")
.WithMany("Components")
.HasForeignKey("SushiId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Sushi");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Component", b =>
{
b.Navigation("SushiComponents");
});
modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,48 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SushiBarDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<SushiComponent> SushiComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update (ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,63 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
[Required]
public int SushiId { get; private set; }
public virtual Sushi Sushi { get; set; }
public static Order Create(SushiBarDatabase context, OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
SushiId = model.SushiId,
Sushi = context.Sushis.First(x => x.Id == model.SushiId)
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
SushiId = SushiId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
SushiName = Sushi.SushiName
};
}
}

View File

@ -0,0 +1,94 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SushiBarDatabaseImplement.Models
{
public class Sushi : ISushiModel
{
public int Id { get; set; }
[Required]
public string SushiName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _sushiComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> SushiComponents
{
get
{
if (_sushiComponents == null)
{
_sushiComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _sushiComponents;
}
}
[ForeignKey("SushiId")]
public virtual List<SushiComponent> Components { get; set; } = new();
[ForeignKey("SushiId")]
public virtual List<Order> Orders { get; set; } = new();
public static Sushi Create(SushiBarDatabase context, SushiBindingModel model)
{
return new Sushi()
{
Id = model.Id,
SushiName = model.SushiName,
Price = model.Price,
Components = model.SushiComponents.Select(x =>
new SushiComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(SushiBindingModel model)
{
SushiName = model.SushiName;
Price = model.Price;
}
public SushiViewModel GetViewModel => new()
{
Id = Id,
SushiName = SushiName,
Price = Price,
SushiComponents = SushiComponents
};
public void UpdateComponents(SushiBarDatabase context, SushiBindingModel model)
{
var sushiComponents = context.SushiComponents.Where(rec =>
rec.SushiId == model.Id).ToList();
if (sushiComponents != null && sushiComponents.Count > 0)
{ // удалили те, которых нет в модели
context.SushiComponents.RemoveRange(sushiComponents.Where(rec =>
!model.SushiComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in sushiComponents)
{
updateComponent.Count = model.SushiComponents[updateComponent.ComponentId].Item2;
model.SushiComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var sushi = context.Sushis.First(x => x.Id == Id);
foreach (var pc in model.SushiComponents)
{
context.SushiComponents.Add(new SushiComponent
{
Sushi = sushi,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_sushiComponents = null;
}
}
}

View File

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models
{
public class SushiComponent
{
public int Id { get; set; }
[Required]
public int SushiId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Sushi Sushi { get; set; } = new();
}
}

View File

@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using SushiBarDatabaseImplement.Models;
namespace SushiBarDatabaseImplement
{
public class SushiBarDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql(@"Host=localhost;Database=SushiBar_db;Username=postgres;Password=postgres");
}
base.OnConfiguring(optionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Sushi> Sushis { set; get; }
public virtual DbSet<SushiComponent> SushiComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}