Coursach/Course/DatabaseImplement/Implements/DetailStorage.cs
Sergey Kozyrev f502a218a7 FixDatabase + UpdateView
Заряжай, заря-заря-жай, заряжай меня
Мне не спрятаться от тебя и не убежать
2024-04-30 17:03:49 +04:00

68 lines
1.9 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.UserId.HasValue)
{
return new();
}
using var context = new FactoryGoWorkDatabase();
return context.Details.Where(x => x.UserId == model.Id).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;
}
}
}