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(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(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(Db.Employee Employee) { try { logger?.LogInformation("Query Employee"); if (!EmployeeExists(Employee.Id)) { EmployeeDbContext.Employees.Add(Employee); EmployeeDbContext.SaveChanges(); var result = mapper.Map(Employee); return (true, result, null); } return (false, null, "Employee 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(Db.Employee Employee) { try { if (Employee != null) { var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id.ToLower() == Employee.Id.ToLower()).FirstOrDefaultAsync(); if (_employee != null) { EmployeeDbContext.Employees.Update(Employee); EmployeeDbContext.SaveChanges(); return (true, mapper.Map(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(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(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" }); EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp5", Name = "ABC5", Email = "abc5@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-23) ,IsActive = true, PreferredLanguage = "sp" }); EmployeeDbContext.Employees.Add(new Db.Employee() { Id = "Emp6", Name = "ABC6", Email = "abc6@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-32) ,IsActive = true, PreferredLanguage = "sp" }); EmployeeDbContext.SaveChanges(); } } } }