diff --git a/Storehouse/Storehouse.sln b/Storehouse/Storehouse.sln index 9b368a0..1be3913 100644 --- a/Storehouse/Storehouse.sln +++ b/Storehouse/Storehouse.sln @@ -3,15 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseView", "StorehouseView\StorehouseView.csproj", "{2A1C5191-CC20-4015-82E3-AEF00D33C09A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorehouseView", "StorehouseView\StorehouseView.csproj", "{2A1C5191-CC20-4015-82E3-AEF00D33C09A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseDataModels", "StorehouseDataModels\StorehouseDataModels.csproj", "{B1D926C1-F37C-4530-A3C0-02EAC8113B3E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorehouseDataModels", "StorehouseDataModels\StorehouseDataModels.csproj", "{B1D926C1-F37C-4530-A3C0-02EAC8113B3E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseContracts", "StorehouseContracts\StorehouseContracts.csproj", "{D5258E85-B904-4299-A07D-1F55B71DB0F8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorehouseContracts", "StorehouseContracts\StorehouseContracts.csproj", "{D5258E85-B904-4299-A07D-1F55B71DB0F8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseBusinessLogics", "StorehouseBusinessLogics\StorehouseBusinessLogics.csproj", "{3F171169-DA8D-4CE6-B100-BDD3A4FEDA77}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StorehouseBusinessLogics", "StorehouseBusinessLogics\StorehouseBusinessLogics.csproj", "{3F171169-DA8D-4CE6-B100-BDD3A4FEDA77}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseDatabase", "StorehouseDatabase\StorehouseDatabase.csproj", "{F2EA8A47-C7B6-4C2B-8AD0-0F3C0C24DF74}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StorehouseDatabaseImplement", "StorehouseDatabaseImplement\StorehouseDatabaseImplement.csproj", "{41FABDF9-049F-447C-B3D5-D9739D852E7C}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,10 +35,10 @@ Global {3F171169-DA8D-4CE6-B100-BDD3A4FEDA77}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F171169-DA8D-4CE6-B100-BDD3A4FEDA77}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F171169-DA8D-4CE6-B100-BDD3A4FEDA77}.Release|Any CPU.Build.0 = Release|Any CPU - {F2EA8A47-C7B6-4C2B-8AD0-0F3C0C24DF74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F2EA8A47-C7B6-4C2B-8AD0-0F3C0C24DF74}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F2EA8A47-C7B6-4C2B-8AD0-0F3C0C24DF74}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F2EA8A47-C7B6-4C2B-8AD0-0F3C0C24DF74}.Release|Any CPU.Build.0 = Release|Any CPU + {41FABDF9-049F-447C-B3D5-D9739D852E7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {41FABDF9-049F-447C-B3D5-D9739D852E7C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {41FABDF9-049F-447C-B3D5-D9739D852E7C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {41FABDF9-049F-447C-B3D5-D9739D852E7C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Storehouse/StorehouseDatabaseImplement/Implements/ClientStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..412e532 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,80 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.SearchModels; +using StorehouseContracts.StoragesContracts; +using StorehouseContracts.ViewModels; +using StorehouseDatabaseImplement.Models; + +namespace StorehouseDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + return context.Clients + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + 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/Storehouse/StorehouseDatabaseImplement/Implements/ProductStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..17f7fc3 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StorehouseDatabaseImplement.Implements +{ + internal class ProductStorage + { + } +} diff --git a/Storehouse/StorehouseDatabaseImplement/Implements/ProviderStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/ProviderStorage.cs new file mode 100644 index 0000000..5ecdb46 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Implements/ProviderStorage.cs @@ -0,0 +1,80 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.SearchModels; +using StorehouseContracts.StoragesContracts; +using StorehouseContracts.ViewModels; +using StorehouseDatabaseImplement.Models; + +namespace StorehouseDatabaseImplement.Implements +{ + public class ProviderStorage : IProviderStorage + { + public ProviderViewModel? GetElement(ProviderSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + return context.Providers + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + context.Providers.Add(newProvider); + context.SaveChanges(); + return newProvider.GetViewModel; + } + + public ProviderViewModel? Update(ProviderBindingModel model) + { + using var context = new StorehouseDatabase(); + 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 StorehouseDatabase(); + 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/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs new file mode 100644 index 0000000..e772839 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Implements/SaleStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StorehouseDatabaseImplement.Implements +{ + internal class SaleStorage + { + } +} diff --git a/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs b/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs new file mode 100644 index 0000000..0d0e781 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Implements/SupplyStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace StorehouseDatabaseImplement.Implements +{ + internal class SupplyStorage + { + } +} diff --git a/Storehouse/StorehouseDatabaseImplement/Models/Client.cs b/Storehouse/StorehouseDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..f169f51 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Models/Client.cs @@ -0,0 +1,56 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.StoragesContracts; +using StorehouseContracts.ViewModels; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace StorehouseDatabaseImplement.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/Storehouse/StorehouseDatabaseImplement/Models/Product.cs b/Storehouse/StorehouseDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..13b87bb --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Models/Product.cs @@ -0,0 +1,58 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.ViewModels; +using StorehouseDataModels.Enums; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace StorehouseDatabaseImplement.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/Storehouse/StorehouseDatabaseImplement/Models/Provider.cs b/Storehouse/StorehouseDatabaseImplement/Models/Provider.cs new file mode 100644 index 0000000..067b1ea --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Models/Provider.cs @@ -0,0 +1,55 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.ViewModels; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace StorehouseDatabaseImplement.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/Storehouse/StorehouseDatabaseImplement/Models/Sale.cs b/Storehouse/StorehouseDatabaseImplement/Models/Sale.cs new file mode 100644 index 0000000..1715d62 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Models/Sale.cs @@ -0,0 +1,57 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.ViewModels; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace StorehouseDatabaseImplement.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, + ProductId = ProductId, + ProductName = Product.ProductName, + SaleNum = SaleNum, + }; + } +} diff --git a/Storehouse/StorehouseDatabaseImplement/Models/Supply.cs b/Storehouse/StorehouseDatabaseImplement/Models/Supply.cs new file mode 100644 index 0000000..905829d --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/Models/Supply.cs @@ -0,0 +1,57 @@ +using StorehouseContracts.BindingModels; +using StorehouseContracts.ViewModels; +using StorehouseDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace StorehouseDatabaseImplement.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, + ProductId = ProductId, + ProductName = Product.ProductName, + SupplyNum = SupplyNum, + }; + } +} diff --git a/Storehouse/StorehouseDatabaseImplement/StorehouseDatabase.cs b/Storehouse/StorehouseDatabaseImplement/StorehouseDatabase.cs new file mode 100644 index 0000000..72ca94a --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/StorehouseDatabase.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using StorehouseDatabaseImplement.Models; + +namespace StorehouseDatabaseImplement +{ + public class StorehouseDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=StorehouseDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + 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/Storehouse/StorehouseDatabaseImplement/StorehouseDatabaseImplement.csproj b/Storehouse/StorehouseDatabaseImplement/StorehouseDatabaseImplement.csproj new file mode 100644 index 0000000..1048595 --- /dev/null +++ b/Storehouse/StorehouseDatabaseImplement/StorehouseDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + +