86 lines
3.0 KiB
C#
86 lines
3.0 KiB
C#
|
using ComputerHardwareStoreContracts.BindingModels;
|
|||
|
using ComputerHardwareStoreContracts.SearchModels;
|
|||
|
using ComputerHardwareStoreContracts.StorageContracts;
|
|||
|
using ComputerHardwareStoreContracts.ViewModels;
|
|||
|
using ComputerHardwareStoreDatabaseImplement.Models;
|
|||
|
|
|||
|
namespace ComputerHardwareStoreDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ComponentStorage : IComponentStorage
|
|||
|
{
|
|||
|
public List<ComponentViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Components
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Name))
|
|||
|
{
|
|||
|
return new ();
|
|||
|
}
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Components
|
|||
|
.Where(x => x.Name.Contains(model.Name))
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
return context.Components
|
|||
|
.FirstOrDefault(x =>
|
|||
|
(!string.IsNullOrEmpty(model.Name)
|
|||
|
&& x.Name == model.Name)
|
|||
|
|| (model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ComponentViewModel? Insert(ComponentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
var newComponent = Component.Create(context, model);
|
|||
|
if (newComponent == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Components.Add(newComponent);
|
|||
|
context.SaveChanges();
|
|||
|
return newComponent.GetViewModel;
|
|||
|
}
|
|||
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerHardwareStoreDBContext();
|
|||
|
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 ComputerHardwareStoreDBContext();
|
|||
|
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Components.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|