107 lines
2.6 KiB
C#
107 lines
2.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TaskTrackerContracts.BindingModels;
|
|
using TaskTrackerContracts.SearchModels;
|
|
using TaskTrackerContracts.StoragesContracts;
|
|
using TaskTrackerContracts.ViewModels;
|
|
|
|
namespace TaskTrackerDatabaseImplement.Implements
|
|
{
|
|
public class ProjectStorage : IProjectStorage
|
|
{
|
|
public ProjectViewModel? Delete(ProjectBindingModel model)
|
|
{
|
|
|
|
using var context = new TasktrackerContext();
|
|
var element = context.Projects.Include(x => x.Company)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Projects.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ProjectViewModel? GetElement(ProjectSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
return context.Projects.Include(x => x.Company)
|
|
.FirstOrDefault(x =>
|
|
(model.Id.HasValue && x.Id ==
|
|
model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<ProjectViewModel> GetFilteredList(ProjectSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.CompanyId.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
if (model.CompanyId.HasValue)
|
|
{
|
|
|
|
return context.Projects
|
|
.Include(x => x.Company)
|
|
.Where(x => x.CompanyId == model.CompanyId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
return context.Projects
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ProjectViewModel> GetFullList()
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
return context.Projects.Include(x => x.Company)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public ProjectViewModel? Insert(ProjectBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
var newProduct = Project.Create(model);
|
|
if (newProduct == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Projects.Add(newProduct);
|
|
context.SaveChanges();
|
|
return context.Projects.Include(x => x.Company).FirstOrDefault(x => x.Id == newProduct.Id)?.GetViewModel;
|
|
}
|
|
|
|
public ProjectViewModel? Update(ProjectBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
|
|
var product = context.Projects.Include(x => x.Company).FirstOrDefault(rec =>
|
|
rec.Id == model.Id);
|
|
if (product == null)
|
|
{
|
|
return null;
|
|
}
|
|
product.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return product.GetViewModel;
|
|
}
|
|
}
|
|
}
|