Готовы некоторые модели для создания бд

This commit is contained in:
Николай 2023-04-01 14:32:31 +04:00
parent d00a938840
commit b29b07baea
15 changed files with 368 additions and 7 deletions

View File

@ -8,20 +8,14 @@ namespace HardwareShopContracts.BindingModels
{
public int Id { get; set; }
[DisplayName("Цена")]
public decimal Sum { get; set; }
[DisplayName("Статус покупки")]
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
[DisplayName("Дата оплаты")]
public DateTime? DatePurchase { get; set; }
public int UserID { get; set; }
[DisplayName("Email клиента")]
public string UserEmail { get; set; } = string.Empty;
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; set; }
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();

View File

@ -0,0 +1,40 @@
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement
{
public class HardwareShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-SINQU55\SQLEXPRESS;Initial Catalog=HardwareShopDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BuildComponent>().HasKey(x => new { x.ComponentId, x.BuildID });
}
public virtual DbSet<Build> Builds { set; get; }
public virtual DbSet<Comment> Comments { set; get; }
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Good> Goods { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Purchase> Purchase { set; get; }
public virtual DbSet<PurchaseBuild> PurchasesBuilds { set; get; }
public virtual DbSet<PurchaseGood> PurchasesGoods { set; get; }
public virtual DbSet<User> Users { set; get; }
}
}

View File

@ -6,4 +6,18 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HardwareShopContracts\HardwareShopContracts.csproj" />
<ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,105 @@
using HardwareShopDataModels.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models
{
public class Build : IBuildModel
{
public int Id { get; set; }
[Required]
public decimal Price { get; set; }
[Required]
public string BuildName { get; set; } = string.Empty;
[Required]
public int UserID { get; set; }
[ForeignKey("BuildId")]
public virtual List<BuildComponent>? Components { get; set; }
[ForeignKey("BuildId")]
public virtual List<PurchaseBuild>? Purchases { get; set; }
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BuildComponents
{
get
{
if (_buildComponents == null)
{
_buildComponents = Components.ToDictionary(recBC => recBC.ComponentId, recBC => (recBC.Component as IComponentModel, recBC.Count));
}
return _buildComponents;
}
}
//public static Build Create(HardwareShopDatabase context, BuildBindingModel model)
//{
// return new Build()
// {
// Id = model.Id,
// BuildName = model.BuildName,
// Price = model.Price,
// Components = model.BuildComponents.Select(x => new BuildComponent
// {
// Component = context.Components.First(y => y.Id == x.Key),
// Count = x.Value.Item2
// }).ToList()
// };
//}
//public void Update(BuildBindingModel model)
//{
// BuildName = model.BuildName;
// Price = model.Price;
//}
//public BuildViewModel GetViewModel => new()
//{
// Id = Id,
// BuildName = BuildName,
// Price = Price,
// BuildComponents = BuildComponents
//};
//public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model)
//{
// var buildComponents = context.BuildComponents.Where(rec => rec.BuildId == model.Id).ToList();
// if (buildComponents != null && buildComponents.Count > 0)
// { // удалили те в бд, которых нет в модели
// context.BuildComponents.RemoveRange(buildComponents.Where(rec => !model.BuildComponents.ContainsKey(rec.ComponentId)));
// context.SaveChanges();
// // обновили количество у существующих записей
// foreach (var updateComponent in buildComponents)
// {
// updateComponent.Count = model.BuildComponents[updateComponent.ComponentId].Item2;
// model.BuildComponents.Remove(updateComponent.ComponentId);
// }
// context.SaveChanges();
// }
// var build = context.Builds.First(x => x.Id == Id);
// //добавляем в бд блюда которые есть в моделе, но ещё нет в бд
// foreach (var dc in model.BuildComponents)
// {
// context.BuildComponents.Add(new BuildComponent
// {
// Build = build,
// Component = context.Components.First(x => x.Id == dc.Key),
// Count = dc.Value.Item2
// });
// context.SaveChanges();
// }
// _buildComponents = null;
//}
}
}

View File

@ -0,0 +1,26 @@
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 HardwareShopDatabaseImplement.Models
{
public class BuildComponent
{
public int BuildID { get; set; }
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Build Build { get; set; } = new();
public virtual Component Component { get; set; } = new();
}
}

View File

@ -0,0 +1,18 @@
using HardwareShopDataModels.Models;
using System.ComponentModel;
namespace HardwareShopDatabaseImplement.Models
{
public class Comment : ICommentModel
{
public int Id { get; set; }
public string Text { get; set; } = string.Empty;
public string BuildName { get; set; } = string.Empty;
public int BuildID { get; set; }
public virtual Build Build { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using HardwareShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopDatabaseImplement.Models
{
public class Good : IGoodModel
{
public int Id { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using HardwareShopDataModels.Models;
using System.ComponentModel;
namespace HardwareShopDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; set; }
}
}

View File

@ -0,0 +1,35 @@
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using HardwareShopDataModels.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models
{
public class Purchase : IPurchaseModel
{
public int Id { get; set; }
public decimal Sum { get; set; }
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
public DateTime? DatePurchase { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds { get; set; }
public Dictionary<int, (IGoodModel, int)> PurchaseGoods { get; set; } = new();
[ForeignKey("PurchaseId")]
public virtual List<PurchaseBuild>? Builds { get; set; }
[ForeignKey("PurchaseId")]
public virtual List<PurchaseGood> Goods { get; set; } = new();
}
}

View File

@ -0,0 +1,29 @@
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 HardwareShopDatabaseImplement.Models
{
public class PurchaseBuild
{
[Key]
[Column(Order = 1)]
public int BuildID { get; set; }
[Key]
[Column(Order = 2)]
public int PurchaseID { get; set; }
[Required]
public int Count { get; set; }
public virtual Build Build { get; set; } = new();
public virtual Purchase Purchase { get; set; } = new();
}
}

View File

@ -0,0 +1,29 @@
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 HardwareShopDatabaseImplement.Models
{
public class PurchaseGood
{
[Key]
[Column(Order = 1)]
public int PurchaseID { get; set; }
[Key]
[Column(Order = 2)]
public int GoodID { get; set; }
[Required]
public int Count { get; set; }
public virtual Purchase Purchase { get; set; } = new();
public virtual Good Good { get; set; } = new();
}
}

View File

@ -0,0 +1,19 @@
using HardwareShopDataModels.Models;
using HardwareShopDataModels.Enums;
using System.ComponentModel;
namespace HardwareShopDatabaseImplement.Models
{
public class User : IUserModel
{
public int Id { get; set; }
public string Login { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public UserRole Role { get; set; } = UserRole.Неизвестен;
}
}

View File

@ -8,4 +8,15 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj" />
</ItemGroup>
</Project>

View File

@ -1,3 +1,4 @@
using HardwareShopDatabaseImplement;
namespace HardwareShopView
{
internal static class Program
@ -11,7 +12,7 @@ namespace HardwareShopView
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
Application.Run();
}
}
}