100 lines
2.4 KiB
C#
100 lines
2.4 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 EmployeeStorage : IEmployeeStorage
|
|
{
|
|
public EmployeeViewModel? Delete(EmployeeBindingModel model)
|
|
{
|
|
|
|
using var context = new TasktrackerContext();
|
|
var element = context.Employees.Include(x => x.Company)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Employees.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
return context.Employees.Include(x => x.Company)
|
|
.FirstOrDefault(x =>
|
|
(model.Id.HasValue && x.Id ==
|
|
model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue && !model.CompanyId.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new TasktrackerContext();
|
|
return context.Employees
|
|
.Include(x => x.Company)
|
|
.Where(x => x.CompanyId == model.CompanyId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
|
|
}
|
|
|
|
public List<EmployeeViewModel> GetFullList()
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
return context.Employees.Include(x => x.Company)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
var newProduct = Employee.Create(model);
|
|
if (newProduct == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Employees.Add(newProduct);
|
|
context.SaveChanges();
|
|
return context.Employees.Include(x => x.Company).FirstOrDefault(x => x.Id == newProduct.Id)?.GetViewModel;
|
|
}
|
|
|
|
public EmployeeViewModel? Update(EmployeeBindingModel model)
|
|
{
|
|
using var context = new TasktrackerContext();
|
|
|
|
var product = context.Employees.Include(x => x.Company).FirstOrDefault(rec =>
|
|
rec.Id == model.Id);
|
|
if (product == null)
|
|
{
|
|
return null;
|
|
}
|
|
product.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return product.GetViewModel;
|
|
}
|
|
}
|
|
}
|