117 lines
2.9 KiB
C#
117 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Reflection.Metadata;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SoftwareContracts.BindingModels;
|
|
using SoftwareContracts.SearchModels;
|
|
using SoftwareContracts.StoragesContracts;
|
|
using SoftwareContracts.ViewModels;
|
|
using SoftwareDataBase.Models;
|
|
|
|
namespace SoftwareDataBase.Repository
|
|
{
|
|
public class TestStorage:ITestStorage
|
|
{
|
|
|
|
public List<TestViewModel> GetFullList()
|
|
{
|
|
using var context = new SoftwareDateBase();
|
|
return context.Tests
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public TestViewModel? GetElement(TestSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new SoftwareDateBase();
|
|
return context.Tests
|
|
.Include(x => x.Project)
|
|
.FirstOrDefault(x => x.Id == model.Id || x.ProjectId == model.ProjectId)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<TestViewModel> GetFilteredList(TestSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.TestStatus.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
|
|
{
|
|
return new List<TestViewModel>();
|
|
}
|
|
using var context = new SoftwareDateBase();
|
|
var query = context.Tests.Include(x => x.Project).AsQueryable();
|
|
|
|
// Постепенное применение фильтров
|
|
// Применяем фильтры только если указаны соответствующие критерии
|
|
if (model.Id.HasValue)
|
|
{
|
|
query = query.Where(x => x.Id == model.Id.Value);
|
|
}
|
|
|
|
if (model.TestStatus.HasValue)
|
|
{
|
|
query = query.Where(x => x.TestStatus == model.TestStatus.Value);
|
|
}
|
|
|
|
if (model.DateFrom.HasValue)
|
|
{
|
|
query = query.Where(x => x.CreatedDate >= model.DateFrom.Value);
|
|
}
|
|
|
|
if (model.DateTo.HasValue)
|
|
{
|
|
query = query.Where(x => x.CreatedDate <= model.DateTo.Value);
|
|
}
|
|
|
|
return query
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public TestViewModel? Insert(TestBindingModel model)
|
|
{
|
|
var newComponent = Test.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SoftwareDateBase();
|
|
context.Tests.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public TestViewModel? Update(TestBindingModel model)
|
|
{
|
|
using var context = new SoftwareDateBase();
|
|
var component = context.Tests.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public TestViewModel? Delete(TestBindingModel model)
|
|
{
|
|
using var context = new SoftwareDateBase();
|
|
var element = context.Tests.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Tests.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|