ПИбд-23 Насыров Артур Газинурович Лабораторная работа №3 #4

Closed
gaillard wants to merge 3 commits from lab3 into lab2
20 changed files with 1079 additions and 18 deletions

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlowerShopDataModels.Models;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace FlowerShopDatabaseImplement.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<FlowerComponent> FlowerComponents { 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 static Component Create(ComponentViewModel model)
{
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,89 @@
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new FlowerShopDataBase();
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 FlowerShopDataBase();
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 FlowerShopDataBase();
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 FlowerShopDataBase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new FlowerShopDataBase();
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 FlowerShopDataBase();
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,97 @@
using FlowerShopDataModels.Models;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using FlowerShopDatabaseImplement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Models
{
public class Flower : IFlowerModel
{
public int Id { get; set; }
[Required]
public string FlowerName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _flowerComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> FlowerComponents
{
get
{
if (_flowerComponents == null)
{
_flowerComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _flowerComponents;
}
}
[ForeignKey("FlowerId")]
public virtual List<FlowerComponent> Components { get; set; } = new();
[ForeignKey("FlowerId")]
public virtual List<Order> Orders { get; set; } = new();
public static Flower Create(FlowerShopDataBase context, FlowerBindingModel model)
{
return new Flower()
{
Id = model.Id,
FlowerName = model.FlowerName,
Price = model.Price,
Components = model.FlowerComponents.Select(x => new FlowerComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(FlowerBindingModel model)
{
FlowerName = model.FlowerName;
Price = model.Price;
}
public FlowerViewModel GetViewModel => new()
{
Id = Id,
FlowerName = FlowerName,
Price = Price,
FlowerComponents = FlowerComponents
};
public void UpdateComponents(FlowerShopDataBase context, FlowerBindingModel model)
{
var flowerComponents = context.FlowerComponents.Where(rec =>rec.FlowerId == model.Id).ToList();
if (flowerComponents != null && FlowerComponents.Count > 0)
{
context.FlowerComponents.RemoveRange(flowerComponents.Where(rec => !model.FlowerComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in flowerComponents)
{
updateComponent.Count =
model.FlowerComponents[updateComponent.ComponentId].Item2;
model.FlowerComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var flower = context.Flowers.First(x => x.Id == Id);
foreach (var pc in model.FlowerComponents)
{
context.FlowerComponents.Add(new FlowerComponent
{
Flower = flower,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_flowerComponents = null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Models
{
public class FlowerComponent
{
public int Id { get; set; }
[Required]
public int FlowerId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Flower Flower { get; set; } = new();
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using FlowerShopDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement
{
public class FlowerShopDataBase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Flower> Flowers { set; get; }
public virtual DbSet<FlowerComponent> FlowerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FlowerShopContracts\FlowerShopContracts.csproj" />
<ProjectReference Include="..\FlowerShopDataModels\FlowerShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,110 @@
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Implements
{
public class FlowerStorage : IFlowerStorage
{
public List<FlowerViewModel> GetFullList()
{
using var context = new FlowerShopDataBase();
return context.Flowers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<FlowerViewModel> GetFilteredList(FlowerSearchModel model)
{
if (string.IsNullOrEmpty(model.FlowerName))
{
return new();
}
using var context = new FlowerShopDataBase();
return context.Flowers.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.FlowerName.Contains(model.FlowerName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public FlowerViewModel? GetElement(FlowerSearchModel model)
{
if (string.IsNullOrEmpty(model.FlowerName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDataBase();
return context.Flowers
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FlowerName) &&
x.FlowerName == model.FlowerName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public FlowerViewModel? Insert(FlowerBindingModel model)
{
using var context = new FlowerShopDataBase();
var newFlower = Flower.Create(context, model);
if (newFlower == null)
{
return null;
}
context.Flowers.Add(newFlower);
context.SaveChanges();
return newFlower.GetViewModel;
}
public FlowerViewModel? Update(FlowerBindingModel model)
{
using var context = new FlowerShopDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var flower = context.Flowers.FirstOrDefault(rec =>
rec.Id == model.Id);
if (flower == null)
{
return null;
}
flower.Update(model);
context.SaveChanges();
flower.UpdateComponents(context, model);
transaction.Commit();
return flower.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public FlowerViewModel? Delete(FlowerBindingModel model)
{
using var context = new FlowerShopDataBase();
var element = context.Flowers
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Flowers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,168 @@
// <auto-generated />
using System;
using FlowerShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
[DbContext(typeof(FlowerShopDataBase))]
[Migration("20240312171524_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("FlowerName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Flowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("FlowerId");
b.ToTable("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("FlowerComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower")
.WithMany("Components")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Flower");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null)
.WithMany("Orders")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,122 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Flowers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FlowerName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Flowers", x => x.Id);
});
migrationBuilder.CreateTable(
name: "FlowerComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FlowerId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_FlowerComponents", x => x.Id);
table.ForeignKey(
name: "FK_FlowerComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_FlowerComponents_Flowers_FlowerId",
column: x => x.FlowerId,
principalTable: "Flowers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true),
FlowerId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Flowers_FlowerId",
column: x => x.FlowerId,
principalTable: "Flowers",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_FlowerComponents_ComponentId",
table: "FlowerComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_FlowerComponents_FlowerId",
table: "FlowerComponents",
column: "FlowerId");
migrationBuilder.CreateIndex(
name: "IX_Orders_FlowerId",
table: "Orders",
column: "FlowerId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "FlowerComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Flowers");
}
}
}

View File

@ -0,0 +1,166 @@
// <auto-generated />
using System;
using FlowerShopDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace FlowerShopDatabaseImplement.Migrations
{
[DbContext(typeof(FlowerShopDataBase))]
partial class FlowerShopDataBaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("FlowerName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Flowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("FlowerId");
b.ToTable("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.ToTable("Orders");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
.WithMany("FlowerComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower")
.WithMany("Components")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Flower");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b =>
{
b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null)
Review

Связь настроена не до конца

Связь настроена не до конца
.WithMany("Orders")
.HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("FlowerComponents");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Flower", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,71 @@
using FlowerShopDataModels;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlowerShopDataModels.Enums;
namespace FlowerShopDatabaseImplement
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public int FlowerId { get; private set; }
public static Order? Create(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,
FlowerId = model.FlowerId,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
FlowerId = FlowerId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
};
}
}

View File

@ -0,0 +1,101 @@
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.ViewModels;
using FlowerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel> GetFullList()
{
using var context = new FlowerShopDataBase();
return context.Orders
.Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new FlowerShopDataBase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new FlowerShopDataBase();
return AccessFlowerStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new FlowerShopDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return AccessFlowerStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AccessFlowerStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new FlowerShopDataBase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return AccessFlowerStorage(element.GetViewModel);
}
return null;
}
public static OrderViewModel AccessFlowerStorage(OrderViewModel model)
Review

Cущности связаны, так что отдельный запрос для получения названия не требуется

Cущности связаны, так что отдельный запрос для получения названия не требуется
{
if (model == null)
return null;
using var context = new FlowerShopDataBase();
foreach (var flower in context.Flowers)
{
if (flower.Id == model.FlowerId)
{
model.FlowerName = flower.FlowerName;
break;
}
}
return model;
}
}
}

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopBusinessLogic", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopListImplement", "FlowerShopListImplement\FlowerShopListImplement.csproj", "{62DAA9A0-9A71-4117-8D06-8825E1678D9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{0CFC7A18-2E56-4D0B-80AD-F52893222027}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlowerShopFileImplement", "FlowerShopFileImplement\FlowerShopFileImplement.csproj", "{0CFC7A18-2E56-4D0B-80AD-F52893222027}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlowerShopDatabaseImplement", "FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj", "{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{0CFC7A18-2E56-4D0B-80AD-F52893222027}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CFC7A18-2E56-4D0B-80AD-F52893222027}.Release|Any CPU.Build.0 = Release|Any CPU
{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD9FDCBB-1EE0-4726-85B7-4B9C6D40F761}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -208,7 +208,6 @@
Controls.Add(textBoxName);
Controls.Add(labelPrice);
Controls.Add(labelName);
// Name = "FormProduct";
Text = "Изделие";
Load += FormProduct_Load;
groupBoxComponent.ResumeLayout(false);
@ -231,10 +230,8 @@
private DataGridView dataGridView;
private Button Save;
private Button buttonCancel;
// private DataGridViewTextBoxColumn Id;
private DataGridViewTextBoxColumn Name;
private DataGridViewTextBoxColumn Number;
//private DataGridViewTextBoxColumn Id;
private DataGridViewTextBoxColumn Component;
private DataGridViewTextBoxColumn idd;
private DataGridViewTextBoxColumn Numbers;

View File

@ -89,7 +89,7 @@
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += buttonCancel_Click;
//
// FormProductComponent
// FormFlowerComponent
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
@ -100,9 +100,8 @@
Controls.Add(comboBoxComponent);
Controls.Add(labelNumber);
Controls.Add(labelComponent);
Name = "FormProductComponent";
Text = "FormProductComponent";
Load += FormProductComponent_Load;
Name = "FormFlowerComponent";
Text = "Цветок-компонент";
ResumeLayout(false);
PerformLayout();
}

View File

@ -65,11 +65,6 @@ namespace ProjectFlowerShop
}
}
private void FormProductComponent_Load(object sender, EventArgs e)
{
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxNumber.Text))

View File

@ -96,7 +96,7 @@
Controls.Add(AddButton);
Controls.Add(DataGridView);
Name = "FormFlowers";
Text = "FlowersForm";
Text = "Форма цветов";
Load += IceCreamsForm_Load;
((System.ComponentModel.ISupportInitialize)DataGridView).EndInit();
ResumeLayout(false);

View File

@ -62,14 +62,14 @@
// КомпонентыStripMenuItem
//
КомпонентыStripMenuItem.Name = "КомпонентыStripMenuItem";
КомпонентыStripMenuItem.Size = new Size(224, 26);
КомпонентыStripMenuItem.Size = new Size(182, 26);
КомпонентыStripMenuItem.Text = "Компоненты";
КомпонентыStripMenuItem.Click += КомпонентыStripMenuItem_Click;
//
// ЦветыStripMenuItem
//
ЦветыStripMenuItem.Name = "ЦветыStripMenuItem";
ЦветыStripMenuItem.Size = new Size(224, 26);
ЦветыStripMenuItem.Size = new Size(182, 26);
ЦветыStripMenuItem.Text = "Цветы";
ЦветыStripMenuItem.Click += ЦветыStripMenuItem_Click;
//
@ -146,7 +146,7 @@
Controls.Add(menuStrip1);
MainMenuStrip = menuStrip1;
Name = "MainForm";
Text = "MainForm";
Text = "Главная форма";
Load += MainForm_Load;
menuStrip1.ResumeLayout(false);
menuStrip1.PerformLayout();

View File

@ -1,7 +1,7 @@
using FlowerShopBusinessLogic.BusinessLogic;
using FlowerShopContracts.BusinessLogicsContracts;
using FlowerShopContracts.StoragesContracts;
using FlowerShopFileImplement.Implements;
using FlowerShopDatabaseImplement.Implements;
using ProjectFlowerShop;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
</ItemGroup>
@ -16,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\FlowerShopBusinessLogic\FlowerShopBusinessLogic.csproj" />
<ProjectReference Include="..\FlowerShopContracts\FlowerShopContracts.csproj" />
<ProjectReference Include="..\FlowerShopDatabaseImplement\FlowerShopDatabaseImplement.csproj" />
<ProjectReference Include="..\FlowerShopDataModels\FlowerShopDataModels.csproj" />
<ProjectReference Include="..\FlowerShopFileImplement\FlowerShopFileImplement.csproj" />
<ProjectReference Include="..\FlowerShopListImplement\FlowerShopListImplement.csproj" />