using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.WebUtilities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using MTWorkHR.Application.Identity; using MTWorkHR.Application.Mapper; using MTWorkHR.Application.Models; using MTWorkHR.Core.Global; using MTWorkHR.Core.IRepositories; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Application.Services.Interfaces; using MTWorkHR.Core.Email; using MTWorkHR.Core.Entities; using MTWorkHR.Infrastructure.UnitOfWorks; using MTWorkHR.Infrastructure.Entities; using System.Transactions; using MTWorkHR.Core.Entities.Base; namespace MTWorkHR.Application.Services { public class CompanyService :BaseService, ICompanyService { private readonly IUnitOfWork _unitOfWork; private readonly AppSettingsConfiguration _configuration; private readonly GlobalInfo _globalInfo; private readonly IUserService _userService; public CompanyService(IUnitOfWork unitOfWork, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IUserService userService) : base(unitOfWork) { _unitOfWork = unitOfWork; _configuration = configuration; _globalInfo = globalInfo; _userService = userService; } public override async Task GetById(long CompanyId) { var entity = await _unitOfWork.Company.GetByIdAsync(CompanyId); var companyResponse = MapperObject.Mapper.Map(entity); var userDto = await _userService.GetById(entity.UserId); companyResponse.CompanyUser = userDto; return companyResponse; } public async Task> GetAllCompanies() { var Companys = await _unitOfWork.Company.GetAllAsync(); var response = MapperObject.Mapper.Map>(Companys); return response; } public override async Task Create(CompanyDto input) { input.CompanyUser.UserType = UserTypeEnum.Business; var userResp = await _userService.Create(input.CompanyUser); input.UserId = userResp?.Id; var entity = MapperObject.Mapper.Map(input); if (entity is null) { throw new AppException(ExceptionEnum.MapperIssue); } var task = await _unitOfWork.Company.AddAsync(entity); await _unitOfWork.CompleteAsync(); var response = MapperObject.Mapper.Map(task); return response; } public override async Task Update(CompanyDto input) { await _userService.Update(input.CompanyUser); var entity = await _unitOfWork.Company.GetByIdAsync(input.Id); if (entity == null) throw new AppException(ExceptionEnum.RecordNotExist); MapperObject.Mapper.Map(input, entity, typeof(CompanyDto), typeof(Company)); await _unitOfWork.CompleteAsync(); return input; } public override async Task Delete(long id) { var entity = await _unitOfWork.Company.GetByIdAsync(id); await _userService.Delete(entity.UserId); // delete user first await _unitOfWork.Company.DeleteAsync(entity); } } }