Основная часть database

This commit is contained in:
Arklightning 2023-03-26 18:08:04 +04:00
parent ddc088cbb3
commit fde8c30ee9
7 changed files with 334 additions and 1 deletions

View File

@ -16,7 +16,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopContracts", "C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{7A19A758-A68B-4B08-8F70-62D42A777926}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{680EC086-C5D7-4827-820A-4610675A0E79}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -48,6 +50,10 @@ Global
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B6F8AFD-D465-4D71-A381-9ED66B40A1D3}.Release|Any CPU.Build.0 = Release|Any CPU
{680EC086-C5D7-4827-820A-4610675A0E79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{680EC086-C5D7-4827-820A-4610675A0E79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{680EC086-C5D7-4827-820A-4610675A0E79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{680EC086-C5D7-4827-820A-4610675A0E79}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,32 @@
using CarRepairShopDatabaseImplement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopDatabaseImplement
{
internal class CarRepairShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-SINQU55\SQLEXPRESS;Initial Catalog=FoodOrdersDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Car> Cars { set; get; }
public virtual DbSet<Detail> Details { set; get; }
public virtual DbSet<DetailComponent> DetailComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
}
}

View File

@ -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.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CarRepairShopContracts\CarRepairShopContracts.csproj" />
<ProjectReference Include="..\CarRepairShopDataModels\CarRepairShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,69 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopDatabaseImplement.Model
{
internal class Car : ICarModel
{
public int Id { get; private set; }
[Required]
public string CarName { get; private set; } = string.Empty;
[Required]
public double Price { get; set; }
[ForeignKey("ComponentId")]
public virtual List<Detail> DishComponents { get; set; } = new();
public static Car? Create(CarBindingModel model)
{
if (model == null)
{
return null;
}
return new Car()
{
Id = model.Id,
CarName = model.CarName,
Price = model.Price
};
}
public static Car Create(CarViewModel model)
{
return new Car
{
Id = model.Id,
CarName = model.CarName,
Price = model.Price
};
}
public void Update(CarBindingModel model)
{
if (model == null)
{
return;
}
CarName = model.CarName;
Price = model.Price;
}
public CarViewModel GetViewModel => new()
{
Id = Id,
CarName = CarName,
Price = Price
};
}
}

View File

@ -0,0 +1,102 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.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 CarRepairShopDatabaseImplement.Model
{
internal class Detail : IDetailModel
{
public int Id { get; set; }
[Required]
public string DetailName { get; set; } = string.Empty;
[Required]
public double Cost { get; set; }
private Dictionary<int, (ICarModel, int)>? _dishComponents = null;
//??
[NotMapped]
public Dictionary<int, (ICarModel, int)> DishComponents
{
get
{
if (_dishComponents == null)
{
_dishComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Car as ICarModel, recPC.Count));
}
return _dishComponents;
}
}
[ForeignKey("DishId")]
public virtual List<DetailComponent> Components { get; set; } = new();
public static Detail Create(CarRepairShopDatabase context, DetailBindingModel model)
{
return new Detail()
{
Id = model.Id,
DetailName = model.DetailName,
Cost = model.Cost,
Components = model.DetailComponents.Select(x => new DetailComponent
{
Car = context.Cars.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(DetailBindingModel model)
{
DetailName = model.DetailName;
Cost = model.Cost;
}
public DetailViewModel GetViewModel => new()
{
Id = Id,
DetailName = DetailName,
Cost = Cost,
DetailComponents = DishComponents
};
public void UpdateComponents(CarRepairShopDatabase context, DetailBindingModel model)
{
var detailComponents = context.DetailComponents.Where(rec => rec.DetailId == model.Id).ToList();
if (detailComponents != null && detailComponents.Count > 0)
{ // удалили те в бд, которых нет в модели
context.DetailComponents.RemoveRange(detailComponents.Where(rec => !model.DetailComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateComponent in detailComponents)
{
updateComponent.Count = model.DetailComponents[updateComponent.ComponentId].Item2;
model.DetailComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var detail = context.Details.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var dc in model.DetailComponents)
{
context.DetailComponents.Add(new DetailComponent
{
Detail = detail,
Car = context.Cars.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_dishComponents = null;
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopDatabaseImplement.Model
{
internal class DetailComponent
{
public int Id { get; set; }
[Required]
public int DetailId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Car Car { get; set; } = new();
public virtual Detail Detail { get; set; } = new();
}
}

View File

@ -0,0 +1,77 @@
using CarRepairShopContracts.BindingModels;
using CarRepairShopContracts.ViewModels;
using CarRepairShopDataModels.Enums;
using CarRepairShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarRepairShopDatabaseImplement.Model
{
internal class Order : IOrderModel
{
public int Id { get; set; }
[Required]
public int CarId { get; set; }
[Required]
public int Count { get; set; }
[Required]
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public virtual Detail Detail { get; set; }
public DateTime? DateImplement { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
CarId = model.CarId,
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,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
CarName = Detail.DetailName
};
}
}