diff --git a/SUBD/SUBD/ComponentDatabase/ComponentStorage.cs b/SUBD/SUBD/ComponentDatabase/ComponentStorage.cs new file mode 100644 index 0000000..82f9a96 --- /dev/null +++ b/SUBD/SUBD/ComponentDatabase/ComponentStorage.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SUBD.ComponentEntity; +using SUBD.ComponentInterfaces; + +namespace SUBD.ComponentDatabase +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComponentsDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ComponentsDatabase(); + 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 ComponentsDatabase(); + 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 ComponentsDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComponentsDatabase(); + 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 ComponentsDatabase(); + 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/SUBD/SUBD/ComponentEntity/ComponentStorage.cs b/SUBD/SUBD/ComponentEntity/ComponentStorage.cs deleted file mode 100644 index 7162963..0000000 --- a/SUBD/SUBD/ComponentEntity/ComponentStorage.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using SUBD.ComponentDatabase; -using SUBD.ComponentInterfaces; - -namespace SUBD.ComponentEntity -{ - public class ComponentStorage : IComponentStorage - { - public List GetFullList() - { - using var context = new ComponentsDatabase(); - return context.Components.Select(x => x.GetViewModel).ToList(); - } - - public List GetFilteredList(ComponentSearchModel model) - { - if (string.IsNullOrEmpty(model.ComponentName)) - { - return new(); - } - using var context = new ComponentsDatabase(); - 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 ComponentsDatabase(); - 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 = ComponentDatabase.Component.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new ComponentsDatabase(); - context.Components.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - - public ComponentViewModel? Update(ComponentBindingModel model) - { - using var context = new ComponentsDatabase(); - 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 ComponentsDatabase(); - 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/SUBD/SUBD/Program.cs b/SUBD/SUBD/Program.cs index 1934119..4f02846 100644 --- a/SUBD/SUBD/Program.cs +++ b/SUBD/SUBD/Program.cs @@ -2,6 +2,7 @@ using SUBD.ComponentEntity; using Microsoft.Extensions.DependencyInjection; using System; using SUBD.ComponentInterfaces; +using SUBD.ComponentDatabase; namespace SUBD {