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 logger; private IMapper mapper; public EmployeesProvider(EmployeeDbContext EmployeeDbContext, ILogger logger, IMapper mapper) { this.EmployeeDbContext = EmployeeDbContext; this.logger = logger; this.mapper = mapper; // SeedData(); } public async Task<(bool IsSuccess, IEnumerable 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>(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) { try { logger?.LogInformation("Query Employee"); var Employee = await EmployeeDbContext.Employees.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id); if (Employee != null) { logger?.LogInformation($"{Employee} customer(s) found"); var result = mapper.Map(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)> PostEmployeeAsync(Models.Employee Employee) { try { Db.Employee _employee = mapper.Map(Employee); logger?.LogInformation("Query Employee"); if (!EmployeeCodeExists(Employee.EmployeeCode)) { EmployeeDbContext.Employees.Add(_employee); Employee.Id = _employee.Id; EmployeeDbContext.SaveChanges(); return (true, Employee, null); } return (false, null, "Employee code is already exits"); } 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) { try { if (Employee != null) { var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id == Id).FirstOrDefaultAsync(); if (_employee != null) { Db.Employee vEmployee = mapper.Map(Employee); EmployeeDbContext.Employees.Update(vEmployee); EmployeeDbContext.SaveChanges(); Employee.Id = Id; return (true, Employee, "Successful"); } 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) { try { Db.Employee Employee = EmployeeDbContext.Employees.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault(); if (Employee == null) { return (false, null, "Not Found"); } EmployeeDbContext.Employees.Remove(Employee); EmployeeDbContext.SaveChanges(); return (true, mapper.Map(Employee), $"EmployeeId {Id} deleted Successfuly"); } catch (Exception ex) { logger?.LogError(ex.ToString()); return (false, null, ex.Message); } } private bool EmployeeExists(int id) { return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.Id == id) > 0; } private bool EmployeeCodeExists(string employeeCode) { return EmployeeDbContext.Employees.AsNoTracking().Count(e => e.EmployeeCode.ToLower() == employeeCode.ToLower()) > 0; } public void SeedData() { if (!EmployeeDbContext.Employees.Any()) { 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" }); EmployeeDbContext.SaveChanges(); } } } }