84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using SYBDContracts.BindingModels;
|
|
using SYBDContracts.SearchModels;
|
|
using SYBDContracts.StoragesContracts;
|
|
using SYBDContracts.ViewModels;
|
|
using SYBDDatabaseImplement.Models;
|
|
|
|
namespace SYBDDatabaseImplement.Implements
|
|
{
|
|
public class AutoStorage : IAutoStorage
|
|
{
|
|
public List<AutoViewModel> GetFullList()
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
return context.auto
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<AutoViewModel> GetFilteredList(AutoSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.StateNumber))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.auto
|
|
.Where(x => x.StateNumber.Contains(model.StateNumber))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public AutoViewModel? GetElement(AutoSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.StateNumber) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
return context.auto
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.StateNumber) && x.StateNumber == model.StateNumber) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public AutoViewModel? Insert(AutoBindingModel model)
|
|
{
|
|
var newAuto = Auto.Create(model);
|
|
if (newAuto == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SYBDDatabase();
|
|
context.auto.Add(newAuto);
|
|
context.SaveChanges();
|
|
return newAuto.GetViewModel;
|
|
}
|
|
|
|
public AutoViewModel? Update(AutoBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var component = context.auto.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public AutoViewModel? Delete(AutoBindingModel model)
|
|
{
|
|
using var context = new SYBDDatabase();
|
|
var element = context.auto.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.auto.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |