Coursach/Course/DatabaseImplement/Implements/DetailStorage.cs

71 lines
2.2 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();
2024-05-23 20:02:34 +04:00
return context.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Equals(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2024-04-27 21:07:45 +04:00
}
public List<DetailViewModel> GetFilteredList(DetailSearchModel model)
{
2024-04-30 22:27:52 +04:00
if (!model.UserId.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
2024-04-27 21:07:45 +04:00
{
return new();
}
using var context = new FactoryGoWorkDatabase();
2024-04-30 22:27:52 +04:00
if (model.DateFrom.HasValue)
2024-05-26 23:23:24 +04:00
return context.Details.Where(x => x.UserId == model.UserId).Where(x => x.DateCreate <= model.DateTo && x.DateCreate >= model.DateFrom).Select(x => x.GetViewModel).ToList();
2024-04-30 22:27:52 +04:00
else
2024-05-23 20:02:34 +04:00
return context.Details.Where(x => x.UserId == model.UserId).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;
}
}
}