103 lines
3.8 KiB
C#

using Marco.Pms.DataAccess.Data;
using Marco.Pms.Model.Employees;
using Marco.Pms.Model.Mapper;
using Marco.Pms.Model.ViewModels.Employee;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Runtime.Intrinsics.Arm;
namespace MarcoBMS.Services.Helpers
{
public class EmployeeHelper
{
private readonly ApplicationDbContext _context;
public EmployeeHelper(ApplicationDbContext context)
{
_context = context;
}
public async Task<Employee> GetEmployeeByID(int EmployeeID)
{
return await _context.Employees.Include(e => e.JobRole).FirstOrDefaultAsync(e => e.Id == EmployeeID) ?? new Employee { };
}
public async Task<Employee> GetEmployeeByApplicationUserID(string ApplicationUserID)
{
try
{
var result = await _context.Employees.Where(c => c.ApplicationUserId == ApplicationUserID).ToListAsync();
return await _context.Employees.Where(c => c.ApplicationUserId == ApplicationUserID).SingleOrDefaultAsync();
}
catch (Exception ex)
{
return null;
}
}
public async Task<List<EmployeeVM>> SearchEmployeeByProjectId(int TenentId, string searchString, int? ProjectId)
{
try
{
List<EmployeeVM> result = new List<EmployeeVM>();
if (ProjectId != null)
{
result = await (from pa in _context.ProjectAllocations.Where(c => c.ProjectId == ProjectId)
join em in _context.Employees.Where(c => c.TenantId == TenentId).Include(fp => fp.JobRole) // Include Feature
on pa.EmployeeId equals em.Id
where em.FirstName.ToLower().Contains(searchString.ToLower()) || em.LastName.ToLower().Contains(searchString.ToLower())
select em.ToEmployeeVMFromEmployee()
)
.ToListAsync();
}
else
{
result = await _context.Employees.Where(c => c.TenantId == TenentId &&
(c.FirstName.ToLower().Contains(searchString.ToLower()) || c.LastName.ToLower().Contains(searchString.ToLower()))).Include(fp => fp.JobRole)
.Select(c => c.ToEmployeeVMFromEmployee()).ToListAsync();
}
return result;
}
catch (Exception ex)
{
return new List<EmployeeVM>();
}
}
public async Task<List<EmployeeVM>> GetEmployeeByProjectId(int TenentId, int? ProjectId)
{
try
{
List<EmployeeVM> result = new List<EmployeeVM>();
if (ProjectId != null)
{
result = await (from pa in _context.ProjectAllocations.Where(c => c.ProjectId == ProjectId)
join em in _context.Employees.Where(c => c.TenantId == TenentId).Include(fp => fp.JobRole) // Include Feature
on pa.EmployeeId equals em.Id
select em.ToEmployeeVMFromEmployee()
)
.ToListAsync();
}
else
{
result = await _context.Employees.Where(c => c.TenantId == TenentId).Include(fp => fp.JobRole)
.Select(c => c.ToEmployeeVMFromEmployee()).ToListAsync();
}
return result;
}
catch (Exception ex)
{
return new List<EmployeeVM>();
}
}
}
}