база данных

This commit is contained in:
leonteva.v 2024-04-29 22:44:15 +04:00
parent fb918bb1d3
commit 6701f58fcb
19 changed files with 1251 additions and 0 deletions

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopContracts", "Ha
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopBusinessLogic", "HardwareShopBusinessLogic\HardwareShopBusinessLogic.csproj", "{A88AB7DF-27D2-4A26-8CBA-1BE3F210C07C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HardwareShopDatabaseImplement", "HardwareShopDatabaseImplement\HardwareShopDatabaseImplement.csproj", "{D61797E9-F58A-4929-AA66-A440C9CA1804}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +35,10 @@ Global
{A88AB7DF-27D2-4A26-8CBA-1BE3F210C07C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A88AB7DF-27D2-4A26-8CBA-1BE3F210C07C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A88AB7DF-27D2-4A26-8CBA-1BE3F210C07C}.Release|Any CPU.Build.0 = Release|Any CPU
{D61797E9-F58A-4929-AA66-A440C9CA1804}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D61797E9-F58A-4929-AA66-A440C9CA1804}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D61797E9-F58A-4929-AA66-A440C9CA1804}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D61797E9-F58A-4929-AA66-A440C9CA1804}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,30 @@
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Linq;
namespace HardwareShopDatabaseImplement
{
public class HardwareShopDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=HardwareShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Build> Builds { set; get; }
public virtual DbSet<BuildComponent> BuildsComponents { 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<GoodComponent> GoodComponents { set; get; }
public virtual DbSet<Purchase> Purchases { 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

@ -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.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="..\HardwareShopContracts\HardwareShopContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,125 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements
{
public class BuildStorage : IBuildStorage
{
public List<BuildViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<BuildViewModel> GetFilteredList(BuildSearchModel model)
{
if (!model.UserId.HasValue)
{
return new();
}
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public BuildViewModel? GetElement(BuildSearchModel model)
{
if (string.IsNullOrEmpty(model.BuildName) && !model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.FirstOrDefault(x => !string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName ||
model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public BuildViewModel? Insert(BuildBindingModel model)
{
using var context = new HardwareShopDatabase();
var newBuild = Build.Create(context, model);
if (newBuild == null)
{
return null;
}
context.Builds.Add(newBuild);
context.SaveChanges();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(x => x.Id == newBuild.Id)
?.GetViewModel;
}
public BuildViewModel? Update(BuildBindingModel model)
{
using var context = new HardwareShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var build = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.FirstOrDefault(x => x.Id == model.Id);
if (build == null)
{
return null;
}
build.Update(model);
context.SaveChanges();
build.UpdateComponents(context, model);
transaction.Commit();
return build?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public BuildViewModel? Delete(BuildBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Builds.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,100 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements
{
public class CommentStorage : ICommentStorage
{
public List<CommentViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CommentViewModel> GetFilteredList(CommentSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new HardwareShopDatabase();
if (model.UserId.HasValue)
{
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.Where(x => x.BuildId == model.BuildId)
.Select(x => x.GetViewModel)
.ToList();
}
public CommentViewModel? GetElement(CommentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public CommentViewModel? Insert(CommentBindingModel model)
{
using var context = new HardwareShopDatabase();
var newComment = Comment.Create(model);
if (newComment == null)
{
return null;
}
context.Comments.Add(newComment);
context.SaveChanges();
return newComment.GetViewModel;
}
public CommentViewModel? Update(CommentBindingModel model)
{
using var context = new HardwareShopDatabase();
var comment = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (comment == null)
{
return null;
}
comment.Update(model);
context.SaveChanges();
return comment.GetViewModel;
}
public CommentViewModel? Delete(CommentBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Comments
.Include(x => x.Build)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Comments.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,83 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
namespace HardwareShopDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Components.Include(x => x.User).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Components.Include(x => x.User)
.FirstOrDefault(x => !string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName ||
model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new HardwareShopDatabase();
return context.Components
.Include(x => x.User)
.Where(x => x.ComponentName.Contains(model.ComponentName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ComponentViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Components
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new HardwareShopDatabase();
context.Components.Add(newComponent);
context.SaveChanges();
return context.Components
.Include(x => x.User).FirstOrDefault(x => x.Id == newComponent.Id)?.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new HardwareShopDatabase();
var component = context.Components.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}

View File

@ -0,0 +1,112 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements
{
public class GoodStorage : IGoodStorage
{
public GoodViewModel? Delete(GoodBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Goods.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public GoodViewModel? GetElement(GoodSearchModel model)
{
if (string.IsNullOrEmpty(model.GoodName) && !model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => !string.IsNullOrEmpty(model.GoodName) && x.GoodName == model.GoodName ||
model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<GoodViewModel> GetFilteredList(GoodSearchModel model)
{
if (string.IsNullOrEmpty(model.GoodName))
{
return new();
}
using var context = new HardwareShopDatabase();
return context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.GoodName.Contains(model.GoodName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<GoodViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Select(x => x.GetViewModel)
.ToList();
}
public GoodViewModel? Insert(GoodBindingModel model)
{
using var context = new HardwareShopDatabase();
var newGood = Good.Create(context, model);
if (newGood == null)
{
return null;
}
context.Goods.Add(newGood);
context.SaveChanges();
return context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => x.Id == newGood.Id)?.GetViewModel;
}
public GoodViewModel? Update(GoodBindingModel model)
{
using var context = new HardwareShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var good = context.Goods
.Include(x => x.User)
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(rec => rec.Id == model.Id);
if (good == null)
{
return null;
}
good.Update(model);
context.SaveChanges();
good.UpdateComponents(context, model);
transaction.Commit();
return good.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,119 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements
{
public class PurchaseStorage : IPurchaseStorage
{
public List<PurchaseViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{
using var context = new HardwareShopDatabase();
if (model.UserId.HasValue)
{
return context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public PurchaseViewModel? Insert(PurchaseBindingModel model)
{
using var context = new HardwareShopDatabase();
var newPurchase = Purchase.Create(context, model);
if (newPurchase == null)
{
return null;
}
context.Purchases.Add(newPurchase);
context.SaveChanges();
return context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == newPurchase.Id)
?.GetViewModel;
}
public PurchaseViewModel? Update(PurchaseBindingModel model)
{
using var context = new HardwareShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var purchase = context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (purchase == null)
{
return null;
}
purchase.Update(model);
context.SaveChanges();
purchase.UpdateGoods(context, model);
purchase.UpdateBuilds(context, model);
transaction.Commit();
return purchase.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PurchaseViewModel? Delete(PurchaseBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Purchases
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Purchases.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,87 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models;
namespace HardwareShopDatabaseImplement.Implements
{
public class UserStorage : IUserStorage
{
public UserViewModel? Delete(UserBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Users
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Users.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public UserViewModel? GetElement(UserSearchModel model)
{
using var context = new HardwareShopDatabase();
if (model.Id.HasValue)
return context.Users
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
return context.Users
.FirstOrDefault(x => x.Email
.Equals(model.Email) && x.Password
.Equals(model.Password))?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Email))
return context.Users
.FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel;
return null;
}
public List<UserViewModel> GetFilteredList(UserSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new HardwareShopDatabase();
return context.Users
.Where(x => x.Login.Contains(model.Login))
.Select(x => x.GetViewModel)
.ToList();
}
public List<UserViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Users
.Select(x => x.GetViewModel)
.ToList();
}
public UserViewModel? Insert(UserBindingModel model)
{
var newUser = User.Create(model);
if (newUser == null)
{
return null;
}
using var context = new HardwareShopDatabase();
context.Users.Add(newUser);
context.SaveChanges();
return newUser.GetViewModel;
}
public UserViewModel? Update(UserBindingModel model)
{
using var context = new HardwareShopDatabase();
var user = context.Users
.FirstOrDefault(x => x.Id == model.Id);
if (user == null)
{
return null;
}
user.Update(model);
context.SaveChanges();
return user.GetViewModel;
}
}
}

View File

@ -0,0 +1,98 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models;
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; }
public virtual User User { get; set; }
[ForeignKey("BuildId")]
public virtual List<Comment>? Comments { 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,
Price = model.Price,
BuildName = model.BuildName,
UserId = model.UserId,
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,
UserLogin = User.Login,
UserId = UserId,
BuildComponents = BuildComponents
};
public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model)
{
var buildComponents = context.BuildsComponents.Where(rec => rec.BuildId == model.Id).ToList();
if (buildComponents != null && buildComponents.Count > 0)
{ // удалили те в бд, которых нет в модели
context.BuildsComponents.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.BuildsComponents.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,15 @@
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
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,52 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace HardwareShopDatabaseImplement.Models
{
public class Comment : ICommentModel
{
public int Id { get; set; }
[Required]
public string Text { get; set; } = string.Empty;
[Required]
public int BuildId { get; set; }
public virtual Build Build { get; set; }
[Required]
public int UserId { get; set; }
public virtual User User { get; set; }
public static Comment? Create(CommentBindingModel? model)
{
if (model == null)
{
return null;
}
return new Comment()
{
Id = model.Id,
Text = model.Text,
BuildId = model.BuildId,
UserId = model.UserId,
};
}
public void Update(CommentBindingModel? model)
{
if (model == null)
{
return;
}
Text = model.Text;
}
public CommentViewModel GetViewModel => new()
{
Id = Id,
Text = Text,
BuildId = BuildId,
BuildName = Build.BuildName,
UserId = UserId,
UserLogin = User.Login,
};
}
}

View File

@ -0,0 +1,55 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; set; }
[Required]
public string ComponentName { get; set; } = string.Empty;
[Required]
public double Cost { get; set; }
[Required]
public int UserId { get; set; }
[ForeignKey("ComponentId")]
public virtual List<GoodComponent> Goods { get; set; } = new();
[ForeignKey("ComponentId")]
public virtual List<BuildComponent> Builds { get; set; } = new();
public virtual User User { get; set; } = null!;
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost,
UserId = model.UserId
};
}
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,
UserId = UserId,
UserLogin = User.Login
};
}
}

View File

@ -0,0 +1,97 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models
{
public class Good : IGoodModel
{
public int Id { get; set; }
[Required]
public string GoodName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[Required]
public int UserId { get; set; }
public virtual User User { get; set; } = null!;
private Dictionary<int, (IComponentModel, int)>? _goodComponents = null;
[ForeignKey("GoodId")]
public virtual List<GoodComponent> Components { get; set; } = new();
[ForeignKey("GoodId")]
public virtual List<PurchaseGood> Purchases { get; set; } = new();
[NotMapped]
public Dictionary<int, (IComponentModel, int)> GoodComponents
{
get
{
if (_goodComponents == null)
{
_goodComponents = Components
.ToDictionary(recGC => recGC.ComponentId, recGC => (recGC.Component as IComponentModel, recGC.Count));
}
return _goodComponents;
}
}
public static Good Create(HardwareShopDatabase context, GoodBindingModel model)
{
return new Good()
{
Id = model.Id,
GoodName = model.GoodName,
Price = model.Price,
UserId = model.UserId,
Components = model.GoodComponents.Select(x => new GoodComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(GoodBindingModel model)
{
GoodName = model.GoodName;
Price = model.Price;
}
public GoodViewModel GetViewModel => new()
{
Id = Id,
GoodName = GoodName,
Price = Price,
UserId = UserId,
UserLogin = User.Login,
GoodComponents = GoodComponents
};
public void UpdateComponents(HardwareShopDatabase context, GoodBindingModel model)
{
var goodComponents = context.GoodComponents
.Where(rec => rec.GoodId == model.Id).ToList();
if (goodComponents != null && goodComponents.Count > 0)
{
context.GoodComponents
.RemoveRange(goodComponents
.Where(rec => !model.GoodComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in goodComponents)
{
updateComponent.Count = model.GoodComponents[updateComponent.ComponentId].Item2;
model.GoodComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var good = context.Goods.First(x => x.Id == Id);
foreach (var gc in model.GoodComponents)
{
context.GoodComponents.Add(new GoodComponent
{
Good = good,
Component = context.Components.First(x => x.Id == gc.Key),
Count = gc.Value.Item2
});
context.SaveChanges();
}
_goodComponents = null;
}
}
}

View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace HardwareShopDatabaseImplement.Models
{
public class GoodComponent
{
public int GoodId { get; set; }
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Good Good { get; set; } = new();
}
}

View File

@ -0,0 +1,146 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace HardwareShopDatabaseImplement.Models
{
public class Purchase : IPurchaseModel
{
public int Id { get; set; }
[Required]
public decimal Sum { get; set; }
[Required]
public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен;
public DateTime? DatePurchase { get; set; }
[Required]
public int UserId { get; set; }
public virtual User User { get; set; }
[ForeignKey("PurchaseId")]
public virtual List<PurchaseBuild>? Builds { get; set; }
public Dictionary<int, (IBuildModel, int)>? _purchaseBuilds = null;
[NotMapped]
public Dictionary<int, (IBuildModel, int)>? PurchaseBuilds
{
get
{
if (_purchaseBuilds == null)
{
_purchaseBuilds = Builds.ToDictionary(recPB => recPB.BuildId, recPB => (recPB.Build as IBuildModel, recPB.Count));
}
return _purchaseBuilds;
}
}
[ForeignKey("PurchaseId")]
public virtual List<PurchaseGood> Goods { get; set; } = new();
public Dictionary<int, (IGoodModel, int)>? _purchaseGoods = null;
[NotMapped]
public Dictionary<int, (IGoodModel, int)>? PurchaseGoods
{
get
{
if (_purchaseGoods == null)
{
_purchaseGoods = Goods.ToDictionary(recPG => recPG.GoodId, recPG => (recPG.Good as IGoodModel, recPG.Count));
}
return _purchaseGoods;
}
}
public static Purchase Create(HardwareShopDatabase context, PurchaseBindingModel model)
{
return new Purchase()
{
Id = model.Id,
Sum = model.Sum,
PurchaseStatus = model.PurchaseStatus,
DatePurchase = model.DatePurchase,
UserId = model.UserId,
Builds = model.PurchaseBuilds.Select(x => new PurchaseBuild
{
Build = context.Builds.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Goods = model.PurchaseGoods.Select(x => new PurchaseGood
{
Good = context.Goods.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(PurchaseBindingModel model)
{
PurchaseStatus = model.PurchaseStatus;
DatePurchase = model.DatePurchase;
}
public PurchaseViewModel GetViewModel => new()
{
Id = Id,
Sum = Sum,
PurchaseStatus = PurchaseStatus,
DatePurchase = DatePurchase,
UserId = UserId,
UserLogin = User.Login,
PurchaseBuilds = PurchaseBuilds,
PurchaseGoods = PurchaseGoods
};
public void UpdateBuilds(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseBuilds = context.PurchasesBuilds.Where(rec => rec.PurchaseId == model.Id).ToList();
if (purchaseBuilds != null && purchaseBuilds.Count > 0)
{
context.PurchasesBuilds.RemoveRange(purchaseBuilds.Where(rec => !model.PurchaseBuilds.ContainsKey(rec.BuildId)));
context.SaveChanges();
foreach (var updateBuild in purchaseBuilds)
{
updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2;
model.PurchaseBuilds.Remove(updateBuild.BuildId);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
foreach (var dc in model.PurchaseBuilds)
{
context.PurchasesBuilds.Add(new PurchaseBuild
{
Purchase = purchase,
Build = context.Builds.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_purchaseBuilds = null;
}
public void UpdateGoods(HardwareShopDatabase context, PurchaseBindingModel model)
{
var purchaseGoods = context.PurchasesGoods.Where(rec => rec.PurchaseId == model.Id).ToList();
if (purchaseGoods != null && purchaseGoods.Count > 0)
{ // удалили те в бд, которых нет в модели
context.PurchasesGoods.RemoveRange(purchaseGoods.Where(rec => !model.PurchaseGoods.ContainsKey(rec.GoodId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateGood in purchaseGoods)
{
updateGood.Count = model.PurchaseGoods[updateGood.GoodId].Item2;
model.PurchaseGoods.Remove(updateGood.GoodId);
}
context.SaveChanges();
}
var purchase = context.Purchases.First(x => x.Id == Id);
//добавляем в бд блюда которые есть в моделе, но ещё нет в бд
foreach (var dc in model.PurchaseGoods)
{
context.PurchasesGoods.Add(new PurchaseGood
{
Purchase = purchase,
Good = context.Goods.First(x => x.Id == dc.Key),
Count = dc.Value.Item2
});
context.SaveChanges();
}
_purchaseGoods = null;
}
}
}

View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace HardwareShopDatabaseImplement.Models
{
public class PurchaseBuild
{
public int BuildId { get; set; }
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,14 @@
using System.ComponentModel.DataAnnotations;
namespace HardwareShopDatabaseImplement.Models
{
public class PurchaseGood
{
public int PurchaseId { get; set; }
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,62 @@
using HardwareShopDataModels.Models;
using HardwareShopDataModels.Enums;
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace HardwareShopDatabaseImplement.Models
{
public class User : IUserModel
{
public int Id { get; set; }
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[Required]
[ForeignKey("ClientId")]
public virtual List<Build> Builds { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<Comment> Comments { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<Purchase> Purchases { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<Component> Components { get; set; } = new();
[ForeignKey("ClientId")]
public virtual List<Good> Goods { get; set; } = new();
public static User? Create(UserBindingModel? model)
{
if (model == null)
{
return null;
}
return new User()
{
Id = model.Id,
Login = model.Login,
Email = model.Email,
Password = model.Password,
};
}
public void Update(UserBindingModel? model)
{
if (model == null)
{
return;
}
Login = model.Login;
Password = model.Password;
Email = model.Email;
}
public UserViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
Email = Email,
Password = Password,
};
}
}