слияние

This commit is contained in:
Николай 2023-04-01 20:28:02 +04:00
commit 7eb1515a4d
22 changed files with 325 additions and 24 deletions

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HardwareShopDataModels\HardwareShopDataModels.csproj" />
</ItemGroup>

View File

@ -1,5 +1,4 @@

using System.ComponentModel;
using HardwareShopDataModels.Enums;
namespace HardwareShopContracts.SearchModels
{
@ -12,5 +11,9 @@ namespace HardwareShopContracts.SearchModels
public string? Email { get; set; } = string.Empty;
public int? UserId { get; set; }
public string? Password { get; set; } = string.Empty;
public UserRole? Role { get; set; } = UserRole.Неизвестен;
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,4 @@
using HardwareShopDataModels;
using HardwareShopDataModels.Enums;
using HardwareShopDataModels.Enums;
namespace HardwareShopDataModels.Models
{

View File

@ -19,6 +19,11 @@ namespace HardwareShopDatabaseImplement
modelBuilder.Entity<PurchaseBuild>().HasKey(x => new { x.PurchaseId, x.BuildId });
modelBuilder.Entity<PurchaseGood>().HasKey(x => new { x.PurchaseId, x.GoodId });
modelBuilder.Entity<GoodComponent>().HasKey(x => new { x.GoodId, x.ComponentId });
modelBuilder.Entity<User>(entity => {
entity.HasIndex(e => e.Login).IsUnique();
entity.HasIndex(e => e.Email).IsUnique();
});
}
public virtual DbSet<Build> Builds { set; get; }

View File

@ -0,0 +1,87 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements.Storekeeper
{
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,117 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements.Storekeeper
{
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,94 @@
using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.Storekeeper;
using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements.Storekeeper
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new HardwareShopDatabase();
var element = context.Orders
.Include(x => x.Good).Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HardwareShopDatabase();
return context.Orders
.Include(x => x.Good).Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new HardwareShopDatabase();
return context.Orders
.Include(x => x.Good).Include(x => x.User)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new HardwareShopDatabase();
return context.Orders
.Include(x => x.Good).Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new HardwareShopDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return context.Orders
.Include(x => x.Good).Include(x => x.User)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new HardwareShopDatabase();
var order = context.Orders
.Include(x => x.Good).Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}

View File

@ -1,12 +1,6 @@
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker;
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.ManyToMany
{

View File

@ -1,6 +1,5 @@
using HardwareShopDatabaseImplement.Models.Storekeeper;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HardwareShopDatabaseImplement.Models.ManyToMany
{

View File

@ -1,11 +1,5 @@
using HardwareShopDatabaseImplement.Models.Worker;
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.ManyToMany
{

View File

@ -1,12 +1,6 @@
using HardwareShopDatabaseImplement.Models.Storekeeper;
using HardwareShopDatabaseImplement.Models.Worker;
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.ManyToMany
{

View File

@ -21,7 +21,10 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
public int UserId { get; set; }
[ForeignKey("ComponentId")]
public virtual List<GoodComponent> GoodComponents { get; set; } = new();
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!;

View File

@ -30,6 +30,9 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper
[ForeignKey("GoodId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("GoodId")]
public virtual List<PurchaseGood> Purchases { get; set; } = new();
[NotMapped]
public Dictionary<int, (IComponentModel, int)> GoodComponents
{

View File

@ -13,6 +13,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.3" />