EmployeeService.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using AutoMapper;
  2. using MTWorkHR.Application.Exceptions;
  3. using MTWorkHR.Application.Models;
  4. using MTWorkHR.Core.IRepositories;
  5. using MTWorkHR.Core.Logging;
  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 CompanyService
  14. {
  15. private readonly ICompanyRepository _companyRepository;
  16. private readonly IMapper _mapper;
  17. private readonly IAppLogger<UserDto> _logger;
  18. public CompanyService(ICompanyRepository companyRepository, IMapper mapper, IAppLogger<UserDto> logger)
  19. {
  20. _companyRepository = companyRepository;
  21. _mapper = mapper;
  22. _logger = logger;
  23. }
  24. public UserDto Get(int id)
  25. {
  26. var companyDto = new UserDto();
  27. try
  28. {
  29. var company = _companyRepository.GetByIdAsync(id);
  30. if (company == null)
  31. {
  32. _logger.LogWarning("NOT FOUND Company");
  33. throw new NotFoundException("company not in DB", id);
  34. }
  35. else
  36. _logger.LogInformation("OK _ Found company");
  37. _mapper.Map(company, companyDto);
  38. }catch(Exception e)
  39. {
  40. _logger.LogWarning($"company [{id}]" + e.Message);
  41. throw new BadRequest($"companyExc [{id}]" + e.Message);
  42. }
  43. return companyDto;
  44. }
  45. }
  46. }