Исправления моделей + некоторая реализация хранилищ

This commit is contained in:
dasha 2023-04-01 20:03:03 +04:00
parent 64e2bbe928
commit c65cc794a6
20 changed files with 235 additions and 31 deletions

View File

@ -1,5 +1,4 @@

using System.ComponentModel;
using HardwareShopDataModels.Enums;
namespace HardwareShopContracts.SearchModels
{
@ -10,5 +9,9 @@ namespace HardwareShopContracts.SearchModels
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

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

View File

@ -12,7 +12,7 @@ namespace HardwareShopDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql("Host=192.168.0.101;Port=5432;Database=Computer_Hardware_Store;Username=user;Password=12345");
optionsBuilder.UseNpgsql("Host=127.0.0.1;Port=5433;Database=Computer_Hardware_Store;Username=user;Password=12345");
}
base.OnConfiguring(optionsBuilder);
}
@ -23,6 +23,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

@ -6,12 +6,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Implements\**" />
<EmbeddedResource Remove="Implements\**" />
<None Remove="Implements\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />

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,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HardwareShopDatabaseImplement.Implements.Storekeeper
{
public class OrderStorage
{
}
}

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
{