152 lines
4.6 KiB
C#
152 lines
4.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TaskTrackerContracts.BindingModels;
|
|
using TaskTrackerContracts.SearchModels;
|
|
using TaskTrackerContracts.StoragesContracts;
|
|
using TaskTrackerContracts.ViewModels;
|
|
using TaskTrackerModels.Models;
|
|
|
|
namespace TaskTrackerDatabaseImplement.Implements
|
|
{
|
|
public class TaskAssigmentStorage : ITaskAssigmentStorage
|
|
{
|
|
public TaskAssigmentViewModel? Delete(TaskAssigmentBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
var element = context.TaskAssigments.Include(x => x.Task).Include(x => x.Employee)
|
|
.FirstOrDefault(rec => rec.TaskId == model.TaskId && rec.EmployeeId == model.EmployeeId);
|
|
if (element != null)
|
|
{
|
|
context.TaskAssigments.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public TaskAssigmentViewModel? GetElement(TaskAssigmentSearchModel model)
|
|
{
|
|
if (!model.EmployeeId.HasValue || !model.TaskId.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
return context.TaskAssigments.Include(x => x.Task).Include(x => x.Employee)
|
|
.FirstOrDefault(x =>
|
|
(model.TaskId.HasValue && x.TaskId ==
|
|
model.TaskId) && (model.EmployeeId.HasValue && x.
|
|
EmployeeId ==
|
|
model.EmployeeId))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<TaskAssigmentViewModel> GetFilteredList(TaskAssigmentSearchModel model)
|
|
{
|
|
if (!model.TaskId.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
return context.TaskAssigments
|
|
.Include(x => x.Task).Include (x => x.Employee)
|
|
.Where(x => x.TaskId == model.TaskId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<TaskViewModel> GetFilteredAssigmentList(EmployeeSearchModel model)
|
|
{
|
|
NpgsqlConnection connection = new NpgsqlConnection("Host=192.168.56.101;Port=5432;Database=tasktracker;Username=postgres;Password=urogil01");
|
|
List<TaskViewModel> tasks = new List<TaskViewModel>();
|
|
|
|
|
|
connection.Open();
|
|
using (NpgsqlCommand getRecordStorage = new NpgsqlCommand("Select * from task where task.id in (Select task_assigment.task_id from task_assigment where task_assigment.employee_id in (@employeeId)) ", connection))
|
|
{
|
|
getRecordStorage.Parameters.AddWithValue("employeeId", model.Id);
|
|
using (NpgsqlDataReader reader = getRecordStorage.ExecuteReader())
|
|
{
|
|
|
|
while (reader.Read())
|
|
{
|
|
TaskViewModel viewModel = ReadTaskView(reader);
|
|
tasks.Add(viewModel);
|
|
}
|
|
}
|
|
}
|
|
connection.Close();
|
|
return tasks;
|
|
}
|
|
|
|
public TaskViewModel ReadTaskView(NpgsqlDataReader reader)
|
|
{
|
|
int? id = reader["id"] as int?;
|
|
string name = reader["name"] as string;
|
|
DateTime? date_create = reader["start_date"] as DateTime?;
|
|
DateTime? date_end = reader["end_date"] as DateTime?;
|
|
DateTime? deadline = reader["deadline"] as DateTime?;
|
|
int? project_id = reader["project_id"] as int?;
|
|
string state = reader["state"] as string;
|
|
|
|
using var context = new TasktrackerContext();
|
|
|
|
TaskViewModel view = new TaskViewModel
|
|
{
|
|
Id = id.Value,
|
|
Name = name,
|
|
DateCreate = (DateTime)date_create,
|
|
DateDone = date_end,
|
|
Deadline = (DateTime)deadline,
|
|
ProjectId = project_id.Value,
|
|
Status = state,
|
|
ProjectName = context.Projects.FirstOrDefault(x => x.Id == project_id)?.Name ?? string.Empty,
|
|
};
|
|
return view;
|
|
}
|
|
|
|
public List<TaskAssigmentViewModel> GetFullList()
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
return context.TaskAssigments.Include(x => x.Task).Include(x=> x.Employee)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public TaskAssigmentViewModel? Insert(TaskAssigmentBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
var newProduct = TaskAssigment.Create(model);
|
|
if (newProduct == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.TaskAssigments.Add(newProduct);
|
|
context.SaveChanges();
|
|
return context.TaskAssigments.Include(x => x.Task).Include(x => x.Employee).FirstOrDefault(x => x.TaskId == newProduct.TaskId && x.EmployeeId == newProduct.EmployeeId)?.GetViewModel;
|
|
}
|
|
|
|
public TaskAssigmentViewModel? Update(TaskAssigmentBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
|
|
var product = context.TaskAssigments.Include(x => x.Task).Include(x => x.Employee).FirstOrDefault(x => x.TaskId == model.TaskId && x.EmployeeId == model.EmployeeId);
|
|
if (product == null)
|
|
{
|
|
return null;
|
|
}
|
|
product.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return product.GetViewModel;
|
|
}
|
|
|
|
}
|
|
|
|
}
|