Presnyakova V.V Lab_3_Hard #11
@ -14,6 +14,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic",
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{B8C8AA30-FC16-4331-B456-CD16C3C97B25}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreFileImplement", "..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{2906CD1C-4D78-4329-A2E9-8B67E624DD92}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreFileImplement", "..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{2906CD1C-4D78-4329-A2E9-8B67E624DD92}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDatabaseImplement", "..\JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj", "{672057E7-DCAE-4B6B-A3EF-39690E577C9D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -10,6 +10,10 @@
|
||||
<JewelReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<JewelReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
12
JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs
Normal file
12
JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement
|
||||
{
|
||||
public class JewelryStoreDataBase
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JewelryStoreContracts\JewelryStoreContracts.csproj" />
|
||||
<ProjectReference Include="..\JewelryStoreDataModels\JewelryStoreDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Implements\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
61
JewelryStoreDatabaseImplement/Models/Component.cs
Normal file
61
JewelryStoreDatabaseImplement/Models/Component.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreDatabaseImplement.Models;
|
||||
using JewelryStoreDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.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<JewelComponent> JewelComponents { 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
|
||||
};
|
||||
}
|
||||
}
|
106
JewelryStoreDatabaseImplement/Models/Jewel.cs
Normal file
106
JewelryStoreDatabaseImplement/Models/Jewel.cs
Normal file
@ -0,0 +1,106 @@
|
||||
using JewelryStoreDataModels.Models;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
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 JewelryStoreDatabaseImplement.Models
|
||||
{
|
||||
public class Jewel : IJewelModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string JewelName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _jewelComponents = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> JewelComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_jewelComponents == null)
|
||||
{
|
||||
_jewelComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _jewelComponents;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("JewelId")]
|
||||
public virtual List<JewelComponent> Components { get; set; } = new();
|
||||
|
||||
[ForeignKey("JewelId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
public static Jewel Create(JewelryStoreDataBase context, JewelBindingModel model)
|
||||
{
|
||||
return new Jewel()
|
||||
{
|
||||
Id = model.Id,
|
||||
JewelName = model.JewelName,
|
||||
Price = model.Price,
|
||||
Components = model.JewelComponents.Select(x => new JewelComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(JewelBindingModel model)
|
||||
{
|
||||
JewelName = model.JewelName;
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
public JewelViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
JewelName = JewelName,
|
||||
Price = Price,
|
||||
JewelComponents = JewelComponents
|
||||
};
|
||||
|
||||
public void UpdateComponents(JewelryStoreDataBase context, JewelBindingModel model)
|
||||
{
|
||||
var jewelComponents = context.JewelComponents.Where(rec => rec.JewelId == model.Id).ToList();
|
||||
|
||||
if (jewelComponents != null && jewelComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.JewelComponents.RemoveRange(jewelComponents.Where(rec => !model.JewelComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in jewelComponents)
|
||||
{
|
||||
updateComponent.Count = model.JewelComponents[updateComponent.ComponentId].Item2;
|
||||
model.JewelComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
var jewel = context.Jewels.First(x => x.Id == Id);
|
||||
|
||||
foreach (var pc in model.JewelComponents)
|
||||
{
|
||||
context.JewelComponents.Add(new JewelComponent
|
||||
{
|
||||
Jewel = jewel,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_jewelComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
26
JewelryStoreDatabaseImplement/Models/JewelComponent.cs
Normal file
26
JewelryStoreDatabaseImplement/Models/JewelComponent.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Models
|
||||
{
|
||||
public class JewelComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int JewelId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Jewel Jewel { get; set; } = new();
|
||||
}
|
||||
}
|
87
JewelryStoreDatabaseImplement/Models/Order.cs
Normal file
87
JewelryStoreDatabaseImplement/Models/Order.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreDataModels.Models;
|
||||
using JewelryStoreDataModels.Enums;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
public int JewelId { get; private set; }
|
||||
|
||||
public string JewelName { get; private set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int Count { get; private set; }
|
||||
|
||||
[Required]
|
||||
public double Sum { get; private set; }
|
||||
|
||||
[Required]
|
||||
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
|
||||
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; } = DateTime.Now;
|
||||
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public virtual Jewel Jewel { get; set; }
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
JewelId = model.JewelId,
|
||||
JewelName = model.JewelName,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
JewelId = model.JewelId;
|
||||
JewelName = model.JewelName;
|
||||
Count = model.Count;
|
||||
Sum = model.Sum;
|
||||
Status = model.Status;
|
||||
DateCreate = model.DateCreate;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
JewelId = JewelId,
|
||||
JewelName = JewelName,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user