Лабораторная работа 3 фулл

This commit is contained in:
Katanaa Die 2024-04-21 16:04:37 +04:00
parent f65e79ef4e
commit da1519bc1b
32 changed files with 1112 additions and 34 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarView", "BarView\BarView.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarDataModels", "BarDataModels\BarDataModels.csproj", "{DCE9F610-B4A7-4BF8-9A09-517847F0C00B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BarFileImplement", "BarFileImplement\BarFileImplement.csproj", "{B5FD7DE0-B624-4775-BA13-B0E10504B397}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarFileImplement", "BarFileImplement\BarFileImplement.csproj", "{B5FD7DE0-B624-4775-BA13-B0E10504B397}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BarDatabaseImplement", "BarDatabaseImplement\BarDatabaseImplement.csproj", "{F90A5C3D-BBFF-4C84-8CA2-A5F29297DAAA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{B5FD7DE0-B624-4775-BA13-B0E10504B397}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5FD7DE0-B624-4775-BA13-B0E10504B397}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5FD7DE0-B624-4775-BA13-B0E10504B397}.Release|Any CPU.Build.0 = Release|Any CPU
{F90A5C3D-BBFF-4C84-8CA2-A5F29297DAAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F90A5C3D-BBFF-4C84-8CA2-A5F29297DAAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F90A5C3D-BBFF-4C84-8CA2-A5F29297DAAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F90A5C3D-BBFF-4C84-8CA2-A5F29297DAAA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -7,6 +7,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />

View File

@ -10,7 +10,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BarBusinessLogic.BusinessLogic
namespace BarBusinessLogic.BusinessLogics
{
public class CocktailLogic : ICocktailLogic
{

View File

@ -7,7 +7,7 @@ using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
namespace BarBusinessLogic.BusinessLogic
namespace BarBusinessLogic.BusinessLogics
{
public class ComponentLogic : IComponentLogic
{

View File

@ -11,7 +11,7 @@ using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace BarBusinessLogic.BusinessLogic
namespace BarBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{

View File

@ -7,6 +7,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BarDataModels\BarDataModels.csproj" />
<ProjectReference Include="..\BarDataModels\BarDataModels.csproj" />
</ItemGroup>

View File

@ -1,9 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BarDataModels
namespace BarDataModels.Enums
{
public interface IId
{

View File

@ -1,13 +1,13 @@
using System;
using System;
namespace BarDataModels.Enums
{
public enum OrderStatus
{
Неизвестен = -1,
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3
Неизвестен = -1,
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarDataModels.Enums;
namespace BarDataModels.Models
{

View File

@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using BarDataModels.Enums;
namespace BarDataModels.Models
{

View File

@ -17,3 +17,4 @@ namespace BarDataModels.Models
DateTime? DateImplement { get; }
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BarDatabaseImplement
{
public class BarDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=BarDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True",
options => options.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: System.TimeSpan.FromSeconds(30),
errorNumbersToAdd: null));
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { get; set; }
public virtual DbSet<Cocktail> Cocktails { get; set; }
public virtual DbSet<CocktailComponent> CocktailComponents { get; set; }
public virtual DbSet<Order> Orders { get; set; }
}
}

View File

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

View File

@ -0,0 +1,95 @@
using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarContracts.StoragesContracts;
using BarDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace BarDatabaseImplement.Implements
{
public class CocktailStorage : ICocktailStorage
{
public List<CocktailViewModel> GetFullList()
{
using var context = new BarDatabase();
return context.Cocktails.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public List<CocktailViewModel> GetFilteredList(CocktailSearchModel model)
{
if (string.IsNullOrEmpty(model.CocktailName))
{
return new();
}
using var context = new BarDatabase();
return context.Cocktails.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.CocktailName.Contains(model.CocktailName)).ToList().Select(x => x.GetViewModel).ToList();
}
public CocktailViewModel? GetElement(CocktailSearchModel model)
{
if (string.IsNullOrEmpty(model.CocktailName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new BarDatabase();
return context.Cocktails.Include(x => x.Components).ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CocktailName) && x.CocktailName == model.CocktailName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public CocktailViewModel? Insert(CocktailBindingModel model)
{
using var context = new BarDatabase();
var newCocktail = Cocktail.Create(context, model);
if (newCocktail == null)
{
return null;
}
context.Cocktails.Add(newCocktail);
context.SaveChanges();
return newCocktail.GetViewModel;
}
public CocktailViewModel? Update(CocktailBindingModel model)
{
using var context = new BarDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var cocktail = context.Cocktails.FirstOrDefault(rec =>
rec.Id == model.Id);
if (cocktail == null)
{
return null;
}
cocktail.Update(model);
context.SaveChanges();
cocktail.UpdateComponents(context, model);
transaction.Commit();
return cocktail.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CocktailViewModel? Delete(CocktailBindingModel model)
{
using var context = new BarDatabase();
var element = context.Cocktails
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cocktails.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,77 @@
using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.StoragesContracts;
using BarContracts.ViewModels;
using BarDatabaseImplement.Models;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BarDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public List<ComponentViewModel> GetFullList()
{
using var context = new BarDatabase();
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 BarDatabase();
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 BarDatabase();
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 BarDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new BarDatabase();
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 BarDatabase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element == null)
{
return null;
}
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
}
}

View File

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BarContracts.BindingModels;
using BarContracts.SearchModels;
using BarContracts.ViewModels;
using BarContracts.StoragesContracts;
using BarDatabaseImplement.Models;
using System.Text;
using System.Threading.Tasks;
namespace BarDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public List<OrderViewModel?> GetFullList()
{
using var context = new BarDatabase();
return context.Orders.Select(x => AcessCocktailsStorage(x.GetViewModel)).ToList();
}
public List<OrderViewModel?> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new BarDatabase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => AcessCocktailsStorage(x.GetViewModel)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new BarDatabase();
return AcessCocktailsStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var order = Order.Create(model);
if (order == null)
{
return null;
}
using var context = new BarDatabase();
context.Orders.Add(order);
context.SaveChanges();
return AcessCocktailsStorage(order.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new BarDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return AcessCocktailsStorage(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new BarDatabase();
var element = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element == null)
{
return null;
}
context.Orders.Remove(element);
context.SaveChanges();
return AcessCocktailsStorage(element.GetViewModel);
}
public static OrderViewModel? AcessCocktailsStorage(OrderViewModel? model)
{
if (model == null)
{
return null;
}
using var context = new BarDatabase();
foreach (var cocktail in context.Cocktails)
{
if (cocktail.Id == model.CocktailId)
{
model.CocktailName = cocktail.CocktailName;
break;
}
}
return model;
}
}
}

View File

@ -0,0 +1,169 @@
// <auto-generated />
using System;
using BarDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace BarDatabaseImplement.Migrations
{
[DbContext(typeof(BarDatabase))]
[Migration("20240308222742_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", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("CocktailName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Price")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Cocktails");
});
modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CocktailId")
.HasColumnType("int");
b.Property<int>("ComponentId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.HasIndex("ComponentId");
b.ToTable("CocktailComponents");
});
modelBuilder.Entity("BarDatabaseImplement.Models.Component", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ComponentName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<double>("Cost")
.HasColumnType("float");
b.HasKey("Id");
b.ToTable("Components");
});
modelBuilder.Entity("BarDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("CocktailId")
.HasColumnType("int");
b.Property<int>("Count")
.HasColumnType("int");
b.Property<DateTime>("DateCreate")
.HasColumnType("datetime2");
b.Property<DateTime?>("DateImplement")
.HasColumnType("datetime2");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("CocktailId");
b.ToTable("Orders");
});
modelBuilder.Entity("BarDatabaseImplement.Models.CocktailComponent", b =>
{
b.HasOne("BarDatabaseImplement.Models.Cocktail", "Cocktail")
.WithMany("Components")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BarDatabaseImplement.Models.Component", "Component")
.WithMany("CocktailComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Cocktail");
b.Navigation("Component");
});
modelBuilder.Entity("BarDatabaseImplement.Models.Order", b =>
{
b.HasOne("BarDatabaseImplement.Models.Cocktail", null)
.WithMany("Orders")
.HasForeignKey("CocktailId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("BarDatabaseImplement.Models.Cocktail", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("BarDatabaseImplement.Models.Component", b =>
{
b.Navigation("CocktailComponents");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,125 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BarDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Cocktails",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CocktailName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Cocktails", x => x.Id);
});
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: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CocktailId = table.Column<int>(type: "int", nullable: false),
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)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Cocktails_CocktailId",
column: x => x.CocktailId,
principalTable: "Cocktails",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "CocktailComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CocktailId = 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_CocktailComponents", x => x.Id);
table.ForeignKey(
name: "FK_CocktailComponents_Cocktails_CocktailId",
column: x => x.CocktailId,
principalTable: "Cocktails",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_CocktailComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_CocktailComponents_CocktailId",
table: "CocktailComponents",
column: "CocktailId");
migrationBuilder.CreateIndex(
name: "IX_CocktailComponents_ComponentId",
table: "CocktailComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_Orders_CocktailId",
table: "Orders",
column: "CocktailId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CocktailComponents");
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Cocktails");
}
}
}

View File

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

View File

@ -0,0 +1,88 @@
using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BarDatabaseImplement.Models
{
public class Cocktail : ICocktailModel
{
public int Id { get; set; }
[Required]
public string CocktailName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _cocktailComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> CocktailComponents
{
get
{
if (_cocktailComponents == null)
{
_cocktailComponents = Components.ToDictionary(recDC => recDC.ComponentId, recDC => (recDC.Component as IComponentModel, recDC.Count));
}
return _cocktailComponents;
}
}
[ForeignKey("CocktailId")]
public virtual List<CocktailComponent> Components { get; set; } = new();
[ForeignKey("CocktailId")]
public virtual List<Order> Orders { get; set; } = new();
public static Cocktail Create(BarDatabase context, CocktailBindingModel model)
{
return new Cocktail()
{
Id = model.Id,
CocktailName = model.CocktailName,
Price = model.Price,
Components = model.CocktailComponents.Select(x => new CocktailComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(CocktailBindingModel model)
{
CocktailName = model.CocktailName;
Price = model.Price;
}
public CocktailViewModel GetViewModel => new()
{
Id = Id,
CocktailName = CocktailName,
Price = Price,
CocktailComponents = CocktailComponents
};
public void UpdateComponents(BarDatabase context, CocktailBindingModel model)
{
var cocktailComponents = context.CocktailComponents.Where(rec => rec.CocktailId == model.Id).ToList();
if (cocktailComponents != null && cocktailComponents.Count > 0)
{
context.CocktailComponents.RemoveRange(cocktailComponents.Where(rec => !model.CocktailComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in cocktailComponents)
{
updateComponent.Count = model.CocktailComponents[updateComponent.ComponentId].Item2;
model.CocktailComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var cocktail = context.Cocktails.First(x => x.Id == Id);
foreach (var pc in model.CocktailComponents)
{
context.CocktailComponents.Add(new CocktailComponent
{
Cocktail = cocktail,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_cocktailComponents = 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 BarDatabaseImplement.Models
{
public class CocktailComponent
{
public int Id { get; set; }
[Required]
public int CocktailId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Cocktail Cocktail { get; set; } = new();
}
}

View File

@ -0,0 +1,60 @@
using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace BarDatabaseImplement.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<CocktailComponent> CocktailComponents { 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,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarContracts.BindingModels;
using BarContracts.ViewModels;
using BarDataModels.Models;
using BarDataModels.Enums;
using System.Xml.Linq;
using System.ComponentModel.DataAnnotations;
namespace BarDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int CocktailId { 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; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
CocktailId = model.CocktailId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
CocktailId = CocktailId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -6,9 +6,19 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarDataModels\BarDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -73,15 +73,8 @@ namespace BarFileImplement.Implements
}
public OrderViewModel AcessCocktailsStorage(OrderViewModel model)
{
if (model == null)
{
return null;
}
var component = source.Cocktails.FirstOrDefault(x => x.Id == model.CocktailId);
if (component != null)
{
model.CocktailName = component.CocktailName;
}
return model;
}
}

View File

@ -7,6 +7,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarDataModels\BarDataModels.csproj" />
</ItemGroup>

View File

@ -53,9 +53,9 @@ namespace BarListImplement.Implements
}
foreach (var cocktail in _source.Cocktails)
{
if (!string.IsNullOrEmpty(model.CocktailName) &&
cocktail.CocktailName == model.CocktailName ||
model.Id.HasValue && cocktail.Id == model.Id)
if ((!string.IsNullOrEmpty(model.CocktailName) &&
cocktail.CocktailName == model.CocktailName) ||
(model.Id.HasValue && cocktail.Id == model.Id))
{
return cocktail.GetViewModel;
}

View File

@ -9,6 +9,16 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
@ -21,8 +31,9 @@
<ItemGroup>
<ProjectReference Include="..\BarBusinessLogic\BarBusinessLogic.csproj" />
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarListImplement\BarListImplement.csproj" />
<ProjectReference Include="..\BarFileImplement\BarFileImplement.csproj" />
<ProjectReference Include="..\BarBusinessLogic\BarBusinessLogic.csproj" />
<ProjectReference Include="..\BarContracts\BarContracts.csproj" />
<ProjectReference Include="..\BarDatabaseImplement\BarDatabaseImplement.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -43,7 +43,7 @@
label1.AutoSize = true;
label1.Location = new Point(40, 30);
label1.Name = "label1";
label1.Size = new Size(61, 15);
label1.Size = new Size(56, 15);
label1.TabIndex = 0;
label1.Text = "Коктейль:";
//

View File

@ -121,14 +121,14 @@
// компонентыToolStripMenuItem1
//
компонентыToolStripMenuItem1.Name = омпонентыToolStripMenuItem1";
компонентыToolStripMenuItem1.Size = new Size(180, 22);
компонентыToolStripMenuItem1.Size = new Size(145, 22);
компонентыToolStripMenuItem1.Text = "Компоненты";
компонентыToolStripMenuItem1.Click += КомпонентыToolStripMenuItem_Click;
//
// коктейлиToolStripMenuItem
//
коктейлиToolStripMenuItem.Name = октейлиToolStripMenuItem";
коктейлиToolStripMenuItem.Size = new Size(180, 22);
коктейлиToolStripMenuItem.Size = new Size(145, 22);
коктейлиToolStripMenuItem.Text = "Коктейли";
коктейлиToolStripMenuItem.Click += КоктейлиToolStripMenuItem_Click;
//

View File

@ -2,11 +2,11 @@ using BarBusinessLogic;
using BarContracts.BusinessLogicsContracts;
using BarContracts.StoragesContracts;
using Microsoft.Extensions.DependencyInjection;
using BarFileImplement.Implements;
using BarDatabaseImplement.Implements;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using BarDataModels;
using BarBusinessLogic.BusinessLogic;
using BarBusinessLogic.BusinessLogics;
namespace BarView
{