73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using ConstructionFirmDatabaseImplement.Models;
|
|
using Subd_4.BindingModels;
|
|
using Subd_4.SearchModels;
|
|
using Subd_4.StoragesContracts;
|
|
using Subd_4.ViewModels;
|
|
|
|
namespace ConstructionFirmDatabaseImplement.Implements
|
|
{
|
|
public class EmployeeStorage : IEmployeeStorage
|
|
{
|
|
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.FullName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Employees.Where(x => x.FullName.Contains(model.FullName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<EmployeeViewModel> GetFullList()
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Employees.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public EmployeeViewModel? Delete(EmployeeBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var element = context.Employees.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Employees.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
return context.Employees.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FullName)) && x.FullName == model.FullName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
|
{
|
|
var newEmployee = Employee.Create(model);
|
|
if (newEmployee == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new ConstructionFirmDatabase();
|
|
context.Employees.Add(newEmployee);
|
|
context.SaveChanges();
|
|
return newEmployee.GetViewModel;
|
|
}
|
|
|
|
public EmployeeViewModel? Update(EmployeeBindingModel model)
|
|
{
|
|
using var context = new ConstructionFirmDatabase();
|
|
var component = context.Employees.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|