EmployeeService.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using AutoMapper;
  2. using MTWorkHR.Application.Exceptions;
  3. using MTWorkHR.Application.Logging;
  4. using MTWorkHR.Application.Models;
  5. using MTWorkHR.Core.IRepositories;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace MTWorkHR.Application.Services
  12. {
  13. public class EmployeeService
  14. {
  15. private readonly IEmployeeRepository _employeeRepository;
  16. private readonly IMapper _mapper;
  17. private readonly IAppLogger<UserDto> _logger;
  18. public EmployeeService(IEmployeeRepository employeeRepository, IMapper mapper, IAppLogger<UserDto> logger)
  19. {
  20. _employeeRepository = employeeRepository;
  21. _mapper = mapper;
  22. _logger = logger;
  23. }
  24. public UserDto Get(int id)
  25. {
  26. var employeeDto = new UserDto();
  27. try
  28. {
  29. var emp = _employeeRepository.GetByIdAsync(id);
  30. if (emp == null)
  31. {
  32. _logger.LogWarning("NOT FOUND Emp");
  33. throw new NotFoundException("Employee not in DB", id);
  34. }
  35. else
  36. _logger.LogInformation("OK _ Found Employee");
  37. _mapper.Map(emp, employeeDto);
  38. }catch(Exception e)
  39. {
  40. _logger.LogWarning($"emp [{id}]" + e.Message);
  41. throw new BadRequest($"empExc [{id}]" + e.Message);
  42. }
  43. return employeeDto;
  44. }
  45. }
  46. }