From 7830d9c21f7556d6a9771fa3f65dcc9ddceaba4b Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Wed, 24 Apr 2024 12:50:12 +0400 Subject: [PATCH] ImplementerStorage --- .../DatabaseImplement.csproj | 4 -- .../Implements/ImplementerStorage.cs | 72 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 Course/DatabaseImplement/Implements/ImplementerStorage.cs diff --git a/Course/DatabaseImplement/DatabaseImplement.csproj b/Course/DatabaseImplement/DatabaseImplement.csproj index 3dbe418..dae78de 100644 --- a/Course/DatabaseImplement/DatabaseImplement.csproj +++ b/Course/DatabaseImplement/DatabaseImplement.csproj @@ -6,10 +6,6 @@ enable - - - - diff --git a/Course/DatabaseImplement/Implements/ImplementerStorage.cs b/Course/DatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..c96278d --- /dev/null +++ b/Course/DatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,72 @@ +using DatabaseImplement.Models; +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Contracts.StoragesContracts; + +namespace DatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (newImplementer == null) + return null; + context.Implementers.Remove(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; } + using var context = new FactoryGoWorkDatabase(); + return context.Implementers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) + return new(); + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Implementers.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Implementers.Where(x => x.Login.Equals(model.Login)).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + return null; + context.Implementers.Add(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (newImplementer == null) + return null; + newImplementer.Update(model); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + } +}