Kharlamov A.A. Lab Work 03 #3

Closed
Kharlamov wants to merge 4 commits from LabWork_03 into LabWork_02
8 changed files with 309 additions and 2 deletions
Showing only changes of commit 0b1ce67481 - Show all commits

View File

@ -0,0 +1,29 @@
using AbstractFoodOrdersDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFoodOrdersDatabaseImplement
{
public class AbstractFoodOrdersDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder
optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-7A1PHA0\SQLEXPRESS;
Initial Catalog=AbstractFoodOrdersDatabaseFull;
Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Dish> Dishes { set; get; }
public virtual DbSet<DishComponent> DishComponents { 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="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj" />
<ProjectReference Include="..\AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,57 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace AbstractFoodOrdersDatabaseImplement.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<DishComponent> DishComponents { 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,101 @@
using AbstractFoodOrdersDataModels.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;
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
namespace AbstractFoodOrdersDatabaseImplement.Models
{
public class Dish : IDishModel
{
public int Id { get; set; }
[Required]
public string DishName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> DishComponents
{
get
{
if (_dishComponents == null)
{
_dishComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _dishComponents;
}
}
[ForeignKey("ProductId")]
public virtual List<DishComponent> Components { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<Order> Orders { get; set; } = new();
public static Dish Create(AbstractFoodOrdersDatabase context,
DishBindingModel model)
{
return new Dish()
{
Id = model.Id,
DishName = model.DishName,
Price = model.Price,
Components = model.DishComponents.Select(x => new
DishComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(DishBindingModel model)
{
DishName = model.DishName;
Price = model.Price;
}
public DishViewModel GetViewModel => new()
{
Id = Id,
DishName = DishName,
Price = Price,
DishComponents = DishComponents
};
public void UpdateComponents(AbstractFoodOrdersDatabase context,
DishBindingModel model)
{
var dishComponents = context.DishComponents.Where(rec =>
rec.DishId == model.Id).ToList();
if (dishComponents != null && dishComponents.Count > 0)
{ // удалили те, которых нет в модели
context.DishComponents.RemoveRange(dishComponents.Where(rec
=> !model.DishComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in dishComponents)
{
updateComponent.Count =
model.DishComponents[updateComponent.ComponentId].Item2;
model.DishComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var dish = context.Dishes.First(x => x.Id == Id);
foreach (var pc in model.DishComponents)
{
context.DishComponents.Add(new DishComponent
{
Dish = dish,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_dishComponents = 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 AbstractFoodOrdersDatabaseImplement.Models
{
public class DishComponent
{
public int Id { get; set; }
[Required]
public int DishId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Dish Dish { get; set; } = new();
}
}

View File

@ -0,0 +1,65 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersDataModels.Enums;
using AbstractFoodOrdersDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFoodOrdersDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int DishId { 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; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
DishId = model.DishId,
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;
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DishId = DishId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersBusinessL
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersListImplement", "AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj", "{275ED134-536F-4025-BE10-2718F66FBD10}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersFileImplement", "AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj", "{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersFileImplement", "AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj", "{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersDatabaseImplement", "AbstractFoodOrdersDatabaseImplement\AbstractFoodOrdersDatabaseImplement.csproj", "{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.Build.0 = Release|Any CPU
{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6FA5D86-8EBB-46C7-93D5-CE9FFC7A3566}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -9,8 +9,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />
</ItemGroup>
<ItemGroup>