123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
-
- using MTWorkHR.Application.Models;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Entities;
- using MTWorkHR.Application.Mapper;
- using MTWorkHR.Core.Global;
- using Microsoft.EntityFrameworkCore;
- using System.Linq.Dynamic.Core;
- using MTWorkHR.Core.IDto;
- namespace MTWorkHR.Application.Services
- {
- public class UserTaskService : BaseService<UserTask, UserTaskDto, UserTaskDto>, IUserTaskService
- {
- private readonly IUnitOfWork _unitOfWork;
- //private readonly AppSettingsConfiguration _configuration;
- //private readonly GlobalInfo _globalInfo;
- private readonly IFileService _fileService;
- private readonly GlobalInfo _globalInfo;
- public UserTaskService(IUnitOfWork unitOfWork, IFileService fileService, GlobalInfo globalInfo) : base(unitOfWork)
- {
- _unitOfWork = unitOfWork;
- _fileService = fileService;
- _globalInfo = globalInfo;
- }
- public override async Task<UserTaskDto> GetById(long id)
- {
- var entity = await _unitOfWork.UserTask.GetByIdWithAllChildren(id);
- var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
- return response;
- }
- public async Task<UserTaskDto> GetByUserId(string userId)
- {
- var entity = await _unitOfWork.UserTask.GetByUserIdWithAllChildren(userId);
- var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
- return response;
- }
- public override async Task<PagingResultDto<UserTaskDto>> GetAll(PagingInputDto PagingInputDto)
- {
- var res = await _unitOfWork.UserTask.GetAllWithChildrenAsync();
- var query = res.Item1;
- if (_globalInfo.UserType != UserTypeEnum.Business)
- {
- query = query.Where(m => m.AssignedUserId == _globalInfo.UserId);
- }
- if (PagingInputDto.HiddenFilter != null)
- {
- query = query.Where(PagingInputDto.HiddenFilter);
- }
- if (PagingInputDto.Filter != null)
- {
- var filter = PagingInputDto.Filter;
- query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
- }
- var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
- var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
- var total = await query.CountAsync();
- var list = MapperObject.Mapper
- .Map<IList<UserTaskDto>>(await page.ToListAsync());
- var response = new PagingResultDto<UserTaskDto>
- {
- Result = list,
- Total = total
- };
- return response;
- }
- public override async Task<UserTaskDto> Create(UserTaskDto input)
- {
- if(input.TaskAttachments?.Count > 0)
- if ( !await _fileService.CopyFileToActualFolder(input.TaskAttachments.ToList()))
- throw new AppException(ExceptionEnum.CouldNotMoveFiles);
- var entity = MapperObject.Mapper.Map<UserTask>(input);
- if (entity is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- var task = await _unitOfWork.UserTask.AddAsync(entity);
- await _unitOfWork.CompleteAsync();
- var response = MapperObject.Mapper.Map<UserTaskDto>(task);
- return response;
- }
- public override async Task<UserTaskDto> Update(UserTaskDto input)
- {
- var entitiy = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
- if (entitiy == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- MapperObject.Mapper.Map(input, entitiy, typeof(UserTaskDto), typeof(UserTask));
- await _unitOfWork.CompleteAsync();
- return input;
- }
- }
- }
|