diff --git a/ComputerShop.sln b/ComputerShop.sln new file mode 100644 index 0000000..79b7668 --- /dev/null +++ b/ComputerShop.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34511.84 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopDataModels", "ComputerShopDataModels\ComputerShopDataModels.csproj", "{BBCF398B-D800-4D8D-99E8-F7ED2CC14A65}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopContracts", "ComputerShopContracts\ComputerShopContracts.csproj", "{B197888D-702B-4122-BFBF-BA6BCB49C0F3}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopBusinessLogic", "ComputerShopBusinessLogic\ComputerShopBusinessLogic.csproj", "{82FCBA71-AC54-45C8-9B21-BCB3DF6E085B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopDatabaseImplement", "ComputerShopDatabaseImplement\ComputerShopDatabaseImplement.csproj", "{CDE4C963-67B5-47A6-A394-901E95EA40F7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerShopImplementerApp", "ComputerShopImplementerApp\ComputerShopImplementerApp.csproj", "{0E13C365-F2CC-4A32-BDD0-52E2DB324734}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BBCF398B-D800-4D8D-99E8-F7ED2CC14A65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BBCF398B-D800-4D8D-99E8-F7ED2CC14A65}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BBCF398B-D800-4D8D-99E8-F7ED2CC14A65}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BBCF398B-D800-4D8D-99E8-F7ED2CC14A65}.Release|Any CPU.Build.0 = Release|Any CPU + {B197888D-702B-4122-BFBF-BA6BCB49C0F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B197888D-702B-4122-BFBF-BA6BCB49C0F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B197888D-702B-4122-BFBF-BA6BCB49C0F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B197888D-702B-4122-BFBF-BA6BCB49C0F3}.Release|Any CPU.Build.0 = Release|Any CPU + {82FCBA71-AC54-45C8-9B21-BCB3DF6E085B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {82FCBA71-AC54-45C8-9B21-BCB3DF6E085B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {82FCBA71-AC54-45C8-9B21-BCB3DF6E085B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {82FCBA71-AC54-45C8-9B21-BCB3DF6E085B}.Release|Any CPU.Build.0 = Release|Any CPU + {CDE4C963-67B5-47A6-A394-901E95EA40F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDE4C963-67B5-47A6-A394-901E95EA40F7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDE4C963-67B5-47A6-A394-901E95EA40F7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDE4C963-67B5-47A6-A394-901E95EA40F7}.Release|Any CPU.Build.0 = Release|Any CPU + {0E13C365-F2CC-4A32-BDD0-52E2DB324734}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E13C365-F2CC-4A32-BDD0-52E2DB324734}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E13C365-F2CC-4A32-BDD0-52E2DB324734}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E13C365-F2CC-4A32-BDD0-52E2DB324734}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9414A502-1CCD-466A-9FFA-21B0297A8ECA} + EndGlobalSection +EndGlobal diff --git a/ComputerShopBusinessLogic/BusinessLogics/Batch.cs b/ComputerShopBusinessLogic/BusinessLogics/Batch.cs new file mode 100644 index 0000000..3e6a275 --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/Batch.cs @@ -0,0 +1,99 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class Batch : IBatchLogic + { + private readonly ILogger _logger; + private readonly IBatchStorage _batchStorage; + + public Batch(ILogger logger, IBatchStorage batchStorage) + { + _logger = logger; + _batchStorage = batchStorage; + } + + public List ReadList(BatchSearchModel? model) + { + var list = (model == null) ? _batchStorage.GetFullList() : _batchStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("Read null list"); + return null; + } + return list; + } + + public BatchViewModel? ReadElement(BatchSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var batch = _batchStorage.GetElement(model); + if (batch == null) + { + _logger.LogWarning("ReadElement batch not found"); + return null; + } + return batch; + } + + public bool Create(BatchBindingModel model) + { + CheckModel(model); + if (_batchStorage.Insert(model) == null) + { + _logger.LogWarning("Insert failed"); + return false; + } + return true; + } + + public bool Update(BatchBindingModel model) + { + CheckModel(model); + if (_batchStorage.Update(model) == null) + { + _logger.LogWarning("Update failed"); + return false; + } + return true; + } + + public bool Delete(BatchBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_batchStorage.Delete(model) == null) + { + _logger.LogWarning("Delete failed"); + return false; + } + return true; + } + + private void CheckModel(BatchBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + } + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs new file mode 100644 index 0000000..c990dbf --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/RequestLogic.cs @@ -0,0 +1,94 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class RequestLogic : IRequestLogic + { + private readonly ILogger _logger; + private readonly IRequestStorage _requestStorage; + public RequestLogic(ILogger logger, IRequestStorage requestStorage) + { + _logger = logger; + _requestStorage = requestStorage; + } + + public List? ReadList(RequestSearchModel? model) + { + var list = (model == null ) ? _requestStorage.GetFullList() : _requestStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("Null list"); + return null; + } + return list; + } + + public RequestViewModel? ReadElement(RequestSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + var element = _requestStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement request not found"); + return null; + } + return element; + } + + public bool Create(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Insert(model) == null) + { + _logger.LogWarning("Insert failed"); + return false; + } + return true; + } + + public bool Update(RequestBindingModel model) + { + CheckModel(model); + if (_requestStorage.Update(model) == null) + { + _logger.LogWarning("Update failed"); + return false; + } + return true; + } + + public bool ConnectRequestAssembly(int requestId, int assemblyId) + { + _logger.LogInformation("Connect Assembly {rId} with request {aId}", requestId, assemblyId); + return _requestStorage.ConnectRequestAssembly(requestId, assemblyId); + } + + public bool Delete(RequestBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_requestStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + } +} diff --git a/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs b/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs new file mode 100644 index 0000000..8633abc --- /dev/null +++ b/ComputerShopBusinessLogic/BusinessLogics/UserLogic.cs @@ -0,0 +1,127 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopBusinessLogic.BusinessLogics +{ + public class UserLogic : IUserLogic + { + private readonly ILogger _logger; + private readonly IUserStorage _userStorage; + + public UserLogic(ILogger logger, IUserStorage userStorage) + { + _logger = logger; + _userStorage = userStorage; + } + public List? ReadList(UserSearchModel? model) + { + var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("Null list user"); + return null; + } + return list; + } + + public UserViewModel? ReadElement(UserSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _userStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement user not found"); + return null; + } + return element; + } + + public bool Create(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Insert(model) == null) + { + _logger.LogWarning("Insert failed"); + return false; + } + return true; + } + + public bool Update(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Update(model) == null) + { + _logger.LogWarning("Update failed"); + return false; + } + return true; + } + + public bool Delete(UserBindingModel model) + { + CheckModel(model, false); + if (_userStorage.Delete(model) == null) + { + _logger.LogWarning("Delete failed"); + return false; + } + return true; + } + + private void CheckModel(UserBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Логин не существует", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Пароль не существует", nameof(model.Password)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Почта не существует", nameof(model.Email)); + } + + var user1 = _userStorage.GetElement(new UserSearchModel + { + Login = model.Login + }); + + if (user1 != null && user1.Id != model.Id) + { + throw new InvalidOperationException("Логин занят"); + } + + var user2 = _userStorage.GetElement(new UserSearchModel + { + Email = model.Email + }); + + if (user2 != null && user2.Id != model.Id) { + throw new InvalidOperationException("Почта занята"); + } + } + } +} diff --git a/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj b/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj new file mode 100644 index 0000000..4a07bc8 --- /dev/null +++ b/ComputerShopBusinessLogic/ComputerShopBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs new file mode 100644 index 0000000..ff1cbac --- /dev/null +++ b/ComputerShopContracts/BindingModels/AssemblyBindingModel.cs @@ -0,0 +1,16 @@ +using ComputerShopDataModels.Models; + +namespace ComputerShopContracts.BindingModels +{ + public class AssemblyBindingModel : IAssemblyModel + { + public string AssemblyName { get; set; } = string.Empty; + + public double Cost { get; set; } + + public int UserId { get; set; } + public int Id { get; set; } + + public Dictionary AssemblyComponents { get; set; } = new(); + } +} diff --git a/ComputerShopContracts/BindingModels/BatchBindingModel.cs b/ComputerShopContracts/BindingModels/BatchBindingModel.cs new file mode 100644 index 0000000..ad03550 --- /dev/null +++ b/ComputerShopContracts/BindingModels/BatchBindingModel.cs @@ -0,0 +1,18 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class BatchBindingModel : IBatchModel + { + public DateTime DateBatch { get; set; } = DateTime.Now; + + public int ProductId { get; set; } + public int Id { get; set; } + + } +} diff --git a/ComputerShopContracts/BindingModels/ComponentBindingModel.cs b/ComputerShopContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..b9c2a29 --- /dev/null +++ b/ComputerShopContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,17 @@ +using ComputerShopDataModels.Models; + +namespace ComputerShopContracts.BindingModels +{ + public class ComponentBindingModel : IComponentModel + { + public double Cost { get; set; } + public string ComponentName { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public int UserId { get; set; } + public int Id { get; set; } + + + + + } +} diff --git a/ComputerShopContracts/BindingModels/ProductBindingModel.cs b/ComputerShopContracts/BindingModels/ProductBindingModel.cs new file mode 100644 index 0000000..d8bf59b --- /dev/null +++ b/ComputerShopContracts/BindingModels/ProductBindingModel.cs @@ -0,0 +1,14 @@ +using ComputerShopDataModels.Models; + +namespace ComputerShopContracts.BindingModels +{ + public class ProductBindingModel : IProductModel + { + public double Cost { get; set; } + public string ProductName { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public int UserId { get; set; } + public int? BatchId { get; set; } + public int Id { get; set; } + } +} diff --git a/ComputerShopContracts/BindingModels/RequestBindingModel.cs b/ComputerShopContracts/BindingModels/RequestBindingModel.cs new file mode 100644 index 0000000..60ea9cb --- /dev/null +++ b/ComputerShopContracts/BindingModels/RequestBindingModel.cs @@ -0,0 +1,16 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class RequestBindingModel : IRequestModel + { + public int Id { get; set; } + public DateTime DateRequest { get; set; } = DateTime.Now; + public int? AssemblyId { get; set; } + } +} diff --git a/ComputerShopContracts/BindingModels/UserBindingModel.cs b/ComputerShopContracts/BindingModels/UserBindingModel.cs new file mode 100644 index 0000000..433b209 --- /dev/null +++ b/ComputerShopContracts/BindingModels/UserBindingModel.cs @@ -0,0 +1,19 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BindingModels +{ + public class UserBindingModel : IUserModel + { + public string Fio { get; set; } = string.Empty; + public string PhoneNumber { get; set; } = string.Empty; + public string Login { get; set; } = string.Empty; // TODO сделать адекватно + public string Password { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public int Id { get; set; } + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs new file mode 100644 index 0000000..3ac85ca --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs @@ -0,0 +1,19 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IAssemblyLogic + { + List? ReadList(AssemblySearchModel? Model); + + AssemblyViewModel? ReadElement(AssemblySearchModel Model); + + bool Create(AssemblyBindingModel Model); + + bool Update(AssemblyBindingModel Model); + + bool Delete(AssemblyBindingModel Model); + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IBatchLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IBatchLogic.cs new file mode 100644 index 0000000..6b17a1d --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IBatchLogic.cs @@ -0,0 +1,20 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IBatchLogic + { + List? ReadList(BatchSearchModel? model); + BatchViewModel? ReadElement(BatchSearchModel model); + bool Create(BatchBindingModel model); + bool Update(BatchBindingModel model); + bool Delete(BatchBindingModel model); + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IComponentLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IComponentLogic.cs new file mode 100644 index 0000000..0c50bb1 --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IComponentLogic.cs @@ -0,0 +1,19 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IComponentLogic + { + List? ReadList(ComponentSearchModel? Model); + + ComponentViewModel? ReadElement(ComponentSearchModel Model); + + bool Create(ComponentBindingModel Model); + + bool Update(ComponentBindingModel Model); + + bool Delete(ComponentBindingModel Model); + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IProductLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IProductLogic.cs new file mode 100644 index 0000000..005a7fa --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IProductLogic.cs @@ -0,0 +1,19 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IProductLogic + { + List? ReadList(ProductSearchModel? Model); + + ProductViewModel? ReadElement(ProductSearchModel Model); + + bool Create(ProductBindingModel Model); + + bool Update(ProductBindingModel Model); + + bool Delete(ProductBindingModel Model); + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs new file mode 100644 index 0000000..6ecc316 --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IRequestLogic.cs @@ -0,0 +1,21 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IRequestLogic + { + List? ReadList(RequestSearchModel? model); + RequestViewModel? ReadElement(RequestSearchModel model); + bool Create(RequestBindingModel model); + bool Update(RequestBindingModel model); + bool Delete(RequestBindingModel model); + bool ConnectRequestAssembly(int requestId, int assemblyId); + } +} diff --git a/ComputerShopContracts/BusinessLogicContracts/IUserLogic.cs b/ComputerShopContracts/BusinessLogicContracts/IUserLogic.cs new file mode 100644 index 0000000..0a6ce48 --- /dev/null +++ b/ComputerShopContracts/BusinessLogicContracts/IUserLogic.cs @@ -0,0 +1,20 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.BusinessLogicContracts +{ + public interface IUserLogic + { + List? ReadList(UserSearchModel? model); + UserViewModel? ReadElement(UserSearchModel model); + bool Create(UserBindingModel model); + bool Update(UserBindingModel model); + bool Delete(UserBindingModel model); + } +} diff --git a/ComputerShopContracts/ComputerShopContracts.csproj b/ComputerShopContracts/ComputerShopContracts.csproj new file mode 100644 index 0000000..ac0c524 --- /dev/null +++ b/ComputerShopContracts/ComputerShopContracts.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/ComputerShopContracts/SearchModels/AssemblySearchModel.cs b/ComputerShopContracts/SearchModels/AssemblySearchModel.cs new file mode 100644 index 0000000..e73de98 --- /dev/null +++ b/ComputerShopContracts/SearchModels/AssemblySearchModel.cs @@ -0,0 +1,10 @@ +namespace ComputerShopContracts.SearchModels +{ + public class AssemblySearchModel + { + public int? Id { get; set; } + + public int? UserId { get; set; } + + } +} diff --git a/ComputerShopContracts/SearchModels/BatchSearchModel.cs b/ComputerShopContracts/SearchModels/BatchSearchModel.cs new file mode 100644 index 0000000..ea871ef --- /dev/null +++ b/ComputerShopContracts/SearchModels/BatchSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.SearchModels +{ + public class BatchSearchModel + { + public int? Id { get; set; } + public int ProductId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + } +} diff --git a/ComputerShopContracts/SearchModels/ComponentSearchModel.cs b/ComputerShopContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..a9ed4ba --- /dev/null +++ b/ComputerShopContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,9 @@ +namespace ComputerShopContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id { get; set; } + public int? UserId { get; set; } + public string? ComponentName { get; set; } + } +} diff --git a/ComputerShopContracts/SearchModels/ProductSearchModel.cs b/ComputerShopContracts/SearchModels/ProductSearchModel.cs new file mode 100644 index 0000000..3b3e375 --- /dev/null +++ b/ComputerShopContracts/SearchModels/ProductSearchModel.cs @@ -0,0 +1,11 @@ +namespace ComputerShopContracts.SearchModels +{ + public class ProductSearchModel + { + public int? Id { get; set; } + + public int? UserId { get; set; } + + public int? BatchId { get; set; } + } +} diff --git a/ComputerShopContracts/SearchModels/RequestSearchModel.cs b/ComputerShopContracts/SearchModels/RequestSearchModel.cs new file mode 100644 index 0000000..1296c3e --- /dev/null +++ b/ComputerShopContracts/SearchModels/RequestSearchModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.SearchModels +{ + public class RequestSearchModel + { + public int? Id { get; set; } + public int? AssemblyId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + + } +} diff --git a/ComputerShopContracts/SearchModels/UserSearchModel.cs b/ComputerShopContracts/SearchModels/UserSearchModel.cs new file mode 100644 index 0000000..7565551 --- /dev/null +++ b/ComputerShopContracts/SearchModels/UserSearchModel.cs @@ -0,0 +1,19 @@ +using ComputerShopDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.SearchModels +{ + public class UserSearchModel + { + public string? Fio { get; set; } + public string? PhoneNumber { get; set; } + public int? Id { get; set; } + public string? Login { get; set; } + public string Password { get; set; } + public string? Email { get; set; } + } +} diff --git a/ComputerShopContracts/StorageContracts/IAssemblyStorage.cs b/ComputerShopContracts/StorageContracts/IAssemblyStorage.cs new file mode 100644 index 0000000..4fde22e --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IAssemblyStorage.cs @@ -0,0 +1,21 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IAssemblyStorage + { + List GetFullList(); + + List GetFilteredList(AssemblySearchModel Model); + + AssemblyViewModel? GetElement(AssemblySearchModel Model); + + AssemblyViewModel? Insert(AssemblyBindingModel Model); + + AssemblyViewModel? Update(AssemblyBindingModel Model); + + AssemblyViewModel? Delete(AssemblyBindingModel Model); + } +} diff --git a/ComputerShopContracts/StorageContracts/IBatchStorage.cs b/ComputerShopContracts/StorageContracts/IBatchStorage.cs new file mode 100644 index 0000000..e5e2bd7 --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IBatchStorage.cs @@ -0,0 +1,20 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IBatchStorage + { + List GetFullList(); + BatchViewModel? GetElement(BatchSearchModel model); + BatchViewModel? Insert(BatchBindingModel model); + BatchViewModel? Update(BatchBindingModel model); + BatchViewModel? Delete(BatchBindingModel model); + } +} diff --git a/ComputerShopContracts/StorageContracts/IComponentStorage.cs b/ComputerShopContracts/StorageContracts/IComponentStorage.cs new file mode 100644 index 0000000..e0b7ed6 --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IComponentStorage.cs @@ -0,0 +1,21 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IComponentStorage + { + List GetFullList(); + + List GetFilteredList(ComponentSearchModel Model); + + ComponentViewModel? GetElement(ComponentSearchModel Model); + + ComponentViewModel? Insert(ComponentBindingModel Model); + + ComponentViewModel? Update(ComponentBindingModel Model); + + ComponentViewModel? Delete(ComponentBindingModel Model); + } +} diff --git a/ComputerShopContracts/StorageContracts/IProductStorage.cs b/ComputerShopContracts/StorageContracts/IProductStorage.cs new file mode 100644 index 0000000..ca151af --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IProductStorage.cs @@ -0,0 +1,21 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IProductStorage + { + List GetFullList(); + + List GetFilteredList(ProductSearchModel Model); + + ProductViewModel? GetElement(ProductSearchModel Model); + + ProductViewModel? Insert(ProductBindingModel Model); + + ProductViewModel? Update(ProductBindingModel Model); + + ProductViewModel? Delete(ProductBindingModel Model); + } +} diff --git a/ComputerShopContracts/StorageContracts/IRequestStorage.cs b/ComputerShopContracts/StorageContracts/IRequestStorage.cs new file mode 100644 index 0000000..70f765a --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IRequestStorage.cs @@ -0,0 +1,21 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IRequestStorage + { + List GetFullList(); + RequestViewModel? GetElement(RequestSearchModel model); + RequestViewModel? Insert(RequestBindingModel model); + RequestViewModel? Update(RequestBindingModel model); + RequestViewModel? Delete(RequestBindingModel model); + bool ConnectRequestAssembly(int requestId, int assemblyId); + } +} diff --git a/ComputerShopContracts/StorageContracts/IUserStorage.cs b/ComputerShopContracts/StorageContracts/IUserStorage.cs new file mode 100644 index 0000000..8dbc921 --- /dev/null +++ b/ComputerShopContracts/StorageContracts/IUserStorage.cs @@ -0,0 +1,20 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.StorageContracts +{ + public interface IUserStorage + { + List GetFullList(); + UserViewModel? GetElement(UserSearchModel model); + UserViewModel? Insert(UserBindingModel model); + UserViewModel? Update(UserBindingModel model); + UserViewModel? Delete(UserBindingModel model); + } +} diff --git a/ComputerShopContracts/ViewModels/AssemblyViewModel.cs b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs new file mode 100644 index 0000000..91ef296 --- /dev/null +++ b/ComputerShopContracts/ViewModels/AssemblyViewModel.cs @@ -0,0 +1,18 @@ +using ComputerShopDataModels.Models; +using System.ComponentModel; + +namespace ComputerShopContracts.ViewModels +{ + public class AssemblyViewModel : IAssemblyModel + { + public int Id { get; set; } + + + [DisplayName("Имя сборки")] + public string AssemblyName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Cost { get; set; } + public int UserId { get; set; } + public Dictionary AssemblyComponents { get; set; } = new(); + } +} diff --git a/ComputerShopContracts/ViewModels/BatchViewModel.cs b/ComputerShopContracts/ViewModels/BatchViewModel.cs new file mode 100644 index 0000000..8adf1b4 --- /dev/null +++ b/ComputerShopContracts/ViewModels/BatchViewModel.cs @@ -0,0 +1,25 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.ViewModels +{ + public class BatchViewModel : IBatchModel + { + [DisplayName("Дата партии")] + public DateTime DateBatch { get; set; } = DateTime.Now; + + public int ProductId { get; set; } + + [DisplayName("Номер")] + public int Id { get; set; } + + + + + } +} diff --git a/ComputerShopContracts/ViewModels/ComponentViewModel.cs b/ComputerShopContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..ceecfb9 --- /dev/null +++ b/ComputerShopContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,22 @@ +using ComputerShopDataModels.Models; +using System.ComponentModel; + +namespace ComputerShopContracts.ViewModels +{ + public class ComponentViewModel : IComponentModel + { + public int Id { get; set; } + + [DisplayName("Цена")] + public double Cost { get; set; } + + [DisplayName("Имя комплектующего")] + public string ComponentName { get; set; } = string.Empty; + + [DisplayName("Описание комплектующего")] + public string Description { get; set; } = string.Empty; + + public int UserId { get; set; } + + } +} diff --git a/ComputerShopContracts/ViewModels/ProductViewModel.cs b/ComputerShopContracts/ViewModels/ProductViewModel.cs new file mode 100644 index 0000000..49c3379 --- /dev/null +++ b/ComputerShopContracts/ViewModels/ProductViewModel.cs @@ -0,0 +1,23 @@ +using ComputerShopDataModels.Models; +using System.ComponentModel; + +namespace ComputerShopContracts.ViewModels +{ + public class ProductViewModel : IProductModel + { + [DisplayName("Цена")] + public double Cost { get; set; } + + [DisplayName("Название товара")] + public string ProductName { get; set; } = string.Empty; + + [DisplayName("Описание товара")] + public string Description { get; set; } = string.Empty; + + public int UserId { get; set; } + public int Id { get; set; } + + public int? BatchId { get; set; } + + } +} diff --git a/ComputerShopContracts/ViewModels/RequestViewModel.cs b/ComputerShopContracts/ViewModels/RequestViewModel.cs new file mode 100644 index 0000000..fa4db11 --- /dev/null +++ b/ComputerShopContracts/ViewModels/RequestViewModel.cs @@ -0,0 +1,23 @@ +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.ViewModels +{ + public class RequestViewModel : IRequestModel + { + [DisplayName("Дата создания")] + public DateTime DateRequest { get; set; } = DateTime.Now; + public int? AssemblyId { get; set; } + public IAssemblyModel? Assembly { get; set; } + + [DisplayName("Номер")] + public int Id { get; set; } + + } +} diff --git a/ComputerShopContracts/ViewModels/UserViewModel.cs b/ComputerShopContracts/ViewModels/UserViewModel.cs new file mode 100644 index 0000000..b47cf50 --- /dev/null +++ b/ComputerShopContracts/ViewModels/UserViewModel.cs @@ -0,0 +1,30 @@ +using ComputerShopDataModels.Enums; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopContracts.ViewModels +{ + public class UserViewModel : IUserModel + { + [DisplayName("ФИО")] + public string Fio { get; set; } = string.Empty; + + [DisplayName("Номер телефона")] + public string PhoneNumber { get; set; } = string.Empty; + + [DisplayName("Логин")] + public string Login { get; set; } = string.Empty; + + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + + [DisplayName("Почта")] + public string Email { get; set; } = string.Empty; + public int Id { get; set; } + } +} diff --git a/ComputerShopDataModels/ComputerShopDataModels.csproj b/ComputerShopDataModels/ComputerShopDataModels.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/ComputerShopDataModels/ComputerShopDataModels.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/ComputerShopDataModels/Models/IAssemblyModel.cs b/ComputerShopDataModels/Models/IAssemblyModel.cs new file mode 100644 index 0000000..5edca7a --- /dev/null +++ b/ComputerShopDataModels/Models/IAssemblyModel.cs @@ -0,0 +1,28 @@ +namespace ComputerShopDataModels.Models +{ + /// + /// Сборка + /// + public interface IAssemblyModel : IId + { + /// + /// Название сборки + /// + string AssemblyName { get; } + + /// + /// Стоимость + /// + double Cost { get; } + + /// + /// Пользователь, который создал сборку + /// + int UserId { get; } + + /// + /// Список комплектующих + /// + Dictionary AssemblyComponents { get; } + } +} diff --git a/ComputerShopDataModels/Models/IBatchModel.cs b/ComputerShopDataModels/Models/IBatchModel.cs new file mode 100644 index 0000000..4c5472a --- /dev/null +++ b/ComputerShopDataModels/Models/IBatchModel.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDataModels.Models +{ + /// + /// Партия товара + /// + public interface IBatchModel : IId + { + /// + /// Дата + /// + DateTime DateBatch { get; } + + + /// + /// ID товара + /// + int ProductId { get; } + } +} diff --git a/ComputerShopDataModels/Models/IComponentModel.cs b/ComputerShopDataModels/Models/IComponentModel.cs new file mode 100644 index 0000000..c6659b7 --- /dev/null +++ b/ComputerShopDataModels/Models/IComponentModel.cs @@ -0,0 +1,29 @@ +namespace ComputerShopDataModels.Models +{ + /// + /// Комплектующее + /// + public interface IComponentModel : IId + { + /// + /// Стоимость комплектующего + /// + double Cost { get; } + + /// + /// Название комплектующего + /// + string ComponentName { get; } + + /// + /// Название комплектующего + /// + string Description { get; } + + /// + /// Пользователь, который добавил комплектующее + /// + int UserId { get; } + + } +} diff --git a/ComputerShopDataModels/Models/IId.cs b/ComputerShopDataModels/Models/IId.cs new file mode 100644 index 0000000..9ec7fdd --- /dev/null +++ b/ComputerShopDataModels/Models/IId.cs @@ -0,0 +1,7 @@ +namespace ComputerShopDataModels.Models +{ + public interface IId + { + int Id { get; } + } +} diff --git a/ComputerShopDataModels/Models/IProductModel.cs b/ComputerShopDataModels/Models/IProductModel.cs new file mode 100644 index 0000000..3b5361e --- /dev/null +++ b/ComputerShopDataModels/Models/IProductModel.cs @@ -0,0 +1,33 @@ +namespace ComputerShopDataModels.Models +{ + /// + /// Товар + /// + public interface IProductModel : IId + { + /// + /// Стоимость товара + /// + double Cost { get; } + + /// + /// Название товара + /// + string ProductName { get; } + + /// + /// Название товара + /// + string Description { get; } + + /// + /// Пользователь, который добавил товар + /// + int UserId { get; } + + /// + /// Привязка товара к партии товаров + /// + int? BatchId { get; } + } +} diff --git a/ComputerShopDataModels/Models/IRequestModel.cs b/ComputerShopDataModels/Models/IRequestModel.cs new file mode 100644 index 0000000..1b7b9b2 --- /dev/null +++ b/ComputerShopDataModels/Models/IRequestModel.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDataModels.Models +{ + /// + /// Заявка + /// + public interface IRequestModel : IId + { + /// + /// Дата создания заявки + /// + DateTime DateRequest { get; } + + /// + /// ID сборки + /// + int? AssemblyId { get; } + + } +} diff --git a/ComputerShopDataModels/Models/IUserModel.cs b/ComputerShopDataModels/Models/IUserModel.cs new file mode 100644 index 0000000..d22874b --- /dev/null +++ b/ComputerShopDataModels/Models/IUserModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDataModels.Models +{ + public interface IUserModel : IId + { + string Fio { get; } + string PhoneNumber { get; } + string Login { get; } + string Password { get; } + string Email { get; } + } +} diff --git a/ComputerShopDatabaseImplement/ComputerShopDatabase.cs b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs new file mode 100644 index 0000000..64d3559 --- /dev/null +++ b/ComputerShopDatabaseImplement/ComputerShopDatabase.cs @@ -0,0 +1,32 @@ +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerShopDatabaseImplement +{ + public class ComputerShopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder OptionsBuilder) + { + if (!OptionsBuilder.IsConfigured) + { + OptionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ComputerStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=True"); + } + + base.OnConfiguring(OptionsBuilder); + + } + public virtual DbSet Users { get; set; } + + public virtual DbSet Components { get; set; } + + public virtual DbSet Assemblies { get; set; } + + public virtual DbSet AssemblyComponents { get; set; } + + public virtual DbSet Products { get; set; } + + public virtual DbSet Requests { get; set; } + public virtual DbSet Batchs { get; set; } + + } +} diff --git a/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj b/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj new file mode 100644 index 0000000..649a093 --- /dev/null +++ b/ComputerShopDatabaseImplement/ComputerShopDatabaseImplement.csproj @@ -0,0 +1,25 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + diff --git a/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs new file mode 100644 index 0000000..1e5fa54 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs @@ -0,0 +1,99 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class AssemblyStorage : IAssemblyStorage + { + public List GetFullList() + { + using var Context = new ComputerShopDatabase(); + + return Context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Assembly) + .Select(x => x.ViewModel) + .ToList(); + } + + public List GetFilteredList(AssemblySearchModel Model) + { + using var Context = new ComputerShopDatabase(); + // сборки по комплектующим + return Context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Assembly) + .Select(x => x.ViewModel) + .ToList(); + } + + public AssemblyViewModel? GetElement(AssemblySearchModel Model) + { + using var Context = new ComputerShopDatabase(); + + return Context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Assembly) + .FirstOrDefault(x => x.Id == Model.Id)? + .ViewModel; + } + + public AssemblyViewModel? Insert(AssemblyBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var NewAssembly = Assembly.Create(Context, Model); + Context.Assemblies.Add(NewAssembly); + Context.SaveChanges(); + + return NewAssembly.ViewModel; + } + + public AssemblyViewModel? Update(AssemblyBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + using var Transaction = Context.Database.BeginTransaction(); + + try + { + var ExistingAssembly = Context.Assemblies.FirstOrDefault(x => x.Id == Model.Id); + if (ExistingAssembly == null) + { + return null; + } + + ExistingAssembly.Update(Model); + Context.SaveChanges(); + ExistingAssembly.UpdateComponents(Context, Model); + Transaction.Commit(); + + return ExistingAssembly.ViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + + public AssemblyViewModel? Delete(AssemblyBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var ExistingAssembly = Context.Assemblies.Include(x => x.Components).FirstOrDefault(x => x.Id == Model.Id); + if (ExistingAssembly == null) + { + return null; + } + + Context.Assemblies.Remove(ExistingAssembly); + Context.SaveChanges(); + + return ExistingAssembly.ViewModel; + } + } +} \ No newline at end of file diff --git a/ComputerShopDatabaseImplement/Implements/BatchStorage.cs b/ComputerShopDatabaseImplement/Implements/BatchStorage.cs new file mode 100644 index 0000000..bf098c3 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/BatchStorage.cs @@ -0,0 +1,111 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class BatchStorage : IBatchStorage + { + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + + return context.Batchs + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(BatchSearchModel model) + { + using var context = new ComputerShopDatabase(); + //сортировка партий по дате + if (model.DateFrom.HasValue && model.DateTo.HasValue) + { + return context.Batchs + .Where(x => (x.DateBatch >= model.DateFrom && x.DateBatch <= model.DateTo)) + .Select(x => x.GetViewModel) + .ToList(); + } + + //партии по товарам + return context.Batchs + .Where(x => x.ProductId == model.ProductId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public BatchViewModel? GetElement(BatchSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Batchs + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public BatchViewModel? Insert(BatchBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newBatch = Batch.Create(context, model); + + if (newBatch == null) + { + return null; + } + context.Batchs.Add(newBatch); + context.SaveChanges(); + + return newBatch.GetViewModel; + } + + public BatchViewModel? Update(BatchBindingModel model) + { + using var context = new ComputerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var batch = context.Batchs.FirstOrDefault(x => x.Id == model.Id); + if (batch == null) + { + return null; + } + batch.Update(model); + context.SaveChanges(); + transaction.Commit(); + return context.Batchs + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public BatchViewModel? Delete(BatchBindingModel model) + { + using var context = new ComputerShopDatabase(); + var batch = context.Batchs.FirstOrDefault(y => y.Id == model.Id); + if (batch != null) + { + context.Batchs.Remove(batch); + context.SaveChanges(); + return batch.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..70865f6 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,82 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var Context = new ComputerShopDatabase(); + + return Context.Components + .Select(x => x.ViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel Model) + { + using var Context = new ComputerShopDatabase(); + // колмплектующие по пользователям (дополнительно) + return Context.Components + .Where(x => x.UserId == Model.UserId) + .Select(x => x.ViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel Model) + { + using var Context = new ComputerShopDatabase(); + + return Context.Components + .FirstOrDefault(x => x.Id == Model.Id)? + .ViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var NewComponent = Component.Create(Model); + Context.Components.Add(NewComponent); + Context.SaveChanges(); + + return NewComponent.ViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == Model.Id); + if (ExistingComponent == null) + { + return null; + } + + ExistingComponent.Update(Model); + Context.SaveChanges(); + + return ExistingComponent.ViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var ExistingComponent = Context.Components.FirstOrDefault(x => x.Id == Model.Id); + if (ExistingComponent == null) + { + return null; + } + + Context.Components.Remove(ExistingComponent); + Context.SaveChanges(); + + return ExistingComponent.ViewModel; + } + } +} diff --git a/ComputerShopDatabaseImplement/Implements/ProductStorage.cs b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..9e40274 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,102 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var Context = new ComputerShopDatabase(); + + return Context.Products + .Select(x => x.ViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel Model) + { + using var Context = new ComputerShopDatabase(); + + // сортировка по партиям + if (Model.BatchId.HasValue) + { + return Context.Products + .Where(x => x.UserId == Model.UserId && x.BatchId == Model.BatchId) + .Select(x => x.ViewModel) + .ToList(); + } + + return Context.Products + .Where(x => x.UserId == Model.UserId) + .Select(x => x.ViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel Model) + { + using var Context = new ComputerShopDatabase(); + + return Context.Products + .FirstOrDefault(x => x.Id == Model.Id)? + .ViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var NewProduct = Product.Create(Context, Model); + Context.Products.Add(NewProduct); + Context.SaveChanges(); + + return NewProduct.ViewModel; + } + + public ProductViewModel? Update(ProductBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + using var Transaction = Context.Database.BeginTransaction(); + + try + { + var ExistingProduct = Context.Products.FirstOrDefault(x => x.Id == Model.Id); + if (ExistingProduct == null) + { + return null; + } + + ExistingProduct.Update(Model); + Context.SaveChanges(); + Transaction.Commit(); + + return ExistingProduct.ViewModel; + } + catch + { + Transaction.Rollback(); + throw; + } + } + + public ProductViewModel? Delete(ProductBindingModel Model) + { + using var Context = new ComputerShopDatabase(); + + var ExistingProduct = Context.Products.FirstOrDefault(x => x.Id == Model.Id); + if (ExistingProduct == null) + { + return null; + } + + Context.Products.Remove(ExistingProduct); + Context.SaveChanges(); + + return ExistingProduct.ViewModel; + } + } +} diff --git a/ComputerShopDatabaseImplement/Implements/RequestStorage.cs b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs new file mode 100644 index 0000000..e69ae8b --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/RequestStorage.cs @@ -0,0 +1,104 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class RequestStorage : IRequestStorage + { + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Requests + .Include(x => x.Assembly) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RequestViewModel? GetElement(RequestSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerShopDatabase(); + return context.Requests + .Include(x => x.Assembly) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public RequestViewModel? Insert(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + var newRequest = Request.Create(context, model); + if (newRequest == null) + { + return null; + } + context.Requests.Add(newRequest); + context.SaveChanges(); + return newRequest.GetViewModel; + } + + public RequestViewModel? Update(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var request = context.Requests.FirstOrDefault(x => x.Id == model.Id); + if (request == null) + { + return null; + } + request.Update(model); + context.SaveChanges(); + transaction.Commit(); + return request.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public RequestViewModel? Delete(RequestBindingModel model) + { + using var context = new ComputerShopDatabase(); + var request = context.Requests + .FirstOrDefault(y => y.Id == model.Id); + if (request != null) + { + context.Requests.Remove(request); + context.SaveChanges(); + return request.GetViewModel; + } + return null; + } + + public bool ConnectRequestAssembly(int requestId, int assemblyId) + { + using var context = new ComputerShopDatabase(); + var request = context.Requests.FirstOrDefault(x => x.Id == requestId); + var assembly = context.Assemblies.FirstOrDefault(x => x.Id == assemblyId); + if (request == null || assembly == null) + { + return false; + } + request.ConnectAssembly(context, assemblyId); + context.SaveChanges(); + return true; + } + } +} diff --git a/ComputerShopDatabaseImplement/Implements/UserStorage.cs b/ComputerShopDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..1977b19 --- /dev/null +++ b/ComputerShopDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,68 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; +using ComputerShopContracts.StorageContracts; +using ComputerShopContracts.ViewModels; +using ComputerShopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public List GetFullList() + { + using var context = new ComputerShopDatabase(); + return context.Users.Select(x => x.GetViewModel).ToList(); + } + + + public UserViewModel? GetElement(UserSearchModel model) + { + using var context = new ComputerShopDatabase(); + return context.Users.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new ComputerShopDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new ComputerShopDatabase(); + var user = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new ComputerShopDatabase(); + var user = context.Users.FirstOrDefault(rec => rec.Id == model.Id); + if (user != null) + { + context.Users.Remove(user); + context.SaveChanges(); + return user.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerShopDatabaseImplement/Models/Assembly.cs b/ComputerShopDatabaseImplement/Models/Assembly.cs new file mode 100644 index 0000000..7fc6418 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Assembly.cs @@ -0,0 +1,115 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerShopDatabaseImplement.Models +{ + public class Assembly : IAssemblyModel + { + public int Id { get; private set; } + + [Required] + public string AssemblyName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; private set; } + + [Required] + public int UserId { get; private set; } + + [ForeignKey("ComponentId")] + public virtual List Components { get; set; } = new(); + + private Dictionary? _assemblyComponents; + + [NotMapped] + public Dictionary AssemblyComponents + { + get + { + if (_assemblyComponents == null) + { + _assemblyComponents = Components.ToDictionary( + AsmComp => AsmComp.ComponentId, + AsmComp => (AsmComp.Component as IComponentModel, AsmComp.Count) + ); + } + + return _assemblyComponents; + } + } + + public static Assembly Create(ComputerShopDatabase Context, AssemblyBindingModel Model) + { + return new() + { + Id = Model.Id, + AssemblyName = Model.AssemblyName, + Cost = Model.Cost, + UserId = Model.UserId, + Components = Model.AssemblyComponents.Select(x => new AssemblyComponent + { + Component = Context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList(), + }; + } + + public void Update(AssemblyBindingModel Model) + { + if (!string.IsNullOrEmpty(Model.AssemblyName)) + { + AssemblyName = Model.AssemblyName; + } + + Cost = Model.Cost; + } + + public void UpdateComponents(ComputerShopDatabase Context, AssemblyBindingModel Model) + { + var AssemblyComponents = Context.AssemblyComponents.Where(x => x.AssemblyId == Model.Id).ToList(); + if (AssemblyComponents != null && AssemblyComponents.Count > 0) + { + Context.AssemblyComponents + .RemoveRange(AssemblyComponents + .Where(x => !Model.AssemblyComponents.ContainsKey(x.ComponentId))); + Context.SaveChanges(); + + foreach (var ComponentToUpdate in AssemblyComponents) + { + ComponentToUpdate.Count = Model.AssemblyComponents[ComponentToUpdate.ComponentId].Item2; + Model.AssemblyComponents.Remove(ComponentToUpdate.ComponentId); + } + + Context.SaveChanges(); + } + + var CurrentAssembly = Context.Assemblies.First(x => x.Id == Id); + foreach (var AssemblyComponent in Model.AssemblyComponents) + { + Context.AssemblyComponents.Add(new AssemblyComponent + { + Assembly = CurrentAssembly, + Component = Context.Components.First(x => x.Id == AssemblyComponent.Key), + Count = AssemblyComponent.Value.Item2 + }); + + Context.SaveChanges(); + } + + _assemblyComponents = null; + } + + public AssemblyViewModel ViewModel => new() + { + Id = Id, + AssemblyName = AssemblyName, + Cost = Cost, + UserId = UserId, + AssemblyComponents = AssemblyComponents, + }; + + } +} diff --git a/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs b/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs new file mode 100644 index 0000000..ea0c084 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/AssemblyComponent.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerShopDatabaseImplement.Models +{ + public class AssemblyComponent + { + public int Id { get; set; } + + [Required] + public int AssemblyId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Assembly Assembly { get; set; } = new(); + + public virtual Component Component { get; set; } = new(); + } +} diff --git a/ComputerShopDatabaseImplement/Models/Batch.cs b/ComputerShopDatabaseImplement/Models/Batch.cs new file mode 100644 index 0000000..e79bb14 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Batch.cs @@ -0,0 +1,51 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using Microsoft.IdentityModel.Tokens; +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 ComputerShopDatabaseImplement.Models +{ + public class Batch : IBatchModel + { + public int Id { get; set; } + + [Required] + public DateTime DateBatch { get; set; } = DateTime.Now; + + [Required] + public int ProductId { get; set; } + public static Batch Create(ComputerShopDatabase context, BatchBindingModel model) + { + return new Batch() + { + Id = model.Id, + DateBatch = model.DateBatch, + ProductId = model.ProductId, + }; + } + + public void Update(BatchBindingModel model) + { + if (model == null) + { + return; + } + ProductId = model.ProductId; + DateBatch = model.DateBatch; + } + + public BatchViewModel GetViewModel => new() + { + Id = Id, + DateBatch = DateBatch, + ProductId = ProductId + }; + } +} diff --git a/ComputerShopDatabaseImplement/Models/Component.cs b/ComputerShopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..aa413d8 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerShopDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + + [Required] + public double Cost { get; private set; } + + [Required] + public string ComponentName { get; private set; } = string.Empty; + + [Required] + public string Description { get; private set; } = string.Empty; + + [Required] + public int UserId { get; private set; } + + + [ForeignKey("ComponentId")] + public virtual List AssemblyComponents { get; set; } = new(); + + public static Component Create(ComponentBindingModel Model) + { + return new() + { + Id = Model.Id, + Cost = Model.Cost, + ComponentName = Model.ComponentName, + UserId = Model.UserId, + }; + } + + public void Update(ComponentBindingModel Model) + { + Cost = Model.Cost; + ComponentName = Model.ComponentName; + + } + + public ComponentViewModel ViewModel => new() + { + Id = Id, + Cost = Cost, + ComponentName = ComponentName, + UserId = UserId, + + }; + } +} diff --git a/ComputerShopDatabaseImplement/Models/Product.cs b/ComputerShopDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..0388f60 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Product.cs @@ -0,0 +1,67 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerShopDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + + [Required] + public double Cost { get; set; } + + [Required] + public string ProductName { get; set; } = string.Empty; + + [Required] + public string Description { get; set; } = string.Empty; + + [Required] + public int UserId { get; set; } + + public int? BatchId { get; set; } + + + public static Product Create(ComputerShopDatabase Context, ProductBindingModel Model) + { + return new() + { + Id = Model.Id, + Cost = Model.Cost, + ProductName = Model.ProductName, + Description = Model.Description, + UserId = Model.UserId, + BatchId = Model.BatchId, + }; + } + + public void Update(ProductBindingModel Model) + { + if (!string.IsNullOrEmpty(Model.ProductName)) + { + ProductName = Model.ProductName; + } + + Cost = Model.Cost; + ProductName = Model.ProductName; + Description = Model.Description; + UserId = Model.UserId; + BatchId = Model.BatchId; + } + + public ProductViewModel ViewModel => new() + { + Id = Id, + Cost = Cost, + ProductName = ProductName, + Description = Description, + UserId = UserId, + BatchId = BatchId, + }; + + } + +} diff --git a/ComputerShopDatabaseImplement/Models/Request.cs b/ComputerShopDatabaseImplement/Models/Request.cs new file mode 100644 index 0000000..d0a2745 --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/Request.cs @@ -0,0 +1,58 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; +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 ComputerShopDatabaseImplement.Models +{ + public class Request : IRequestModel + { + public int Id { get; set; } + + + public int? AssemblyId { get; set; } + + public virtual Assembly? Assembly { get; set; } = new(); + + [Required] + public DateTime DateRequest { get; set; } + + + public static Request Create(ComputerShopDatabase context, RequestBindingModel model) + { + return new Request() { + Id = model.Id, + AssemblyId = model.AssemblyId, + Assembly = model.AssemblyId.HasValue ? context.Assemblies.First(x => x.Id == model.AssemblyId) : null, + DateRequest = model.DateRequest, + }; + } + + public void Update(RequestBindingModel model) + { + if (model == null) { + return; + } + } + + public RequestViewModel GetViewModel => new() + { + Id = Id, + AssemblyId = AssemblyId, + Assembly = Assembly, + DateRequest = DateRequest, + }; + + public void ConnectAssembly(ComputerShopDatabase context, int assemblyId) + { + AssemblyId = assemblyId; + Assembly = context.Assemblies.First(x => x.Id == assemblyId); + } + } +} diff --git a/ComputerShopDatabaseImplement/Models/User.cs b/ComputerShopDatabaseImplement/Models/User.cs new file mode 100644 index 0000000..a71510e --- /dev/null +++ b/ComputerShopDatabaseImplement/Models/User.cs @@ -0,0 +1,70 @@ +using ComputerShopContracts.BindingModels; +using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Enums; +using ComputerShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerShopDatabaseImplement.Models +{ + public class User : IUserModel + { + public int Id { get; private set; } + + [Required] + public string Fio { get; set; } = string.Empty; + + [Required] + public string PhoneNumber { get; set; } = string.Empty; + + [Required] + public string Login { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + + public static User Create(UserBindingModel model) + { + return new User + { + Id = model.Id, + Fio = model.Fio, + PhoneNumber = model.PhoneNumber, + Login = model.Login, + Password = model.Password, + Email = model.Email, + }; + } + + public void Update(UserBindingModel model) + { + if (model == null) + { + return; + } + Fio = model.Fio; + PhoneNumber = model.PhoneNumber; + Login = model.Login; + Password = model.Password; + Email = model.Email; + } + + public UserViewModel GetViewModel => new() + { + Id = Id, + Fio = Fio, + PhoneNumber = PhoneNumber, + Login = Login, + Password = Password, + Email = Email, + }; + } +} diff --git a/README.md b/README.md index eb9c4d6..0bf1858 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ -# PIbd-21_Medvedkov_Coursach +# Курсовая работа "Магазин компьютерной техники "Ты ж программист"." -9.4 Магазин компьютерной техники «Ты ж программист». Поручитель. \ No newline at end of file +Роли: +- Исполнитель (9.3) - Чернышев Г.Я. +- Поручитель (9.4) - Шабунов О.А.