+DatabaseImplement

This commit is contained in:
Алексей Тихоненков 2024-05-19 02:59:13 +04:00
parent 7bbc1aa768
commit 6b99f2446f
13 changed files with 754 additions and 4 deletions

View File

@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34728.123
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageCompanyView", "StorageCompanyView\StorageCompanyView.csproj", "{8A570BF1-316C-4AF3-B2DE-BEED3132E98E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageCompanyView", "StorageCompanyView\StorageCompanyView.csproj", "{8A570BF1-316C-4AF3-B2DE-BEED3132E98E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageCompanyDataModels", "StorageCompanyDataModels\StorageCompanyDataModels.csproj", "{8A256C21-D0A8-4A6B-A670-BA359B8E8A88}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageCompanyDataModels", "StorageCompanyDataModels\StorageCompanyDataModels.csproj", "{8A256C21-D0A8-4A6B-A670-BA359B8E8A88}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageCompanyBusinessLogics", "StorageCompanyBusinessLogics\StorageCompanyBusinessLogics.csproj", "{AA209E1A-12E0-4401-942D-3A78B8A4AB79}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageCompanyBusinessLogics", "StorageCompanyBusinessLogics\StorageCompanyBusinessLogics.csproj", "{AA209E1A-12E0-4401-942D-3A78B8A4AB79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageCompanyContracts", "StorageCompanyContracts\StorageCompanyContracts.csproj", "{AC578537-AFF1-4816-94DB-D7029C439634}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorageCompanyContracts", "StorageCompanyContracts\StorageCompanyContracts.csproj", "{AC578537-AFF1-4816-94DB-D7029C439634}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorageCompanyDatabaseImplement", "StorageCompanyDatabaseImplement\StorageCompanyDatabaseImplement.csproj", "{8C68BD0B-294D-4F35-BF0B-6AD13EA82D6B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -33,6 +35,10 @@ Global
{AC578537-AFF1-4816-94DB-D7029C439634}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC578537-AFF1-4816-94DB-D7029C439634}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC578537-AFF1-4816-94DB-D7029C439634}.Release|Any CPU.Build.0 = Release|Any CPU
{8C68BD0B-294D-4F35-BF0B-6AD13EA82D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C68BD0B-294D-4F35-BF0B-6AD13EA82D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C68BD0B-294D-4F35-BF0B-6AD13EA82D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C68BD0B-294D-4F35-BF0B-6AD13EA82D6B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,80 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.SearchModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDatabaseImplement.Models;
namespace StorageCompanyDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new StorageCompanyDatabase();
return context.Clients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new StorageCompanyDatabase();
return context.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new StorageCompanyDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new StorageCompanyDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new StorageCompanyDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new StorageCompanyDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
context.Clients.Remove(client);
context.SaveChanges();
return client.GetViewModel;
}
}
}

View File

@ -0,0 +1,80 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.SearchModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDatabaseImplement.Models;
namespace StorageCompanyDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue)
{
return null;
}
using var context = new StorageCompanyDatabase();
return context.Products.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ProductName) && x.ProductName == model.ProductName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName))
{
return new();
}
using var context = new StorageCompanyDatabase();
return context.Products
.Where(x => x.ProductName.Contains(model.ProductName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProductViewModel> GetFullList()
{
using var context = new StorageCompanyDatabase();
return context.Products.Select(x => x.GetViewModel).ToList();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
var newProduct = Product.Create(model);
if (newProduct == null)
{
return null;
}
using var context = new StorageCompanyDatabase();
context.Products.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new StorageCompanyDatabase();
var product = context.Products.FirstOrDefault(x => x.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
return product.GetViewModel;
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new StorageCompanyDatabase();
var product = context.Products.FirstOrDefault(x => x.Id == model.Id);
if (product == null)
{
return null;
}
context.Products.Remove(product);
context.SaveChanges();
return product.GetViewModel;
}
}
}

View File

@ -0,0 +1,80 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.SearchModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDatabaseImplement.Models;
namespace StorageCompanyDatabaseImplement.Implements
{
public class ProviderStorage : IProviderStorage
{
public ProviderViewModel? GetElement(ProviderSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new StorageCompanyDatabase();
return context.Providers.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ProviderViewModel> GetFilteredList(ProviderSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new StorageCompanyDatabase();
return context.Providers
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProviderViewModel> GetFullList()
{
using var context = new StorageCompanyDatabase();
return context.Providers.Select(x => x.GetViewModel).ToList();
}
public ProviderViewModel? Insert(ProviderBindingModel model)
{
var newProvider = Provider.Create(model);
if (newProvider == null)
{
return null;
}
using var context = new StorageCompanyDatabase();
context.Providers.Add(newProvider);
context.SaveChanges();
return newProvider.GetViewModel;
}
public ProviderViewModel? Update(ProviderBindingModel model)
{
using var context = new StorageCompanyDatabase();
var provider = context.Providers.FirstOrDefault(x => x.Id == model.Id);
if (provider == null)
{
return null;
}
provider.Update(model);
context.SaveChanges();
return provider.GetViewModel;
}
public ProviderViewModel? Delete(ProviderBindingModel model)
{
using var context = new StorageCompanyDatabase();
var provider = context.Providers.FirstOrDefault(x => x.Id == model.Id);
if (provider == null)
{
return null;
}
context.Providers.Remove(provider);
context.SaveChanges();
return provider.GetViewModel;
}
}
}

View File

@ -0,0 +1,88 @@
using Microsoft.EntityFrameworkCore;
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.SearchModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDatabaseImplement.Models;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace StorageCompanyDatabaseImplement.Implements
{
public class SaleStorage : ISaleStorage
{
public SaleViewModel? GetElement(SaleSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new StorageCompanyDatabase();
return context.Sales
.Include(x => x.Client)
.Include(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SaleViewModel> GetFilteredList(SaleSearchModel model)
{
if (model.ClientId == null && model.ProductId == null)
{
return new();
}
using var context = new StorageCompanyDatabase();
return context.Sales.Include(x => x.Product).Include(x => x.Client).Select(x => x.GetViewModel).ToList();
}
public List<SaleViewModel> GetFullList()
{
using var context = new StorageCompanyDatabase();
return context.Sales.Include(x => x.Product).Include(x => x.Client).Select(x => x.GetViewModel).ToList();
}
public SaleViewModel? Insert(SaleBindingModel model)
{
using var context = new StorageCompanyDatabase();
var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId);
if (product == null || model.SaleNum > product.ProductNum)
{
return null;
}
var newSale = Sale.Create(model);
if (newSale == null)
{
return null;
}
product.ProductNum -= model.SaleNum;
context.Sales.Add(newSale);
context.SaveChanges();
return context.Sales.Include(x => x.Product).Include(x => x.Client).FirstOrDefault(x => x.Id == newSale.Id)?.GetViewModel;
}
public SaleViewModel? Update(SaleBindingModel model)
{
using var context = new StorageCompanyDatabase();
var sale = context.Sales.FirstOrDefault(x => x.Id == model.Id);
if (sale == null)
{
return null;
}
sale.Update(model);
context.SaveChanges();
return context.Sales.Include(x => x.Product).Include(x => x.Client).FirstOrDefault(x => x.Id == sale.Id)?.GetViewModel;
}
public SaleViewModel? Delete(SaleBindingModel model)
{
using var context = new StorageCompanyDatabase();
var sale = context.Sales.FirstOrDefault(x => x.Id == model.Id);
if (sale == null)
{
return null;
}
context.Sales.Remove(sale);
context.SaveChanges();
return sale.GetViewModel;
}
}
}

View File

@ -0,0 +1,86 @@
using Microsoft.EntityFrameworkCore;
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.SearchModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDatabaseImplement.Models;
namespace StorageCompanyDatabaseImplement.Implements
{
public class SupplyStorage : ISupplyStorage
{
public SupplyViewModel? GetElement(SupplySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new StorageCompanyDatabase();
return context.Supplys
.Include(x => x.Provider)
.Include(x => x.Product)
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<SupplyViewModel> GetFilteredList(SupplySearchModel model)
{
if (model.ProviderId == null && model.ProductId == null)
{
return new();
}
using var context = new StorageCompanyDatabase();
return context.Supplys.Include(x => x.Product).Include(x => x.Provider).Select(x => x.GetViewModel).ToList();
}
public List<SupplyViewModel> GetFullList()
{
using var context = new StorageCompanyDatabase();
return context.Supplys.Include(x => x.Product).Include(x => x.Provider).Select(x => x.GetViewModel).ToList();
}
public SupplyViewModel? Insert(SupplyBindingModel model)
{
using var context = new StorageCompanyDatabase();
var product = context.Products.FirstOrDefault(p => p.Id == model.ProductId);
if (product == null)
{
return null;
}
var newSupply = Supply.Create(model);
if (newSupply == null)
{
return null;
}
product.ProductNum += model.SupplyNum;
context.Supplys.Add(newSupply);
context.SaveChanges();
return context.Supplys.Include(x => x.Product).Include(x => x.Provider).FirstOrDefault(x => x.Id == newSupply.Id)?.GetViewModel;
}
public SupplyViewModel? Update(SupplyBindingModel model)
{
using var context = new StorageCompanyDatabase();
var supply = context.Supplys.FirstOrDefault(x => x.Id == model.Id);
if (supply == null)
{
return null;
}
supply.Update(model);
context.SaveChanges();
return context.Supplys.Include(x => x.Product).Include(x => x.Provider).FirstOrDefault(x => x.Id == supply.Id)?.GetViewModel;
}
public SupplyViewModel? Delete(SupplyBindingModel model)
{
using var context = new StorageCompanyDatabase();
var supply = context.Supplys.FirstOrDefault(x => x.Id == model.Id);
if (supply == null)
{
return null;
}
context.Supplys.Remove(supply);
context.SaveChanges();
return supply.GetViewModel;
}
}
}

View File

@ -0,0 +1,56 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.StoragesContracts;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace StorageCompanyDatabaseImplement.Models
{
public class Client : IClientModel
{
public int Id { get; set; }
[Required]
public string? ClientFIO { get; set; } = string.Empty;
[Required]
public string? Email { get; set; } = string.Empty;
[Required]
public string? Password { get; set; } = string.Empty;
[ForeignKey("ClientId")]
public virtual List<Sale> Sales { get; set; } = new();
public static Client? Create(ClientBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
ClientFIO = model.ClientFIO,
Email = model.Email,
Password = model.Password
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
Email = model.Email;
Password = model.Password;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password,
};
}
}

View File

@ -0,0 +1,58 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDataModels.Enums;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace StorageCompanyDatabaseImplement.Models
{
public class Product : IProductModel
{
public int Id { get; set; }
[Required]
public string? ProductName { get; set; } = string.Empty;
[Required]
public int ProductNum { get; set; }
[Required]
public ProductType ProductType { get; set; } = ProductType.Неизвестен;
[ForeignKey("ProductId")]
public virtual List<Sale> Sales { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<Supply> Supplys { get; set; } = new();
public static Product? Create(ProductBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
ProductName = model.ProductName,
ProductNum = model.ProductNum,
ProductType = model.ProductType,
};
}
public void Update(ProductBindingModel model)
{
if (model == null)
{
return;
}
ProductName = model.ProductName;
ProductNum = model.ProductNum;
ProductType = model.ProductType;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
ProductName = ProductName,
ProductNum = ProductNum,
ProductType = ProductType,
};
}
}

View File

@ -0,0 +1,55 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace StorageCompanyDatabaseImplement.Models
{
public class Provider : IProviderModel
{
public int Id { get; set; }
[Required]
public string? ProviderFIO { get; set; } = string.Empty;
[Required]
public string? Email { get; set; } = string.Empty;
[Required]
public string? Password { get; set; } = string.Empty;
[ForeignKey("ProviderId")]
public virtual List<Supply> Supplys { get; set; } = new();
public static Provider? Create(ProviderBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
ProviderFIO = model.ProviderFIO,
Email = model.Email,
Password = model.Password
};
}
public void Update(ProviderBindingModel model)
{
if (model == null)
{
return;
}
ProviderFIO = model.ProviderFIO;
Email = model.Email;
Password = model.Password;
}
public ProviderViewModel GetViewModel => new()
{
Id = Id,
ProviderFIO = ProviderFIO,
Email = Email,
Password = Password,
};
}
}

View File

@ -0,0 +1,57 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace StorageCompanyDatabaseImplement.Models
{
public class Sale : ISaleModel
{
public int Id { get; set; }
[Required]
public int ClientId { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int SaleNum { get; set; }
public virtual Client Client { get; set; }
public virtual Product Product { get; set; }
public static Sale? Create(SaleBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
ClientId = model.ClientId,
ProductId = model.ProductId,
SaleNum = model.SaleNum,
};
}
public void Update(SaleBindingModel model)
{
if (model == null)
{
return;
}
ClientId = model.ClientId;
ProductId = model.ProductId;
SaleNum = model.SaleNum;
}
public SaleViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
ClientFIO = Client.ClientFIO ?? string.Empty,
ProductId = ProductId,
ProductName = Product.ProductName ?? string.Empty,
SaleNum = SaleNum,
};
}
}

View File

@ -0,0 +1,57 @@
using StorageCompanyContracts.BindingModels;
using StorageCompanyContracts.ViewModels;
using StorageCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace StorageCompanyDatabaseImplement.Models
{
public class Supply : ISupplyModel
{
public int Id { get; set; }
[Required]
public int ProviderId { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int SupplyNum { get; set; }
public virtual Provider Provider { get; set; }
public virtual Product Product { get; set; }
public static Supply? Create(SupplyBindingModel model)
{
if (model == null)
{
return null;
}
return new()
{
Id = model.Id,
ProviderId = model.ProviderId,
ProductId = model.ProductId,
SupplyNum = model.SupplyNum,
};
}
public void Update(SupplyBindingModel model)
{
if (model == null)
{
return;
}
ProviderId = model.ProviderId;
ProductId = model.ProductId;
SupplyNum = model.SupplyNum;
}
public SupplyViewModel GetViewModel => new()
{
Id = Id,
ProviderId = ProviderId,
ProviderFIO = Provider.ProviderFIO ?? string.Empty,
ProductId = ProductId,
ProductName = Product.ProductName ?? string.Empty,
SupplyNum = SupplyNum,
};
}
}

View File

@ -0,0 +1,24 @@
using Microsoft.EntityFrameworkCore;
using StorageCompanyDatabaseImplement.Models;
namespace StorageCompanyDatabaseImplement
{
public class StorageCompanyDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=StorageDatabase;Username=postgres;Password=postgres");
}
base.OnConfiguring(optionsBuilder);
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
}
public virtual DbSet<Client> Clients { set; get; }
public virtual DbSet<Product> Products { set; get; }
public virtual DbSet<Provider> Providers { set; get; }
public virtual DbSet<Sale> Sales { set; get; }
public virtual DbSet<Supply> Supplys { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<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.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StorageCompanyContracts\StorageCompanyContracts.csproj" />
<ProjectReference Include="..\StorageCompanyDataModels\StorageCompanyDataModels.csproj" />
</ItemGroup>
</Project>