Создан новый проект, модели в нем

This commit is contained in:
prodigygirl 2023-02-26 20:20:41 +04:00
parent cba86d7615
commit ac8a0e4602
7 changed files with 311 additions and 1 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyBusinessLo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplement", "FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj", "{6662252C-A676-4376-AA01-0ACC6AE5B217}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemFileImplement", "FurnitureAssemFileImplement\FurnitureAssemFileImplement.csproj", "{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemFileImplement", "FurnitureAssemFileImplement\FurnitureAssemFileImplement.csproj", "{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyDatabaseImplement", "FurnitureAssemblyDatabaseImplement\FurnitureAssemblyDatabaseImplement.csproj", "{F4FAEBEF-1E02-44DE-A13B-27A8C3838D98}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A6822E0E-DBF3-4DEA-A337-C6C64429C0DE}.Release|Any CPU.Build.0 = Release|Any CPU
{F4FAEBEF-1E02-44DE-A13B-27A8C3838D98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F4FAEBEF-1E02-44DE-A13B-27A8C3838D98}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F4FAEBEF-1E02-44DE-A13B-27A8C3838D98}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F4FAEBEF-1E02-44DE-A13B-27A8C3838D98}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -19,6 +19,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.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />

View File

@ -0,0 +1,27 @@
<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.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>
<ItemGroup>
<ProjectReference Include="..\FurnitureAssemblyContracts\FurnitureAssemblyContracts.csproj" />
<ProjectReference Include="..\FurnitureAssemblyDataModels\FurnitureAssemblyDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,58 @@

using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace FurnitureAssemblyDatabaseImplement.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<FurnitureComponent> FurnitureComponents { 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,98 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Models
{
public class Furniture : IFurnitureModel
{
public int Id { get; set; }
[Required]
public string FurnitureName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _furnitureComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> FurnitureComponents
{
get
{
if (_furnitureComponents == null)
{
_furnitureComponents = Components
.ToDictionary(recPC => recPC.ComponentId, recPC =>
(recPC.Component as IComponentModel, recPC.Count));
}
return _furnitureComponents;
}
}
[ForeignKey("FurnitureId")]
public virtual List<FurnitureComponent> Components { get; set; } = new();
[ForeignKey("FurnitureId")]
public virtual List<Order> Orders { get; set; } = new();
public static Furniture Create(FurnitureAssemblyDatabase context, FurnitureBindingModel model)
{
return new Furniture()
{
Id = model.Id,
FurnitureName = model.FurnitureName,
Price = model.Price,
Components = model.FurnitureComponents.Select(x => new FurnitureComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(FurnitureBindingModel model)
{
FurnitureName = model.FurnitureName;
Price = model.Price;
}
public FurnitureViewModel GetViewModel => new()
{
Id = Id,
FurnitureName = FurnitureName,
Price = Price,
FurnitureComponents = FurnitureComponents
};
public void UpdateComponents(FurnitureAssemblyDatabase context, FurnitureBindingModel model)
{
var furnitureComponents = context.FurnitureComponents.Where(rec => rec.ProductId == model.Id).ToList();
if (furnitureComponents != null && furnitureComponents.Count > 0)
{ // удалили те, которых нет в модели
context.ProductComponents.RemoveRange(furnitureComponents.Where(rec
=> !model.FurnitureComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in furnitureComponents)
{
updateComponent.Count = model.FurnitureComponents[updateComponent.ComponentId].Item2;
model.FurnitureComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var furniture = context.Furnitures.First(x => x.Id == Id);
foreach (var pc in model.FurnitureComponents)
{
context.ProductComponents.Add(new FurnitureComponent
{
Furniture = furniture,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_furnitureComponents = null;
}
}
}

View File

@ -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 FurnitureAssemblyDatabaseImplement.Models
{
public class FurnitureComponent
{
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 Furniture Furniture { get; set; } = new();
}
}

View File

@ -0,0 +1,94 @@
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyDataModels.Enums;
using FurnitureAssemblyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int FurnitureId { get; private set; }
public string FurnitureName { 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 static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
FurnitureId = model.FurnitureId,
FurnitureName = model.FurnitureName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public static Order Create(OrderViewModel model)
{
return new Order
{
Id = model.Id,
FurnitureId = model.FurnitureId,
FurnitureName = model.FurnitureName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel model)
{
if (model == null)
{
return;
}
FurnitureId = model.FurnitureId;
FurnitureName = model.FurnitureName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
FurnitureId = FurnitureId,
FurnitureName = FurnitureName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}