diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDataBase.cs b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDataBase.cs index df28dd3..d0f654f 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDataBase.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/AbstractLawFirmDataBase.cs @@ -14,7 +14,7 @@ namespace AbstractLawFirmDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=GOKA\SQLEXPRESS;Initial Catalog=AbstractLawFirmDataBase Full;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-N8BRIPR\SQLEXPRESS;Initial Catalog=AbstractLawFirmDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs index 8b18364..fc12fa6 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/ComponentStorage.cs @@ -2,8 +2,10 @@ using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataBaseImplement; +using AbstractLawFirmDataBaseImplement.Models; using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -11,35 +13,72 @@ using System.Threading.Tasks; namespace AbstractLawFirmDatabaseImplement.Implements { public class ComponentStorage : IComponentStorage - { - public ComponentViewModel? Delete(ComponentBindingModel model) - { - throw new NotImplementedException(); - } - - public ComponentViewModel? GetElement(ComponentSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFilteredList(ComponentSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFullList() - { - throw new NotImplementedException(); - } - - public ComponentViewModel? Insert(ComponentBindingModel model) - { - throw new NotImplementedException(); - } - - public ComponentViewModel? Update(ComponentBindingModel model) - { - throw new NotImplementedException(); - } + { + public List GetFullList() + { + using var context = new AbstractLawFirmDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new AbstractLawFirmDataBase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDataBase(); + return context.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new AbstractLawFirmDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new AbstractLawFirmDataBase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new AbstractLawFirmDataBase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs index 4727a60..a916835 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -3,6 +3,9 @@ using AbstractLawFirmContracts.BindingModels.BindingModels; using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.StoragesContracts; using AbstractLawFirmContracts.ViewModels; +using AbstractLawFirmDataBaseImplement; +using AbstractLawFirmDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -12,35 +15,98 @@ using System.Threading.Tasks; namespace AbstractLawFirmDatabaseImplement.Implements { public class DocumentStorage : IDocumentStorage - { - public DocumentViewModel? Delete(DocumentBindingModel model) - { - throw new NotImplementedException(); - } - - public DocumentViewModel? GetElement(DocumentSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFilteredList(DocumentSearchModel model) - { - throw new NotImplementedException(); - } - - public List GetFullList() - { - throw new NotImplementedException(); - } - - public DocumentViewModel? Insert(DocumentBindingModel model) - { - throw new NotImplementedException(); - } - - public DocumentViewModel? Update(DocumentBindingModel model) - { - throw new NotImplementedException(); - } + { + public List GetFullList() + { + using var context = new AbstractLawFirmDataBase(); + return context.Documents + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new AbstractLawFirmDataBase(); + return context.Documents + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && + !model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDataBase(); + return context.Documents + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && + x.DocumentName == model.DocumentName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDataBase(); + var newDocument = Document.Create(context, model); + if (newDocument == null) + { + return null; + } + context.Documents.Add(newDocument); + context.SaveChanges(); + return newDocument.GetViewModel; + } + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Documents.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new AbstractLawFirmDataBase(); + var element = context.Documents + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Documents.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs index 036e6fc..52f2afa 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -2,11 +2,14 @@ using AbstractLawFirmContracts.SearchModels; using AbstractLawFirmContracts.ViewModels; using AbstractLawFirmContracts.StoragesContracts; +using AbstractLawFirmDataBaseImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AbstractLawFirmDataBaseImplement; +using Microsoft.EntityFrameworkCore; namespace AbstractLawFirmDatabaseImplement.Implements { @@ -14,32 +17,74 @@ namespace AbstractLawFirmDatabaseImplement.Implements { public OrderViewModel? Delete(OrderBindingModel model) { - throw new NotImplementedException(); + using var context = new AbstractLawFirmDataBase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public OrderViewModel? GetElement(OrderSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return null; + } + using var context = new AbstractLawFirmDataBase(); + return context.Orders + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(OrderSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return new(); + } + using var context = new AbstractLawFirmDataBase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new AbstractLawFirmDataBase(); + return context.Orders + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) { - throw new NotImplementedException(); + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new AbstractLawFirmDataBase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { - throw new NotImplementedException(); + using var context = new AbstractLawFirmDataBase(); + var component = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; } } } diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240325152423_InitialCreate.Designer.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240325152423_InitialCreate.Designer.cs index 14fd17b..7dedd3d 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240325152423_InitialCreate.Designer.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/20240325152423_InitialCreate.Designer.cs @@ -19,7 +19,7 @@ namespace AbstractLawFirmDatabaseImplement.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "6.0.28") + .HasAnnotation("DocumentVersion", "6.0.28") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); diff --git a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDataBaseModelSnapshot.cs b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDataBaseModelSnapshot.cs index c6d34ab..f356f2b 100644 --- a/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDataBaseModelSnapshot.cs +++ b/LawFirm/AbstractLawFirmDatabaseImplement/Migrations/AbstractLawFirmDataBaseModelSnapshot.cs @@ -17,7 +17,7 @@ namespace AbstractLawFirmDatabaseImplement.Migrations { #pragma warning disable 612, 618 modelBuilder - .HasAnnotation("ProductVersion", "6.0.28") + .HasAnnotation("DocumentVersion", "6.0.28") .HasAnnotation("Relational:MaxIdentifierLength", 128); SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); diff --git a/LawFirm/LawFirm.sln b/LawFirm/LawFirm.sln index 52338f1..45059dc 100644 --- a/LawFirm/LawFirm.sln +++ b/LawFirm/LawFirm.sln @@ -15,7 +15,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmListImplemen EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmFileImplement", "AbstractLawFirmFileImpliment\AbstractLawFirmFileImplement.csproj", "{8AF960C2-208F-4B7B-9F8B-36B63446B6B7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractLawFirmDatabaseImplement", "AbstractLawFirmDatabaseImplement\AbstractLawFirmDatabaseImplement.csproj", "{BF41FCA1-C12C-4D56-B29C-8BF1D9AE69B1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractLawFirmDataBaseImplement", "AbstractLawFirmDatabaseImplement\AbstractLawFirmDataBaseImplement.csproj", "{BF41FCA1-C12C-4D56-B29C-8BF1D9AE69B1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/LawFirm/LawFirmView/LawFirmView.csproj b/LawFirm/LawFirmView/LawFirmView.csproj index f4a4211..697d6b7 100644 --- a/LawFirm/LawFirmView/LawFirmView.csproj +++ b/LawFirm/LawFirmView/LawFirmView.csproj @@ -19,7 +19,7 @@ - + diff --git a/LawFirm/LawFirmView/Program.cs b/LawFirm/LawFirmView/Program.cs index cffdc50..1986fb9 100644 --- a/LawFirm/LawFirmView/Program.cs +++ b/LawFirm/LawFirmView/Program.cs @@ -1,7 +1,7 @@ using AbstractLawFirmBusinessLogic.BusinessLogic; using AbstractLawFirmContracts.BusinessLogicsContracts; using AbstractLawFirmContracts.StoragesContracts; -using AbstractLawFirmFileImplement.Implements; +using AbstractLawFirmDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;