1
This commit is contained in:
parent
86eb33f1eb
commit
a3b3120398
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "Pi
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaFileImplement", "PizzeriaFileImplement\PizzeriaFileImplement.csproj", "{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaDatabaseImplement", "PizzeriaDatabaseImplement\PizzeriaDatabaseImplement.csproj", "{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5FA9C75E-4A06-4B3E-AD87-48B0C96BB4A1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{31FC8CAC-BED8-4DF0-8B57-20917DC66A5C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
56
Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs
Normal file
56
Pizzeria/PizzeriaDatabaseImplement/Models/Component.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using PizzeriaDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using PizzeriaContracts.BindingModels;
|
||||
using PizzeriaContracts.ViewModels;
|
||||
|
||||
namespace PizzeriaDatabaseImplement.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<PizzaComponent> ProductComponents { 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
|
||||
};
|
||||
}
|
||||
}
|
94
Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs
Normal file
94
Pizzeria/PizzeriaDatabaseImplement/Models/Pizza.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using PizzeriaDataModels.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 PizzeriaContracts.BindingModels;
|
||||
using PizzeriaContracts.ViewModels;
|
||||
|
||||
namespace PizzeriaDatabaseImplement.Models
|
||||
{
|
||||
public class Pizza : IPizzaModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string PizzaName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _PizzaComponents = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> PizzaComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_PizzaComponents == null)
|
||||
{
|
||||
_PizzaComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _PizzaComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("PizzaId")]
|
||||
public virtual List<PizzaComponent> Components { get; set; } = new();
|
||||
[ForeignKey("PizzaId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
public static Pizza Create(PizzeriaDatabase context, PizzaBindingModel model)
|
||||
{
|
||||
return new Pizza()
|
||||
{
|
||||
Id = model.Id,
|
||||
PizzaName = model.PizzaName,
|
||||
Price = model.Price,
|
||||
Components = model.PizzaComponents.Select(x => new PizzaComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(PizzaBindingModel model)
|
||||
{
|
||||
PizzaName = model.PizzaName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public PizzaViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
PizzaName = PizzaName,
|
||||
Price = Price,
|
||||
PizzaComponents = PizzaComponents
|
||||
};
|
||||
public void UpdateComponents(PizzeriaDatabase context, PizzaBindingModel model)
|
||||
{
|
||||
var PizzaComponents = context.PizzaComponents.Where(rec => rec.PizzaId == model.Id).ToList();
|
||||
if (PizzaComponents != null && PizzaComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.PizzaComponents.RemoveRange(PizzaComponents.Where(rec => !model.PizzaComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in PizzaComponents)
|
||||
{
|
||||
updateComponent.Count = model.PizzaComponents[updateComponent.ComponentId].Item2;
|
||||
model.PizzaComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var Pizza = context.Pizzas.First(x => x.Id == Id);
|
||||
foreach (var pc in model.PizzaComponents)
|
||||
{
|
||||
context.PizzaComponents.Add(new PizzaComponent
|
||||
{
|
||||
Pizza = Pizza,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_PizzaComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PizzeriaDatabaseImplement.Models
|
||||
{
|
||||
public class ProductComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int ProductId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Pizza Pizza { get; set; } = new();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PizzeriaContracts\PizzeriaContracts.csproj" />
|
||||
<ProjectReference Include="..\PizzeriaDataModels\PizzeriaDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user