|
@@ -14,23 +14,54 @@ using MTWorkHR.Core.Email;
|
|
|
using MTWorkHR.Core.Entities;
|
|
|
using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
|
using System.Linq;
|
|
|
+using System.Linq.Dynamic.Core;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
|
{
|
|
|
- public class TeamService : BaseService<Team, TeamDto, TeamDto>, ITeamService
|
|
|
+ public class TeamService : BaseService<Team, TeamDto, TeamAllDto>, ITeamService
|
|
|
{
|
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
+ private readonly IUserService _userService;
|
|
|
|
|
|
- public TeamService(IUnitOfWork unitOfWork):base(unitOfWork)
|
|
|
+ public TeamService(IUnitOfWork unitOfWork, IUserService userService) : base(unitOfWork)
|
|
|
{
|
|
|
_unitOfWork = unitOfWork;
|
|
|
+ _userService = userService;
|
|
|
}
|
|
|
|
|
|
+ public override async Task<PagingResultDto<TeamAllDto>> GetAll(PagingInputDto PagingInputDto)
|
|
|
+ {
|
|
|
+ var res = await _unitOfWork.Team.GetAllWithChildrenAsync();
|
|
|
+ var query = res.Item1;
|
|
|
+ if (PagingInputDto.Filter != null)
|
|
|
+ {
|
|
|
+ var filter = PagingInputDto.Filter;
|
|
|
+ query = query.Where(u => u.NameAr.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<TeamAllDto>>(await page.ToListAsync());
|
|
|
+
|
|
|
+ var response = new PagingResultDto<TeamAllDto>
|
|
|
+ {
|
|
|
+ Result = list,
|
|
|
+ Total = total
|
|
|
+ };
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
public override async Task<TeamDto> GetById(long id)
|
|
|
{
|
|
|
var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
|
|
|
var response = MapperObject.Mapper.Map<TeamDto>(entity);
|
|
|
+ if(response != null)
|
|
|
+ response.TeamUserIds = response.TeamUsers != null ? response.TeamUsers?.Select(u => u.AssignedUserId).ToList() : new List<string>();
|
|
|
return response;
|
|
|
}
|
|
|
public override async Task<TeamDto> Create(TeamDto input)
|
|
@@ -41,18 +72,24 @@ namespace MTWorkHR.Application.Services
|
|
|
{
|
|
|
throw new AppException(ExceptionEnum.MapperIssue);
|
|
|
}
|
|
|
- entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
|
|
|
+ entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s,AssignedUserName = GetUserName(s).Result, IsAdmin = false }).ToList();
|
|
|
|
|
|
var team = await _unitOfWork.Team.AddAsync(entity);
|
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
|
|
var response = MapperObject.Mapper.Map<TeamDto>(team);
|
|
|
+ response.TeamUserIds = input.TeamUserIds;
|
|
|
return response;
|
|
|
}
|
|
|
-
|
|
|
+ private async Task<string> GetUserName(string userId)
|
|
|
+ {
|
|
|
+ var entity = await _userService.GetUserById(userId);
|
|
|
+ var name = entity == null ? "" : entity.FirstName + " " + entity.LastName;
|
|
|
+ return name;
|
|
|
+ }
|
|
|
public override async Task<TeamDto> Update(TeamDto input)
|
|
|
{
|
|
|
- var entity = await _unitOfWork.Team.GetByIdAsync(input.Id);
|
|
|
+ var entity = await _unitOfWork.Team.GetByIdWithAllChildren(input.Id);
|
|
|
if (entity is null)
|
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
entity.NameAr = input.NameAr;
|
|
@@ -60,19 +97,21 @@ namespace MTWorkHR.Application.Services
|
|
|
var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
|
|
|
|
|
|
var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
|
|
|
- var newTeamUsers = NewItems?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
|
|
|
+ var newTeamUsers = NewItems?.Select(s => new TeamUser {TeamId = input.Id, AssignedUserId = s, AssignedUserName = GetUserName(s).Result, IsAdmin = false }).ToList();
|
|
|
var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
|
|
|
+ _unitOfWork.BeginTran();
|
|
|
foreach (var delUser in DeletedItems)
|
|
|
{
|
|
|
var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
|
|
|
- if(removeItem != null) entity.TeamUsers.Remove(removeItem);
|
|
|
+ if(removeItem != null) _unitOfWork.TeamUser.DeleteAsync(removeItem);
|
|
|
}
|
|
|
foreach (var newUsr in newTeamUsers)
|
|
|
{
|
|
|
- entity.TeamUsers.Add(newUsr);
|
|
|
+ _unitOfWork.TeamUser.AddAsync(newUsr);
|
|
|
}
|
|
|
|
|
|
await _unitOfWork.CompleteAsync();
|
|
|
+ _unitOfWork.CommitTran();
|
|
|
|
|
|
var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
|
|
|
return response;
|