diff --git a/LawFirm/LawFirm/Program.cs b/LawFirm/LawFirm/Program.cs index 09dd87f..8babc23 100644 --- a/LawFirm/LawFirm/Program.cs +++ b/LawFirm/LawFirm/Program.cs @@ -1,7 +1,7 @@ using LawFirmBusinessLogic.BusinessLogics; using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.StorageContracts; -using LawFirmFileImplement.Implements; +using LawFirmDatabaseImplement.Implements; using LawFirmView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs new file mode 100644 index 0000000..57174f5 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs @@ -0,0 +1,91 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class BlankStorage : IBlankStorage + { + public BlankViewModel? GetElement(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Blanks.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.BlankName) && x.BlankName == model.BlankName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + + } + + public List GetFilteredList(BlankSearchModel model) + { + if (string.IsNullOrEmpty(model.BlankName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Blanks + .Where(x => x.BlankName.Contains(model.BlankName)) + .Select(x => x.GetViewModel) + .ToList(); + + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Blanks.Select(x => x.GetViewModel).ToList(); + } + + public BlankViewModel? Insert(BlankBindingModel model) + { + var newBlank = Blank.Create(model); + if (newBlank == null) + { + return null; + } + using var context = new LawFirmDatabase(); + context.Blanks.Add(newBlank); + context.SaveChanges(); + return newBlank.GetViewModel; + + } + + public BlankViewModel? Update(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var blank = context.Blanks.FirstOrDefault(x => x.Id == model.Id); + if (blank == null) + { + return null; + } + blank.Update(model); + context.SaveChanges(); + return blank.GetViewModel; + } + public BlankViewModel? Delete(BlankBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Blanks.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Blanks.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + + } + + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs new file mode 100644 index 0000000..631fac6 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/DocumentStorage.cs @@ -0,0 +1,128 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata.Ecma335; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class DocumentStorage : IDocumentStorage + { + public DocumentViewModel? GetElement(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + /*return context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;*/ + + var element = context.Documents.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) + || (model.Id.HasValue && x.Id == model.Id)); + + return element?.GetViewModel; + } + + public List GetFilteredList(DocumentSearchModel model) + { + if (string.IsNullOrEmpty(model.DocumentName)) + { + return new(); + } + using var context = new LawFirmDatabase(); + /*return context.Documents + .Where(x => x.DocumentName.Contains(model.DocumentName)) + .Select(x => x.GetViewModel) + .ToList();*/ + var filteredList = context.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).ToList(); + return filteredList.Select(x=> x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + //return context.Documents.Select(x => x.GetViewModel).ToList(); + var fullList = context.Documents.ToList(); + return fullList.Select(x => x.GetViewModel).ToList(); + } + + public DocumentViewModel? Insert(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var newDoc = Document.Create(context, model); + if (newDoc == null) + { + transaction.Rollback(); + return null; + } + context.Documents.Add(newDoc); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return newDoc.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + + public DocumentViewModel? Update(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + using var transaction = context.Database.BeginTransaction(); + { + try + { + var document = context.Documents.FirstOrDefault(x => x.Id == model.Id); + if (document == null) + { + transaction.Rollback(); + return null; + } + + document.Update(model); + + context.SaveChanges(); + context.Database.CommitTransaction(); + + return document.GetViewModel; + } + catch (Exception) + { + transaction.Rollback(); + return null; + } + } + } + public DocumentViewModel? Delete(DocumentBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Documents.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Documents.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1c37874 --- /dev/null +++ b/LawFirm/LawFirmDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,83 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace LawFirmDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new LawFirmDatabase(); + return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new LawFirmDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new LawFirmDatabase(); + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new LawFirmDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new LawFirmDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +}