PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/EmployeeStorage.cs

102 lines
3.4 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyPsqlImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyPsqlImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
private readonly ConstructionCompanyDatabase _source;
public EmployeeStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<EmployeeViewModel> GetFullList()
{
List<EmployeeViewModel> result = new List<EmployeeViewModel>();
foreach (var material in _source.Employees)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
{
if (model == null || !model.Id.HasValue && string.IsNullOrEmpty(model.EmployeeName))
{
return new();
}
List<EmployeeViewModel> result = new List<EmployeeViewModel>();
if (!string.IsNullOrEmpty(model.EmployeeName))
{
foreach (var material in _source.Employees)
{
if (material.EmployeeName.Equals(model.EmployeeName)) result.Add(material.GetViewModel);
}
return result;
}
else
{
foreach (var material in _source.Employees)
{
if (material.Id == model.Id) result.Add(material.GetViewModel);
}
return result;
}
}
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
return _source.Employees.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
var command = Employee.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newEmployee = _source.Employees[_source.Employees.Count - 1];
return newEmployee.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
var command = Employee.UpdateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var updatedEmployee = _source.Employees.First(x => x.Id == model.Id);
return updatedEmployee.GetViewModel;
}
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
var command = Employee.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedEmployee = _source.Employees.First(x => x.Id == model.Id).GetViewModel;
_source.ExecuteSql(command);
return deletedEmployee;
}
}
}