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

103 lines
3.8 KiB
C#
Raw Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyMongoDBImplement.Models;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyMongoDBImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
private readonly ConstructionCompanyDatabase _source;
private readonly IPositionStorage _positionStorage;
public EmployeeStorage(IPositionStorage positionStorage)
{
_source = ConstructionCompanyDatabase.GetInstance();
_positionStorage = positionStorage;
}
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)
{
model.Id = _source.Employees.Count > 0 ? _source.Employees.Max(x => x.Id) + 1 : 0;
var document = Employee.CreateBSON(model);
if (document == null)
{
return null;
}
_source.InsertDocument(document, "Employees");
var newEmployee = _source.Employees[_source.Employees.Count - 1];
return newEmployee.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
var document = Employee.UpdateBSON(model);
if (document == null)
{
return null;
}
_source.ReplaceDocument(document, new BsonDocument { { "_id", model.Id } }, "Employees");
var position = _source.Positions.First(x => x.Id == model.PositionID);
_positionStorage.Update(new PositionBindingModel { Id = position.Id, PositionName = position.PositionName, Salary = position.Salary });
var updatedEmployee = _source.Employees.First(x => x.Id == model.Id);
return updatedEmployee.GetViewModel;
}
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
var deletedEmployee = _source.Employees.First(x => x.Id == model.Id).GetViewModel;
_source.DeleteDocument(new BsonDocument { { "_id", model.Id } }, "Employees");
return deletedEmployee;
}
}
}