CompanyService.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.WebUtilities;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using MTWorkHR.Application.Identity;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories;
  10. using MTWorkHR.Core.UnitOfWork;
  11. using MTWorkHR.Application.Services.Interfaces;
  12. using MTWorkHR.Core.Email;
  13. using MTWorkHR.Core.Entities;
  14. using MTWorkHR.Infrastructure.UnitOfWorks;
  15. using MTWorkHR.Infrastructure.Entities;
  16. using System.Transactions;
  17. using MTWorkHR.Core.Entities.Base;
  18. namespace MTWorkHR.Application.Services
  19. {
  20. public class CompanyService :BaseService<Company, CompanyDto, CompanyDto>, ICompanyService
  21. {
  22. private readonly IUnitOfWork _unitOfWork;
  23. private readonly AppSettingsConfiguration _configuration;
  24. private readonly GlobalInfo _globalInfo;
  25. private readonly IUserService _userService;
  26. public CompanyService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService) : base(unitOfWork)
  27. {
  28. _unitOfWork = unitOfWork;
  29. _configuration = configuration;
  30. _globalInfo = globalInfo;
  31. _userService = userService;
  32. }
  33. public override async Task<CompanyDto> GetById(long CompanyId)
  34. {
  35. var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId);
  36. var companyResponse = MapperObject.Mapper.Map<CompanyDto>(entity);
  37. var userDto = await _userService.GetById(entity.UserId);
  38. companyResponse.CompanyUser = userDto;
  39. return companyResponse;
  40. }
  41. public async Task<List<CompanyDto>> GetAllCompanies()
  42. {
  43. var Companys = await _unitOfWork.Company.GetAllAsync();
  44. var response = MapperObject.Mapper.Map<List<CompanyDto>>(Companys);
  45. return response;
  46. }
  47. public override async Task<CompanyDto> Create(CompanyDto input)
  48. {
  49. var userResp = await _userService.Create(input.CompanyUser);
  50. input.UserId = userResp?.Id;
  51. var entity = MapperObject.Mapper.Map<Company>(input);
  52. if (entity is null)
  53. {
  54. throw new AppException(ExceptionEnum.MapperIssue);
  55. }
  56. var task = await _unitOfWork.Company.AddAsync(entity);
  57. await _unitOfWork.CompleteAsync();
  58. var response = MapperObject.Mapper.Map<CompanyDto>(task);
  59. return response;
  60. }
  61. public override async Task<CompanyDto> Update(CompanyDto input)
  62. {
  63. await _userService.Update(input.CompanyUser);
  64. var entity = await _unitOfWork.Company.GetByIdAsync(input.Id);
  65. if (entity == null)
  66. throw new AppException(ExceptionEnum.RecordNotExist);
  67. MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company));
  68. await _unitOfWork.CompleteAsync();
  69. return input;
  70. }
  71. public override async Task Delete(long id)
  72. {
  73. var entity = await _unitOfWork.Company.GetByIdAsync(id);
  74. await _userService.Delete(entity.UserId); // delete user first
  75. await _unitOfWork.Company.DeleteAsync(entity);
  76. }
  77. }
  78. }