Coursach/Course/DatabaseImplement/Implements/DetailStorage.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2024-04-27 21:07:45 +04:00
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)
2024-04-27 21:07:45 +04:00
{
return new();
}
using var context = new FactoryGoWorkDatabase();
return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList();
2024-04-27 21:07:45 +04:00
}
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;
}
}
}