diff --git a/SushiBar/SushiBar/nlog.config b/SushiBar/SushiBar/nlog.config index aad59db..77343b2 100644 --- a/SushiBar/SushiBar/nlog.config +++ b/SushiBar/SushiBar/nlog.config @@ -1,10 +1,10 @@ - + - + diff --git a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs index b7a8107..bdcc7b4 100644 --- a/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs +++ b/SushiBar/SushiBarDatabaseImplement/Implements/OrderStorage.cs @@ -15,7 +15,7 @@ namespace SushiBarDatabaseImplement.Implements using var context = new SushiBarDatabase(); return context.Orders .ToList() - .Select(x => x.GetViewModel) + .Select(x => x.GetViewModel(context)) .ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -36,7 +36,7 @@ namespace SushiBarDatabaseImplement.Implements } using var context = new SushiBarDatabase(); return context.Orders - .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel(context); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -48,7 +48,7 @@ namespace SushiBarDatabaseImplement.Implements } context.Orders.Add(newOrder); context.SaveChanges(); - return newOrder.GetViewModel; + return newOrder.GetViewModel(context); } public OrderViewModel? Update(OrderBindingModel model) { @@ -64,9 +64,8 @@ namespace SushiBarDatabaseImplement.Implements } order.Update(model); context.SaveChanges(); - order.UpdateSushi(context, model); transaction.Commit(); - return order.GetViewModel; + return order.GetViewModel(context); } catch { @@ -83,7 +82,7 @@ namespace SushiBarDatabaseImplement.Implements { context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return element.GetViewModel(context); } return null; } diff --git a/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs new file mode 100644 index 0000000..05f06b1 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.Designer.cs @@ -0,0 +1,169 @@ +// +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 new file mode 100644 index 0000000..3fa0b84 --- /dev/null +++ b/SushiBar/SushiBarDatabaseImplement/Migrations/20240313064708_InitialCreate3.cs @@ -0,0 +1,22 @@ +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/Models/Order.cs b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs index 7f84ac8..a97cbeb 100644 --- a/SushiBar/SushiBarDatabaseImplement/Models/Order.cs +++ b/SushiBar/SushiBarDatabaseImplement/Models/Order.cs @@ -3,7 +3,6 @@ using SushiBarContracts.ViewModels; using SushiBarDataModels.Enums; using SushiBarDataModels.Models; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; namespace SushiBarDatabaseImplement.Models { @@ -21,9 +20,6 @@ namespace SushiBarDatabaseImplement.Models [Required] public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } - [Required] - Sushi sushi { get; set; } = new(); - public static Order Create(SushiBarDatabase context, OrderBindingModel model) { return new Order() @@ -35,7 +31,6 @@ namespace SushiBarDatabaseImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, - sushi = context.Sushis.First(x => x.Id == model.Id) }; } public void Update(OrderBindingModel model) @@ -47,20 +42,22 @@ namespace SushiBarDatabaseImplement.Models DateCreate = model.DateCreate; DateImplement = model.DateImplement; } - public OrderViewModel GetViewModel => new() + public OrderViewModel GetViewModel(SushiBarDatabase context) { - Id = Id, - SushiId = SushiId, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - SushiName = sushi.SushiName - }; - public void UpdateSushi(SushiBarDatabase context, OrderBindingModel model) - { - sushi = context.Sushis.First(x => x.Id == model.Id); + 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(), + }; } } } diff --git a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs index e4257b1..0aaf345 100644 --- a/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs +++ b/SushiBar/SushiBarDatabaseImplement/SushiBarDatabase.cs @@ -9,9 +9,11 @@ namespace SushiBarDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql(@"Host=localhost;Database=SushiBar_db;Username=sushibar;Password=sushibar"); + 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 Components { set; get; } public virtual DbSet Sushis { set; get; }