CompanyService.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. input.CompanyUser.UserType = UserTypeEnum.Business;
  50. var userResp = await _userService.Create(input.CompanyUser);
  51. input.UserId = userResp?.Id;
  52. var entity = MapperObject.Mapper.Map<Company>(input);
  53. if (entity is null)
  54. {
  55. throw new AppException(ExceptionEnum.MapperIssue);
  56. }
  57. var task = await _unitOfWork.Company.AddAsync(entity);
  58. await _unitOfWork.CompleteAsync();
  59. var response = MapperObject.Mapper.Map<CompanyDto>(task);
  60. return response;
  61. }
  62. public override async Task<CompanyDto> Update(CompanyDto input)
  63. {
  64. await _userService.Update(input.CompanyUser);
  65. var entity = await _unitOfWork.Company.GetByIdAsync(input.Id);
  66. if (entity == null)
  67. throw new AppException(ExceptionEnum.RecordNotExist);
  68. MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company));
  69. await _unitOfWork.CompleteAsync();
  70. return input;
  71. }
  72. public override async Task Delete(long id)
  73. {
  74. var entity = await _unitOfWork.Company.GetByIdAsync(id);
  75. await _userService.Delete(entity.UserId); // delete user first
  76. await _unitOfWork.Company.DeleteAsync(entity);
  77. }
  78. }
  79. }