some done
This commit is contained in:
parent
b3208294f8
commit
dc0f13d41a
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopView", "B
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopDatabaseImplement", "BlacksmithWorkshopDatabaseImplement\BlacksmithWorkshopDatabaseImplement.csproj", "{99CF0DD1-5945-426C-9343-461A72204B74}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{99CF0DD1-5945-426C-9343-461A72204B74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{99CF0DD1-5945-426C-9343-461A72204B74}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{99CF0DD1-5945-426C-9343-461A72204B74}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{99CF0DD1-5945-426C-9343-461A72204B74}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -0,0 +1,21 @@
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement
|
||||
{
|
||||
public class BlacksmithWorkshopDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
public virtual DbSet<Manufacture> Manufactures { set; get; }
|
||||
public virtual DbSet<ManufactureComponent> ManufactureComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<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" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BlacksmithWorkshopContracts\BlacksmithWorkshopContracts.csproj" />
|
||||
<ProjectReference Include="..\BlacksmithWorkshopDataModels\BlacksmithWorkshopDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,56 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.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<ManufactureComponent> ManufactureComponents { 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
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class Manufacture : IManufactureModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public string ManufactureName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
private Dictionary<int, (IComponentModel, int)>? _manufactureComponents = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> ManufactureComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_manufactureComponents == null)
|
||||
{
|
||||
_manufactureComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC =>
|
||||
(recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _manufactureComponents;
|
||||
}
|
||||
}
|
||||
[ForeignKey("ManufactureId")]
|
||||
public virtual List<ManufactureComponent> Components { get; set; } = new();
|
||||
[ForeignKey("ManufactureId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model)
|
||||
{
|
||||
return new Manufacture()
|
||||
{
|
||||
Id = model.Id,
|
||||
ManufactureName = model.ManufactureName,
|
||||
Price = model.Price,
|
||||
Components = model.ManufactureComponents.Select(x => new ManufactureComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(ManufactureBindingModel model)
|
||||
{
|
||||
ManufactureName = model.ManufactureName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public ManufactureViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ManufactureName = ManufactureName,
|
||||
Price = Price,
|
||||
ManufactureComponents = ManufactureComponents
|
||||
};
|
||||
public void UpdateComponents(BlacksmithWorkshopDatabase context, ManufactureBindingModel model)
|
||||
{
|
||||
var manufactureComponents = context.ManufactureComponents.Where(rec =>
|
||||
rec.ManufactureId == model.Id).ToList();
|
||||
if (manufactureComponents != null && manufactureComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.ManufactureComponents.RemoveRange(manufactureComponents.Where(rec
|
||||
=> !model.ManufactureComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in manufactureComponents)
|
||||
{
|
||||
updateComponent.Count = model.ManufactureComponents[updateComponent.ComponentId].Item2;
|
||||
model.ManufactureComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var manufacture = context.Manufactures.First(x => x.Id == Id);
|
||||
foreach (var pc in model.ManufactureComponents)
|
||||
{
|
||||
context.ManufactureComponents.Add(new ManufactureComponent
|
||||
{
|
||||
Manufacture = manufacture,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_manufactureComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class ManufactureComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int ManufactureId { get; set; }
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Component Component { get; set; } = new();
|
||||
public virtual Manufacture Manufacture { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user