2023-08-15 22:52:30 -05:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
using DamageAssesment.Api.Employees.Db;
|
|
|
|
|
using DamageAssesment.Api.Employees.Interfaces;
|
|
|
|
|
using DamageAssesment.Api.Employees.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DamageAssesment.Api.Employees.Providers
|
|
|
|
|
{
|
|
|
|
|
public class EmployeesProvider : IEmployeesProvider
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private EmployeeDbContext EmployeeDbContext;
|
|
|
|
|
private ILogger<EmployeesProvider> logger;
|
|
|
|
|
private IMapper mapper;
|
|
|
|
|
|
|
|
|
|
public EmployeesProvider(EmployeeDbContext EmployeeDbContext, ILogger<EmployeesProvider> logger, IMapper mapper)
|
|
|
|
|
{
|
|
|
|
|
this.EmployeeDbContext = EmployeeDbContext;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.mapper = mapper;
|
|
|
|
|
SeedData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Query Question");
|
|
|
|
|
var Employee = await EmployeeDbContext.Employees.AsNoTracking().ToListAsync();
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"{Employee.Count} Employees(s) found");
|
|
|
|
|
var result = mapper.Map<IEnumerable<Db.Employee>, IEnumerable<Models.Employee>>(Employee);
|
|
|
|
|
return (true, result, null);
|
|
|
|
|
}
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(string Id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Query Employee");
|
|
|
|
|
var Employee = await EmployeeDbContext.Employees.AsNoTracking().FirstOrDefaultAsync(q => q.Id.ToLower() == Id.ToLower());
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"{Employee} customer(s) found");
|
|
|
|
|
var result = mapper.Map<Db.Employee, Models.Employee>(Employee);
|
|
|
|
|
return (true, result, null);
|
|
|
|
|
}
|
|
|
|
|
return (false, null, "Not found");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-25 17:44:04 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Models.Employee Employee)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-25 17:44:04 -05:00
|
|
|
|
Db.Employee _employee = mapper.Map<Models.Employee, Db.Employee>(Employee);
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
logger?.LogInformation("Query Employee");
|
|
|
|
|
if (!EmployeeExists(Employee.Id))
|
|
|
|
|
{
|
2023-08-25 17:44:04 -05:00
|
|
|
|
EmployeeDbContext.Employees.Add(_employee);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
EmployeeDbContext.SaveChanges();
|
2023-08-25 17:44:04 -05:00
|
|
|
|
return (true, Employee, null);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
return (false, null, "Employee is already exits");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-25 17:44:04 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(string Id , Models.Employee Employee)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
2023-08-25 17:44:04 -05:00
|
|
|
|
var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id.ToLower() == Id.ToLower()).FirstOrDefaultAsync();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (_employee != null)
|
|
|
|
|
{
|
2023-08-25 17:44:04 -05:00
|
|
|
|
Db.Employee vEmployee = mapper.Map<Models.Employee, Db.Employee>(Employee);
|
|
|
|
|
EmployeeDbContext.Employees.Update(vEmployee);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
EmployeeDbContext.SaveChanges();
|
2023-08-25 17:44:04 -05:00
|
|
|
|
return (true, Employee, "Successful");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"{Employee} Not found");
|
|
|
|
|
return (false, null, "Not Found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation($"{Employee} Bad Request");
|
|
|
|
|
return (false, null, "Bad request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(string Id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Db.Employee Employee = EmployeeDbContext.Employees.AsNoTracking().Where(a => a.Id.ToLower() == Id.ToLower()).FirstOrDefault();
|
|
|
|
|
if (Employee == null)
|
|
|
|
|
{
|
|
|
|
|
return (false, null, "Not Found");
|
|
|
|
|
}
|
|
|
|
|
EmployeeDbContext.Employees.Remove(Employee);
|
|
|
|
|
EmployeeDbContext.SaveChanges();
|
|
|
|
|
return (true, mapper.Map<Db.Employee, Models.Employee>(Employee), $"EmployeeId {Id} deleted Successfuly");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false,null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private bool EmployeeExists(string id)
|
|
|
|
|
{
|
|
|
|
|
return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.Id.ToLower() == id.ToLower()) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SeedData()
|
|
|
|
|
{
|
|
|
|
|
if (!EmployeeDbContext.Employees.Any())
|
|
|
|
|
{
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp1", Name = "ABC1", Email = "abc1@gmail.com", OfficePhoneNumber = "12345678",BirthDate=DateTime.Now.AddYears(-18), IsActive = true,PreferredLanguage="en" });
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp2", Name = "ABC2", Email = "abc2@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-22), IsActive = true, PreferredLanguage = "fr" });
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp3", Name = "ABC3", Email = "abc3@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-30) ,IsActive = true, PreferredLanguage = "fr" });
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp4", Name = "ABC4", Email = "abc4@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-20) ,IsActive = true, PreferredLanguage = "en" });
|
2023-08-22 16:05:51 -05:00
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp5", Name = "ABC5", Email = "abc5@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-23) ,IsActive = true, PreferredLanguage = "es" });
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp6", Name = "ABC6", Email = "abc6@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-32) ,IsActive = true, PreferredLanguage = "es" });
|
2023-08-15 22:52:30 -05:00
|
|
|
|
EmployeeDbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|