diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs index bdcc7b4..24c40d1 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -14,8 +14,8 @@ namespace SushiBarDatabaseImplement.Implements { using var context = new SushiBarDatabase(); return context.Orders - .ToList() - .Select(x => x.GetViewModel(context)) + .Include(x => x.Sushi) + .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -36,7 +36,8 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Orders - .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel(context); + .Include (x => x.Sushi) + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { @@ -48,7 +49,7 @@ namespace SushiBarDatabaseImplement.Implements } context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel(context); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { @@ -65,7 +66,7 @@ namespace SushiBarDatabaseImplement.Implements order.Update(model); context.SaveChanges(); transaction.Commit(); - return order.GetViewModel(context); + return order.GetViewModel; } catch { @@ -82,7 +83,7 @@ namespace SushiBarDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel(context); + return element.GetViewModel; } return null; } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs deleted file mode 100644 index 05f06b1..0000000 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs +++ /dev/null @@ -1,169 +0,0 @@ -// -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("20240313064708_InitialCreate3")] - partial class InitialCreate3 - { - /// - 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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("text"); - - b.Property("Cost") - .HasColumnType("double precision"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("DateCreate") - .HasColumnType("timestamp with time zone"); - - b.Property("DateImplement") - .HasColumnType("timestamp with time zone"); - - b.Property("Status") - .HasColumnType("integer"); - - b.Property("Sum") - .HasColumnType("double precision"); - - b.Property("SushiId") - .HasColumnType("integer"); - - b.HasKey("Id"); - - b.HasIndex("SushiId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("SushiBarDatabaseImplement.Models.Sushi", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Price") - .HasColumnType("double precision"); - - b.Property("SushiName") - .IsRequired() - .HasColumnType("text"); - - b.HasKey("Id"); - - b.ToTable("Sushis"); - }); - - modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("integer"); - - b.Property("Count") - .HasColumnType("integer"); - - b.Property("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", null) - .WithMany("Orders") - .HasForeignKey("SushiId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - }); - - 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 - } - } -} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.cs deleted file mode 100644 index 3fa0b84..0000000 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace SushiBarDatabaseImplement.Migrations -{ - /// - public partial class InitialCreate3 : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs similarity index 95% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.Designer.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs index e5daf24..1aa7242 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.Designer.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.Designer.cs @@ -12,8 +12,8 @@ using SushiBarDatabaseImplement; namespace SushiBarDatabaseImplement.Migrations { [DbContext(typeof(SushiBarDatabase))] - [Migration("20240313061851_InitialCreate2")] - partial class InitialCreate2 + [Migration("20240424132544_InitialCreate")] + partial class InitialCreate { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -57,10 +57,10 @@ namespace SushiBarDatabaseImplement.Migrations .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("timestamp with time zone"); + .HasColumnType("timestamp without time zone"); b.Property("DateImplement") - .HasColumnType("timestamp with time zone"); + .HasColumnType("timestamp without time zone"); b.Property("Status") .HasColumnType("integer"); @@ -126,11 +126,13 @@ namespace SushiBarDatabaseImplement.Migrations modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => { - b.HasOne("SushiBarDatabaseImplement.Models.Sushi", null) + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") .WithMany("Orders") .HasForeignKey("SushiId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Sushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs similarity index 96% rename from SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.cs rename to SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs index 5967311..545b5b1 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313061851_InitialCreate2.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240424132544_InitialCreate.cs @@ -7,7 +7,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace SushiBarDatabaseImplement.Migrations { /// - public partial class InitialCreate2 : Migration + public partial class InitialCreate : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -46,12 +46,12 @@ namespace SushiBarDatabaseImplement.Migrations { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - SushiId = table.Column(type: "integer", nullable: false), Count = table.Column(type: "integer", nullable: false), Sum = table.Column(type: "double precision", nullable: false), Status = table.Column(type: "integer", nullable: false), - DateCreate = table.Column(type: "timestamp with time zone", nullable: false), - DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + DateCreate = table.Column(type: "timestamp without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", nullable: true), + SushiId = table.Column(type: "integer", nullable: false) }, constraints: table => { diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs index 1c10cec..be024d5 100644 --- a/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/SushiBarDatabaseModelSnapshot.cs @@ -54,10 +54,10 @@ namespace SushiBarDatabaseImplement.Migrations .HasColumnType("integer"); b.Property("DateCreate") - .HasColumnType("timestamp with time zone"); + .HasColumnType("timestamp without time zone"); b.Property("DateImplement") - .HasColumnType("timestamp with time zone"); + .HasColumnType("timestamp without time zone"); b.Property("Status") .HasColumnType("integer"); @@ -123,11 +123,13 @@ namespace SushiBarDatabaseImplement.Migrations modelBuilder.Entity("SushiBarDatabaseImplement.Models.Order", b => { - b.HasOne("SushiBarDatabaseImplement.Models.Sushi", null) + b.HasOne("SushiBarDatabaseImplement.Models.Sushi", "Sushi") .WithMany("Orders") .HasForeignKey("SushiId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Sushi"); }); modelBuilder.Entity("SushiBarDatabaseImplement.Models.SushiComponent", b => diff --git a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index a97cbeb..b40afa2 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -10,8 +10,6 @@ namespace SushiBarDatabaseImplement.Models { public int Id { get; private set; } [Required] - public int SushiId { get; set; } - [Required] public int Count { get; set; } [Required] public double Sum { get; set; } @@ -20,44 +18,46 @@ namespace SushiBarDatabaseImplement.Models [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, - SushiId = model.SushiId, 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) { - Id = model.Id; - SushiId = model.SushiId; - Sum = model.Sum; + if (model == null) + { + return; + } Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; } - public OrderViewModel GetViewModel(SushiBarDatabase context) + public OrderViewModel GetViewModel => new() { - return new() - { - Id = Id, - SushiId = SushiId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - SushiName = context.Sushis - .Where(s => s.Id == SushiId) - .Select(s => s.SushiName) - .First(), - }; - } + Id = Id, + SushiId = SushiId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + SushiName = Sushi.SushiName + }; } }