118 lines
3.9 KiB
C#
118 lines
3.9 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;
|
|
using Task = TaskTrackerDatabase.Models.Task;
|
|
|
|
namespace TaskTrackerDatabase.Implements
|
|
{
|
|
public class TaskStorage : ITaskStorage
|
|
{
|
|
public List<TaskViewModel> GetFullList()
|
|
{
|
|
using var context = new TaskTrackerDatabase();
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<TaskViewModel> GetFilteredList(TaskSearchModel model)
|
|
{
|
|
using var context = new TaskTrackerDatabase();
|
|
if (model.Id.HasValue)
|
|
{
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
else if (model.UserId.HasValue)
|
|
{
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.Where(x => x.UserId == model.UserId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
return new();
|
|
}
|
|
|
|
public TaskViewModel? GetElement(TaskSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TaskTrackerDatabase();
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public TaskViewModel? Insert(TaskBindingModel model)
|
|
{
|
|
var newTask = Task.Create(model);
|
|
if (newTask == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TaskTrackerDatabase();
|
|
context.Tasks.Add(newTask);
|
|
context.SaveChanges();
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == newTask.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public TaskViewModel? Update(TaskBindingModel model)
|
|
{
|
|
using var context = new TaskTrackerDatabase();
|
|
var order = context.Tasks.FirstOrDefault(x => x.Id == model.Id);
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
return context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public TaskViewModel? Delete(TaskBindingModel model)
|
|
{
|
|
using var context = new TaskTrackerDatabase();
|
|
var element = context.Tasks.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
var deletedElement = context.Tasks
|
|
.Include(x => x.Project)
|
|
.Include(x => x.User)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
context.Tasks.Remove(element);
|
|
context.SaveChanges();
|
|
return deletedElement;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|