Новый проект хранения данных + модели
This commit is contained in:
parent
07dc91676c
commit
9a3f4f5271
@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomobilePlant", "Automobi
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomomilePlantFileImplement", "AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj", "{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDataBaseImplements", "AutomobilePlantDataBaseImplements\AutomobilePlantDataBaseImplements.csproj", "{166A9D0A-A545-4634-811B-7BC2BC33631D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -45,6 +47,10 @@ Global
|
||||
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3EC099D5-0C5C-43F5-AC6D-09B4D7CE9D72}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{166A9D0A-A545-4634-811B-7BC2BC33631D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -19,6 +19,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -0,0 +1,31 @@
|
||||
using AutomobilePlantDataBaseImplements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AutomobilePlantDataBaseImplements
|
||||
{
|
||||
public class AutoPlantDataBase : DbContext
|
||||
{
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("Server=PostgreSQL;Host=localhost;Port=5432;Database=AutoPlantDataBase;Username=postgres;Password=postgres");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public virtual DbSet<Component> Components { set; get; }
|
||||
|
||||
public virtual DbSet<Car> Cars { set; get; }
|
||||
|
||||
public virtual DbSet<CarComponent> CarComponents { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
<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.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AbstractAutoContracts\AutomobilePlantContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
104
AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs
Normal file
104
AutomobilePlant/AutomobilePlantDataBaseImplements/Models/Car.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using AutomobilePlantDataModels.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 AutomobilePlantContracts.BindingModels;
|
||||
using AutomobilePlantContracts.ViewModel;
|
||||
|
||||
namespace AutomobilePlantDataBaseImplements.Models
|
||||
{
|
||||
public class Car : ICarModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string CarName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _carComponents = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> CarComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_carComponents == null)
|
||||
{
|
||||
_carComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _carComponents;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("CarId")]
|
||||
public virtual List<CarComponent> Components { get; set; } = new();
|
||||
|
||||
[ForeignKey("CarId")]
|
||||
public virtual List<Order> Orders { get; set; } = new();
|
||||
|
||||
public static Car Create(AutoPlantDataBase context, CarBindingModel model)
|
||||
{
|
||||
return new Car()
|
||||
{
|
||||
Id = model.Id,
|
||||
CarName = model.CarName,
|
||||
Price = model.Price,
|
||||
Components = model.CarComponents.Select(x => new CarComponent
|
||||
{
|
||||
Component = context.Components.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(CarBindingModel model)
|
||||
{
|
||||
CarName = model.CarName;
|
||||
Price = model.Price;
|
||||
}
|
||||
|
||||
public CarViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
CarName = CarName,
|
||||
Price = Price,
|
||||
CarComponents = CarComponents
|
||||
};
|
||||
|
||||
public void UpdateComponents(AutoPlantDataBase context, CarBindingModel model)
|
||||
{
|
||||
var carComponents = context.CarComponents.Where(rec => rec.CarId == model.Id).ToList();
|
||||
if (carComponents != null && carComponents.Count > 0)
|
||||
{
|
||||
context.CarComponents.RemoveRange(carComponents.Where(rec => !model.CarComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
|
||||
foreach (var updateComponent in carComponents)
|
||||
{
|
||||
updateComponent.Count = model.CarComponents[updateComponent.ComponentId].Item2;
|
||||
model.CarComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var car = context.Cars.First(x => x.Id == Id);
|
||||
foreach (var pc in model.CarComponents)
|
||||
{
|
||||
context.CarComponents.Add(new CarComponent
|
||||
{
|
||||
Car = car,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_carComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AutomobilePlantDataBaseImplements.Models
|
||||
{
|
||||
public class CarComponent
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int CarId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ComponentId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Component Component { get; set; } = new();
|
||||
|
||||
public virtual Car Car { get; set; } = new();
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
using AutomobilePlantContracts.BindingModels;
|
||||
using AutomobilePlantContracts.ViewModel;
|
||||
using AutomobilePlantDataModels.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;
|
||||
|
||||
namespace AutomobilePlantDataBaseImplements.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<CarComponent> CarComponents { 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,73 @@
|
||||
using AutomobilePlantContracts.BindingModels;
|
||||
using AutomobilePlantContracts.ViewModel;
|
||||
using AutomobilePlantDataModels.Enums;
|
||||
using AutomobilePlantDataModels.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 AutomobilePlantDataBaseImplements.Models
|
||||
{
|
||||
public class Order : IOrderModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public int CarId { get; private set; }
|
||||
public string CarName { get; private set; }
|
||||
public Car car { get; private set; }
|
||||
[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.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Order()
|
||||
{
|
||||
Id = model.Id,
|
||||
CarId = model.CarId,
|
||||
CarName = model.CarName,
|
||||
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;
|
||||
DateImplement = model.DateImplement;
|
||||
}
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
CarId = CarId,
|
||||
CarName = CarName,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user