123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
-
- using MTWorkHR.Application.Models;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Entities.Base;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.IRepositories.Base;
- using MTWorkHR.Core.UnitOfWork;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace MTWorkHR.Application.Services
- {
- public class BaseService<TEntity, TDto, TGetAllDto> : IService<TEntity, TDto, TGetAllDto>
- where TEntity : class
- where TDto : EntityDto
- where TGetAllDto : class
- {
- private readonly IUnitOfWork unitOfWork;
- private readonly IRepository<TEntity> repository;
- public BaseService(IUnitOfWork _unitOfWork)
- {
- unitOfWork = _unitOfWork;
- repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
- }
- //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
- //{
- // throw new NotImplementedException();
- //}
- public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
- {
- var result = await repository.GetAllAsync();
- var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
- var response = new PagingResultDto<TGetAllDto>
- {
- Result = list,
- Total = result.Item2
- };
- return response;
- }
- public virtual async Task<TDto> GetById(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
- return response;
- }
- public virtual async Task Delete(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- if (entity == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
- if (isSoftDelete)
- {
- ((ISoftDelete)entity).IsDeleted = true;
- }
- else
- {
- await repository.DeleteAsync(entity);
- }
- await unitOfWork.CompleteAsync();
- }
- public virtual async Task<TDto> Create(TDto input)
- {
- var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
- if (employeeEntitiy is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- var newEmployee = await repository.AddAsync(employeeEntitiy);
- await unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
- return response;
- }
- public virtual async Task<TDto> Update(TDto input)
- {
- var entitiy = await repository.GetByIdAsync(input.Id);
- if (entitiy == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
- await unitOfWork.CompleteAsync();
- return input;
- }
- }
- public class BaseService<TEntity, TDto, TUpdatDto, TGetAllDto> : IService<TEntity, TDto, TUpdatDto, TGetAllDto>
- where TEntity : class
- where TDto : EntityDto
- where TUpdatDto : EntityDto
- where TGetAllDto : class
- {
- private readonly IUnitOfWork unitOfWork;
- private readonly IRepository<TEntity> repository;
- public BaseService(IUnitOfWork _unitOfWork)
- {
- unitOfWork = _unitOfWork;
- repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
- }
- //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
- //{
- // throw new NotImplementedException();
- //}
- public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
- {
- var result = await repository.GetAllAsync();
- var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
- var response = new PagingResultDto<TGetAllDto>
- {
- Result = list,
- Total = result.Item2
- };
- return response;
- }
- public virtual async Task<TDto> GetById(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
- return response;
- }
- public virtual async Task Delete(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- if (entity == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
- if (isSoftDelete)
- {
- ((ISoftDelete)entity).IsDeleted = true;
- }
- else
- {
- await repository.DeleteAsync(entity);
- }
- await unitOfWork.CompleteAsync();
- }
- public virtual async Task<TDto> Create(TDto input)
- {
- var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
- if (employeeEntitiy is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- var newEmployee = await repository.AddAsync(employeeEntitiy);
- await unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
- return response;
- }
- public virtual async Task<TDto> Update(TUpdatDto input)
- {
- var entitiy = await repository.GetByIdAsync(input.Id);
- if (entitiy == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
- await unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TDto>(entitiy);
- return response;
- }
- }
- public class BaseService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto> : IService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto>
- where TEntity : class
- where TGetDto : EntityDto
- where TCreateDto : EntityDto
- where TUpdatDto : EntityDto
- where TGetAllDto : class
- {
- private readonly IUnitOfWork unitOfWork;
- private readonly IRepository<TEntity> repository;
- public BaseService(IUnitOfWork _unitOfWork)
- {
- unitOfWork = _unitOfWork;
- repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
- }
- //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
- //{
- // throw new NotImplementedException();
- //}
- public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
- {
- var result = await repository.GetAllAsync();
- var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
- var response = new PagingResultDto<TGetAllDto>
- {
- Result = list,
- Total = result.Item2
- };
- return response;
- }
- public virtual async Task<TGetDto> GetById(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entity);
- return response;
- }
- public virtual async Task Delete(long id)
- {
- var entity = await repository.GetByIdAsync(id);
- if (entity == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
- if (isSoftDelete)
- {
- ((ISoftDelete)entity).IsDeleted = true;
- }
- else
- {
- await repository.DeleteAsync(entity);
- }
- await unitOfWork.CompleteAsync();
- }
- public virtual async Task<TGetDto> Create(TCreateDto input)
- {
- var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
- if (employeeEntitiy is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- var newEmployee = await repository.AddAsync(employeeEntitiy);
- await unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TGetDto>(newEmployee);
- return response;
- }
- public virtual async Task<TGetDto> Update(TUpdatDto input)
- {
- var entitiy = await repository.GetByIdAsync(input.Id);
- if (entitiy == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TGetDto), typeof(TEntity));
- await unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entitiy);
- return response;
- }
- }
- }
|