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

Closed
bekodeg wants to merge 44 commits from labWork5 into labWork4
6 changed files with 216 additions and 27 deletions
Showing only changes of commit a3232337a2 - Show all commits

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

@ -15,7 +15,7 @@ namespace SushiBarDatabaseImplement.Implements
using var context = new SushiBarDatabase(); using var context = new SushiBarDatabase();
return context.Orders return context.Orders
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel(context))
.ToList(); .ToList();
} }
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
@ -36,7 +36,7 @@ namespace SushiBarDatabaseImplement.Implements
} }
using var context = new SushiBarDatabase(); using var context = new SushiBarDatabase();
return context.Orders return context.Orders
.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel(context);
} }
public OrderViewModel? Insert(OrderBindingModel model) public OrderViewModel? Insert(OrderBindingModel model)
{ {
@ -48,7 +48,7 @@ namespace SushiBarDatabaseImplement.Implements
} }
context.Orders.Add(newOrder); context.Orders.Add(newOrder);
context.SaveChanges(); context.SaveChanges();
return newOrder.GetViewModel; return newOrder.GetViewModel(context);
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
@ -64,9 +64,8 @@ namespace SushiBarDatabaseImplement.Implements
} }
order.Update(model); order.Update(model);
context.SaveChanges(); context.SaveChanges();
order.UpdateSushi(context, model);
transaction.Commit(); transaction.Commit();
return order.GetViewModel; return order.GetViewModel(context);
} }
catch catch
{ {
@ -83,7 +82,7 @@ namespace SushiBarDatabaseImplement.Implements
{ {
context.Orders.Remove(element); context.Orders.Remove(element);
context.SaveChanges(); context.SaveChanges();
return element.GetViewModel; return element.GetViewModel(context);
} }
return null; return null;
} }

View File

@ -0,0 +1,169 @@
// <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("20240313064708_InitialCreate3")]
partial class InitialCreate3
{
/// <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 with time zone");
b.Property<DateTime?>("DateImplement")
.HasColumnType("timestamp with 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", 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
}
}
}

View File

@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace SushiBarDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate3 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -3,7 +3,6 @@ using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums; using SushiBarDataModels.Enums;
using SushiBarDataModels.Models; using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SushiBarDatabaseImplement.Models namespace SushiBarDatabaseImplement.Models
{ {
@ -21,9 +20,6 @@ namespace SushiBarDatabaseImplement.Models
[Required] [Required]
public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; } public DateTime? DateImplement { get; set; }
[Required]
Sushi sushi { get; set; } = new();
public static Order Create(SushiBarDatabase context, OrderBindingModel model) public static Order Create(SushiBarDatabase context, OrderBindingModel model)
{ {
return new Order() return new Order()
@ -35,7 +31,6 @@ namespace SushiBarDatabaseImplement.Models
Status = model.Status, Status = model.Status,
DateCreate = model.DateCreate, DateCreate = model.DateCreate,
DateImplement = model.DateImplement, DateImplement = model.DateImplement,
sushi = context.Sushis.First(x => x.Id == model.Id)
}; };
} }
public void Update(OrderBindingModel model) public void Update(OrderBindingModel model)
@ -47,7 +42,9 @@ namespace SushiBarDatabaseImplement.Models
DateCreate = model.DateCreate; DateCreate = model.DateCreate;
DateImplement = model.DateImplement; DateImplement = model.DateImplement;
} }
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel(SushiBarDatabase context)
{
return new()
{ {
Id = Id, Id = Id,
SushiId = SushiId, SushiId = SushiId,
@ -56,11 +53,11 @@ namespace SushiBarDatabaseImplement.Models
Status = Status, Status = Status,
DateCreate = DateCreate, DateCreate = DateCreate,
DateImplement = DateImplement, DateImplement = DateImplement,
SushiName = sushi.SushiName SushiName = context.Sushis
.Where(s => s.Id == SushiId)
.Select(s => s.SushiName)
.First(),
}; };
public void UpdateSushi(SushiBarDatabase context, OrderBindingModel model)
{
sushi = context.Sushis.First(x => x.Id == model.Id);
} }
} }
} }

View File

@ -9,9 +9,11 @@ namespace SushiBarDatabaseImplement
{ {
if (optionsBuilder.IsConfigured == false) 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); base.OnConfiguring(optionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
} }
public virtual DbSet<Component> Components { set; get; } public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Sushi> Sushis { set; get; } public virtual DbSet<Sushi> Sushis { set; get; }