ThirdLaba

This commit is contained in:
Дарья Антонова 2023-05-22 01:27:46 +04:00
parent 430ef543a0
commit bdbdba7f4d
12 changed files with 929 additions and 6 deletions

View File

@ -3,23 +3,25 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32922.545
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryView", "ConfectioneryView\ConfectioneryView.csproj", "{9B75FC6A-4403-406F-BD78-70A656A9C38C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryView", "ConfectioneryView\ConfectioneryView.csproj", "{9B75FC6A-4403-406F-BD78-70A656A9C38C}"
ProjectSection(ProjectDependencies) = postProject
{F40D4032-72B5-44F1-9657-940F5931C1C1} = {F40D4032-72B5-44F1-9657-940F5931C1C1}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{138F5333-8A07-47A5-B6FB-5F5170E89D50}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{138F5333-8A07-47A5-B6FB-5F5170E89D50}"
ProjectSection(ProjectDependencies) = postProject
{AE5AAF6C-4EA0-45F6-A433-50D1A347DD60} = {AE5AAF6C-4EA0-45F6-A433-50D1A347DD60}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{70FA9740-C8FF-48F1-939A-8B6AAB8CB6FD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{70FA9740-C8FF-48F1-939A-8B6AAB8CB6FD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{FF4C1B6D-E988-4CE0-AF41-C4539744B9BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{F40D4032-72B5-44F1-9657-940F5931C1C1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryFileImplement", "ConfectioneryFileImplement\ConfectioneryFileImplement.csproj", "{F40D4032-72B5-44F1-9657-940F5931C1C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDatabaseImplement", "ConfectioneryDatabaseImplement\ConfectioneryDatabaseImplement.csproj", "{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -51,6 +53,10 @@ Global
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F40D4032-72B5-44F1-9657-940F5931C1C1}.Release|Any CPU.Build.0 = Release|Any CPU
{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDAAEEB5-96BD-4F0E-902D-C98435A6CD02}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,30 @@
using ConfectioneryDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement
{
public class ConfectioneryDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-6JVHBUD\SQLEXPRESS;Initial Catalog=SweetsDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Ingredient> Ingredients { set; get; }
public virtual DbSet<Sweets> ListSweets { set; get; }
public virtual DbSet<SweetsIngredient> SweetsIngredients { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ConfectioneryContracts\ConfectioneryContracts.csproj" />
<ProjectReference Include="..\ConfectioneryDataModels\ConfectioneryDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,88 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Implements
{
public class IngredientStorage : IIngredientStorage
{
public List<IngredientViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Ingredients
.Select(x => x.GetViewModel)
.ToList();
}
public List<IngredientViewModel> GetFilteredList(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.Ingredients
.Where(x => x.IngredientName.Contains(model.IngredientName))
.Select(x => x.GetViewModel)
.ToList();
}
public IngredientViewModel? GetElement(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.Ingredients
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName == model.IngredientName) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public IngredientViewModel? Insert(IngredientBindingModel model)
{
var newIngredient = Ingredient.Create(model);
if (newIngredient == null)
{
return null;
}
using var context = new ConfectioneryDatabase();
context.Ingredients.Add(newIngredient);
context.SaveChanges();
return newIngredient.GetViewModel;
}
public IngredientViewModel? Update(IngredientBindingModel model)
{
using var context = new ConfectioneryDatabase();
var ingredient = context.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (ingredient == null)
{
return null;
}
ingredient.Update(model);
context.SaveChanges();
return ingredient.GetViewModel;
}
public IngredientViewModel? Delete(IngredientBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Ingredients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

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

View File

@ -0,0 +1,112 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using Microsoft.EntityFrameworkCore;
using ConfectioneryDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Implements
{
public class SweetsStorage : ISweetsStorage
{
public List<SweetsViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.ListSweets
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<SweetsViewModel> GetFilteredList(SweetsSearchModel model)
{
if (string.IsNullOrEmpty(model.SweetsName))
{
return new();
}
using var context = new ConfectioneryDatabase();
return context.ListSweets
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.Where(x => x.SweetsName.Contains(model.SweetsName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public SweetsViewModel? GetElement(SweetsSearchModel model)
{
if (string.IsNullOrEmpty(model.SweetsName) && !model.Id.HasValue)
{
return null;
}
using var context = new ConfectioneryDatabase();
return context.ListSweets
.Include(x => x.Ingredients)
.ThenInclude(x => x.Ingredient)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SweetsName) && x.SweetsName == model.SweetsName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public SweetsViewModel? Insert(SweetsBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newSweets = Sweets.Create(context, model);
if (newSweets == null)
{
return null;
}
context.ListSweets.Add(newSweets);
context.SaveChanges();
return newSweets.GetViewModel;
}
public SweetsViewModel? Update(SweetsBindingModel model)
{
using var context = new ConfectioneryDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var sweets = context.ListSweets.FirstOrDefault(rec => rec.Id == model.Id);
if (sweets == null)
{
return null;
}
sweets.Update(model);
context.SaveChanges();
sweets.UpdateIngredients(context, model);
transaction.Commit();
return sweets.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public SweetsViewModel? Delete(SweetsBindingModel model)
{
using var context = new ConfectioneryDatabase();
var element = context.ListSweets
.Include(x => x.Ingredients)
.Include(x => x.Orders)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.ListSweets.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,125 @@
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Ingredients",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IngredientName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Ingredients", x => x.Id);
});
migrationBuilder.CreateTable(
name: "ListSweets",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SweetsName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ListSweets", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SweetsId = 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_ListSweets_SweetsId",
column: x => x.SweetsId,
principalTable: "ListSweets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "SweetsIngredients",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
SweetsId = table.Column<int>(type: "int", nullable: false),
IngredientId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SweetsIngredients", x => x.Id);
table.ForeignKey(
name: "FK_SweetsIngredients_Ingredients_IngredientId",
column: x => x.IngredientId,
principalTable: "Ingredients",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_SweetsIngredients_ListSweets_SweetsId",
column: x => x.SweetsId,
principalTable: "ListSweets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_SweetsId",
table: "Orders",
column: "SweetsId");
migrationBuilder.CreateIndex(
name: "IX_SweetsIngredients_IngredientId",
table: "SweetsIngredients",
column: "IngredientId");
migrationBuilder.CreateIndex(
name: "IX_SweetsIngredients_SweetsId",
table: "SweetsIngredients",
column: "SweetsId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "SweetsIngredients");
migrationBuilder.DropTable(
name: "Ingredients");
migrationBuilder.DropTable(
name: "ListSweets");
}
}
}

View File

@ -0,0 +1,169 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#nullable disable
namespace ConfectioneryDatabaseImplement.Migrations
{
[DbContext(typeof(ConfectioneryDatabase))]
partial class ConfectioneryDatabaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Ingredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Cost")
.HasColumnType("float");
b.Property<string>("IngredientName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Ingredients");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
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.Property<int>("SweetsId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("SweetsId");
b.ToTable("Orders");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Sweets", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("SweetsName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("ListSweets");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.SweetsIngredient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("IngredientId")
.HasColumnType("int");
b.Property<int>("SweetsId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("IngredientId");
b.HasIndex("SweetsId");
b.ToTable("SweetsIngredients");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Sweets", "Sweets")
.WithMany("Orders")
.HasForeignKey("SweetsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Sweets");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.SweetsIngredient", b =>
{
b.HasOne("ConfectioneryDatabaseImplement.Models.Ingredient", "Ingredient")
.WithMany("ConfectioneryIngredients")
.HasForeignKey("IngredientId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ConfectioneryDatabaseImplement.Models.Sweets", "Sweets")
.WithMany("Ingredients")
.HasForeignKey("SweetsId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Ingredient");
b.Navigation("Sweets");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Ingredient", b =>
{
b.Navigation("ConfectioneryIngredients");
});
modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Sweets", b =>
{
b.Navigation("Ingredients");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Models
{
public class Ingredient : IIngredientModel
{
public int Id { get; private set; }
[Required]
public string IngredientName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("IngredientId")]
public virtual List<SweetsIngredient> ConfectioneryIngredients { get; set; } = new();
public static Ingredient? Create(IngredientBindingModel model)
{
if (model == null)
{
return null;
}
return new Ingredient()
{
Id = model.Id,
IngredientName = model.IngredientName,
Cost = model.Cost
};
}
public static Ingredient Create(IngredientViewModel model)
{
return new Ingredient
{
Id = model.Id,
IngredientName = model.IngredientName,
Cost = model.Cost
};
}
public void Update(IngredientBindingModel model)
{
if (model == null)
{
return;
}
IngredientName = model.IngredientName;
Cost = model.Cost;
}
public IngredientViewModel GetViewModel => new()
{
Id = Id,
IngredientName = IngredientName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,69 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Enums;
using ConfectioneryDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Models
{
public class Order : IOrderModel
{
[Required]
public int SweetsId { get; set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public int Id { get; set; }
public Sweets Sweets { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SweetsId = model.SweetsId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
SweetsId = SweetsId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
SweetsName = Sweets.SweetsName,
};
}
}

View File

@ -0,0 +1,102 @@
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Models
{
public class Sweets : ISweetsModel
{
public int Id { get; set; }
[Required]
public string SweetsName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IIngredientModel, int)>? _sweetsIngredients = null;
[NotMapped]
public Dictionary<int, (IIngredientModel, int)> SweetsIngredients
{
get
{
if (_sweetsIngredients == null)
{
_sweetsIngredients = Ingredients
.ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count));
}
return _sweetsIngredients;
}
}
[ForeignKey("SweetsId")]
public virtual List<SweetsIngredient> Ingredients { get; set; } = new();
[ForeignKey("SweetsId")]
public virtual List<Order> Orders { get; set; } = new();
public static Sweets Create(ConfectioneryDatabase context, SweetsBindingModel model)
{
return new Sweets()
{
Id = model.Id,
SweetsName = model.SweetsName,
Price = model.Price,
Ingredients = model.SweetsIngredients.Select(x => new SweetsIngredient
{
Ingredient = context.Ingredients.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(SweetsBindingModel model)
{
SweetsName = model.SweetsName;
Price = model.Price;
}
public SweetsViewModel GetViewModel => new()
{
Id = Id,
SweetsName = SweetsName,
Price = Price,
SweetsIngredients = SweetsIngredients
};
public void UpdateIngredients(ConfectioneryDatabase context, SweetsBindingModel model)
{
var sweetsIngredients = context.SweetsIngredients.Where(rec => rec.SweetsId == model.Id).ToList();
if (sweetsIngredients != null && sweetsIngredients.Count > 0)
{
context.SweetsIngredients.RemoveRange(sweetsIngredients.Where(rec => !model.SweetsIngredients.ContainsKey(rec.IngredientId)));
context.SaveChanges();
foreach (var updateIngredient in sweetsIngredients)
{
updateIngredient.Count = model.SweetsIngredients[updateIngredient.IngredientId].Item2;
model.SweetsIngredients.Remove(updateIngredient.IngredientId);
}
context.SaveChanges();
}
var sweets = context.ListSweets.First(x => x.Id == Id);
foreach (var si in model.SweetsIngredients)
{
context.SweetsIngredients.Add(new SweetsIngredient
{
Sweets = sweets,
Ingredient = context.Ingredients.First(x => x.Id == si.Key),
Count = si.Value.Item2
});
context.SaveChanges();
}
_sweetsIngredients = null;
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryDatabaseImplement.Models
{
public class SweetsIngredient
{
public int Id { get; set; }
[Required]
public int SweetsId { get; set; }
[Required]
public int IngredientId { get; set; }
[Required]
public int Count { get; set; }
public virtual Ingredient Ingredient { get; set; } = new();
public virtual Sweets Sweets { get; set; } = new();
}
}