From 6b99f2446f928ab2f3f516b39545f0e7efce000f Mon Sep 17 00:00:00 2001 From: Yourdax Date: Sun, 19 May 2024 02:59:13 +0400 Subject: [PATCH] +DatabaseImplement --- StorageCompany/StorageCompany.sln | 14 ++- .../Implements/ClientStorage.cs | 80 +++++++++++++++++ .../Implements/ProductStorage.cs | 80 +++++++++++++++++ .../Implements/ProviderStorage.cs | 80 +++++++++++++++++ .../Implements/SaleStorage.cs | 88 +++++++++++++++++++ .../Implements/SupplyStorage.cs | 86 ++++++++++++++++++ .../Models/Client.cs | 56 ++++++++++++ .../Models/Product.cs | 58 ++++++++++++ .../Models/Provider.cs | 55 ++++++++++++ .../Models/Sale.cs | 57 ++++++++++++ .../Models/Supply.cs | 57 ++++++++++++ .../StorageCompanyDatabase.cs | 24 +++++ .../StorageCompanyDatabaseImplement.csproj | 23 +++++ 13 files changed, 754 insertions(+), 4 deletions(-) create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Implements/ClientStorage.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Implements/ProductStorage.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Implements/ProviderStorage.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Implements/SaleStorage.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Implements/SupplyStorage.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Models/Client.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Models/Product.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Models/Provider.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Models/Sale.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/Models/Supply.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabase.cs create mode 100644 StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabaseImplement.csproj diff --git a/StorageCompany/StorageCompany.sln b/StorageCompany/StorageCompany.sln index 727f8c1..64fe53a 100644 --- a/StorageCompany/StorageCompany.sln +++ b/StorageCompany/StorageCompany.sln @@ -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 diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Implements/ClientStorage.cs b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..585490c --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ClientStorage.cs @@ -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 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 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; + } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProductStorage.cs b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..71f3c60 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProductStorage.cs @@ -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 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 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; + } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProviderStorage.cs b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProviderStorage.cs new file mode 100644 index 0000000..42f34dc --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Implements/ProviderStorage.cs @@ -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 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 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; + } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Implements/SaleStorage.cs b/StorageCompany/StorageCompanyDatabaseImplement/Implements/SaleStorage.cs new file mode 100644 index 0000000..c25e384 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Implements/SaleStorage.cs @@ -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 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 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; + } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Implements/SupplyStorage.cs b/StorageCompany/StorageCompanyDatabaseImplement/Implements/SupplyStorage.cs new file mode 100644 index 0000000..e37c1d0 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Implements/SupplyStorage.cs @@ -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 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 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; + } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Models/Client.cs b/StorageCompany/StorageCompanyDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..340e1a5 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Models/Client.cs @@ -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 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, + }; + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Models/Product.cs b/StorageCompany/StorageCompanyDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..b37ea82 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Models/Product.cs @@ -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 Sales { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List 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, + }; + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Models/Provider.cs b/StorageCompany/StorageCompanyDatabaseImplement/Models/Provider.cs new file mode 100644 index 0000000..590ddca --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Models/Provider.cs @@ -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 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, + }; + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Models/Sale.cs b/StorageCompany/StorageCompanyDatabaseImplement/Models/Sale.cs new file mode 100644 index 0000000..78ffa57 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Models/Sale.cs @@ -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, + }; + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/Models/Supply.cs b/StorageCompany/StorageCompanyDatabaseImplement/Models/Supply.cs new file mode 100644 index 0000000..cc87fe7 --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/Models/Supply.cs @@ -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, + }; + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabase.cs b/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabase.cs new file mode 100644 index 0000000..ace3e6d --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabase.cs @@ -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 Clients { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet Providers { set; get; } + public virtual DbSet Sales { set; get; } + public virtual DbSet Supplys { set; get; } + } +} diff --git a/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabaseImplement.csproj b/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabaseImplement.csproj new file mode 100644 index 0000000..8dc4b6e --- /dev/null +++ b/StorageCompany/StorageCompanyDatabaseImplement/StorageCompanyDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + +