DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs

175 lines
7.6 KiB
C#
Raw Permalink Normal View History

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);
}
}
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");
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");
if (!EmployeeCodeExists(Employee.EmployeeCode))
2023-08-15 22:52:30 -05:00
{
2023-08-25 17:44:04 -05:00
EmployeeDbContext.Employees.Add(_employee);
Employee.Id = _employee.Id;
2023-08-15 22:52:30 -05:00
EmployeeDbContext.SaveChanges();
2023-11-28 12:00:12 -05:00
//return (true, Employee, null);
return (true, mapper.Map<Db.Employee, Models.Employee>(_employee), null);
2023-08-15 22:52:30 -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);
}
}
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)
{
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();
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);
}
}
public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(int Id)
2023-08-15 22:52:30 -05:00
{
try
{
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());
return (false, null, ex.Message);
2023-08-15 22:52:30 -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
{
return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.EmployeeCode.ToLower() == employeeCode.ToLower()) > 0;
2023-08-15 22:52:30 -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" });
//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();
}
}
}
}