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;
|
2023-10-06 17:22:37 -05:00
|
|
|
|
// SeedData();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(int Id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Query Employee");
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var Employee = await EmployeeDbContext.Employees.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
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");
|
2023-09-13 00:28:24 -05:00
|
|
|
|
if (!EmployeeCodeExists(Employee.EmployeeCode))
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-08-25 17:44:04 -05:00
|
|
|
|
EmployeeDbContext.Employees.Add(_employee);
|
2023-09-13 00:28:24 -05:00
|
|
|
|
Employee.Id = _employee.Id;
|
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
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return (false, null, "Employee code is already exits");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError(ex.ToString());
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(int Id, Models.Employee Employee)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (Employee != null)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id == Id).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-09-13 00:28:24 -05:00
|
|
|
|
Employee.Id = Id;
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(int Id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
Db.Employee Employee = EmployeeDbContext.Employees.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
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());
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return (false, null, ex.Message);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
private bool EmployeeExists(int id)
|
|
|
|
|
{
|
|
|
|
|
return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.Id == id) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool EmployeeCodeExists(string employeeCode)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.EmployeeCode.ToLower() == employeeCode.ToLower()) > 0;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 20:31:41 -05:00
|
|
|
|
public void SeedData()
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (!EmployeeDbContext.Employees.Any())
|
|
|
|
|
{
|
2023-10-06 17:22:37 -05:00
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "10101", Name = "David", Email = "david@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-18), IsActive = true, PreferredLanguage = "en" });
|
|
|
|
|
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "20202", Name = "Smith", Email = "smith@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-22), IsActive = true, PreferredLanguage = "fr" });
|
2023-10-04 14:03:59 -05:00
|
|
|
|
//EmployeeDbContext.Employees.Add(new Db.Employee() { Id = 3, EmployeeCode = "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 = 4, EmployeeCode = "Emp4", Name = "ABC4", Email = "abc4@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-20), IsActive = true, PreferredLanguage = "en" });
|
|
|
|
|
//EmployeeDbContext.Employees.Add(new Db.Employee() { Id = 5, EmployeeCode = "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 = 6, EmployeeCode = "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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|