77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
|
using SushiContracts.BindingModels;
|
|||
|
using SushiContracts.SearchModels;
|
|||
|
using SushiContracts.StorageContracts;
|
|||
|
using SushiContracts.ViewModels;
|
|||
|
using SushiDatabaseImplement.Models;
|
|||
|
|
|||
|
namespace SushiDatabaseImplement.implements
|
|||
|
{
|
|||
|
public class ComponentStorage : IComponentStorage
|
|||
|
{
|
|||
|
public List<ComponentViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new SushiDatabase();
|
|||
|
return context.Components.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.ComponentName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new SushiDatabase();
|
|||
|
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 SushiDatabase();
|
|||
|
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 SushiDatabase();
|
|||
|
context.Components.Add(newComponent);
|
|||
|
context.SaveChanges();
|
|||
|
return newComponent.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ComponentViewModel? Update(ComponentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SushiDatabase();
|
|||
|
var client = context.Components.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (client == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
client.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return client.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ComponentViewModel? Delete(ComponentBindingModel model)
|
|||
|
{
|
|||
|
using var context = new SushiDatabase();
|
|||
|
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Components.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|