78 lines
2.3 KiB
C#
78 lines
2.3 KiB
C#
|
using DatabaseImplement.Models;
|
|||
|
using Contracts.BindingModels;
|
|||
|
using Contracts.SearchModels;
|
|||
|
using Contracts.ViewModels;
|
|||
|
using Contracts.StoragesContracts;
|
|||
|
|
|||
|
namespace DatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class DetailStorage : IDetailStorage
|
|||
|
{
|
|||
|
public DetailViewModel? Delete(DetailBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newDetail = context.Details.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (newDetail == null)
|
|||
|
return null;
|
|||
|
context.Details.Remove(newDetail);
|
|||
|
context.SaveChanges();
|
|||
|
return newDetail.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public DetailViewModel? GetElement(DetailSearchModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
return context.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name) && !model.UserId.HasValue)
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
return context.Details.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
else if (model.UserId.HasValue)
|
|||
|
{
|
|||
|
return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return context.Details.Where(x => model.Name == x.Name).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<DetailViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
return context.Details.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public DetailViewModel? Insert(DetailBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newDetail = Detail.Create(model);
|
|||
|
if (newDetail == null)
|
|||
|
return null;
|
|||
|
context.Details.Add(newDetail);
|
|||
|
context.SaveChanges();
|
|||
|
return newDetail.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public DetailViewModel? Update(DetailBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FactoryGoWorkDatabase();
|
|||
|
var newDetail = context.Details.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (newDetail == null)
|
|||
|
return null;
|
|||
|
newDetail.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return newDetail.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|