Skip to content

Commit

Permalink
add search query to member
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-shirbandi committed Nov 9, 2021
1 parent 279731c commit 9478f6f
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

using System.ComponentModel.DataAnnotations;
using TaskoMask.Application.Core.Resources;

namespace TaskoMask.Application.Core.Dtos.Team.Members
{
public class MemberOutputDto : MemberBasicInfoDto
{
/// <summary>
/// Member Organizations count as an owner or as an invited member
/// </summary>
[Display(Name = nameof(ApplicationMetadata.OrganizationsCount), ResourceType = typeof(ApplicationMetadata))]
public long OrganizationsCount { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@
using TaskoMask.Application.Team.Members.Queries.Models;
using TaskoMask.Application.Core.Dtos.Team.Members;
using TaskoMask.Domain.Team.Data;
using TaskoMask.Application.Core.Helpers;
using System.Collections.Generic;

namespace TaskoMask.Application.Team.Members.Queries.Handlers
{
public class MemberQueryHandlers : BaseQueryHandler,
IRequestHandler<GetMemberByIdQuery, MemberBasicInfoDto>
IRequestHandler<GetMemberByIdQuery, MemberBasicInfoDto>,
IRequestHandler<SearchMembersQuery, PublicPaginatedListReturnType<MemberOutputDto>>
{
#region Fields

private readonly IMemberRepository _memberRepository;
private readonly IInvitationRepository _invitationRepository;
private readonly IOrganizationRepository _organizationRepository;

#endregion

#region Ctors

public MemberQueryHandlers(IMemberRepository memberRepository, IDomainNotificationHandler notifications, IMapper mapper ) : base(mapper, notifications)
public MemberQueryHandlers(IMemberRepository memberRepository, IDomainNotificationHandler notifications, IMapper mapper, IInvitationRepository invitationRepository, IOrganizationRepository organizationRepository) : base(mapper, notifications)
{
_memberRepository = memberRepository;
_invitationRepository = invitationRepository;
_organizationRepository = organizationRepository;
}

#endregion
Expand All @@ -47,9 +54,38 @@ public async Task<MemberBasicInfoDto> Handle(GetMemberByIdQuery request, Cancell
return _mapper.Map<MemberBasicInfoDto>(member);
}

#endregion


/// <summary>
///
/// </summary>
public async Task<PublicPaginatedListReturnType<MemberOutputDto>> Handle(SearchMembersQuery request, CancellationToken cancellationToken)
{
var members = _memberRepository.Search(request.Page,request.RecordsPerPage,request.Term, out var pageNumber, out var totalCount);
var membersDto = _mapper.Map<IEnumerable<MemberOutputDto>>(members);

foreach (var item in membersDto)
{
//As an invited member to organizations
item.OrganizationsCount = await _invitationRepository.OrganizationsCountByInvitedMemberIdAsync(item.Id);

//As an owner of organizations
item.OrganizationsCount += await _organizationRepository.CountByOwnerMemberIdAsync(item.Id);
}


return new PublicPaginatedListReturnType<MemberOutputDto>
{
TotalCount = totalCount,
PageNumber = pageNumber,
Items = membersDto
};
}



#endregion

#region Private Methods


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using TaskoMask.Application.Core.Dtos.Team.Members;
using TaskoMask.Application.Core.Helpers;
using TaskoMask.Application.Core.Queries;

namespace TaskoMask.Application.Team.Members.Queries.Models
{
public class SearchMembersQuery:BaseQuery<PublicPaginatedListReturnType<MemberOutputDto>>
{
public SearchMembersQuery(int page, int recordsPerPage, string term)
{
Page = page;
RecordsPerPage = recordsPerPage;
Term = term;
}

public int Page { get; set; }
public int RecordsPerPage { get; set; }
public string Term { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TaskoMask.Domain.Team.Data;
Expand All @@ -12,7 +13,7 @@ public class MemberRepository : UserRepository<Member>, IMemberRepository
{
#region Fields


#endregion

#region Ctors
Expand All @@ -26,6 +27,44 @@ public MemberRepository(IMongoDbContext dbContext) : base(dbContext)
#region Public Methods


/// <summary>
///
/// </summary>
public IEnumerable<Member> Search(int page, int recordsPerPage, string term, out int pageSize, out int totalItemCount)
{
var queryable = _users.AsQueryable();

#region By term

if (!string.IsNullOrEmpty(term))
{
queryable = queryable.Where(p => p.DisplayName.Contains(term) || p.Email.Contains(term) || p.PhoneNumber.Contains(term));
}

#endregion

#region SortOrder

queryable = queryable.OrderByDescending(p => p.Id);

#endregion

#region Skip Take

totalItemCount = queryable.CountAsync().Result;
pageSize = (int)Math.Ceiling((double)totalItemCount / recordsPerPage);

page = page > pageSize || page < 1 ? 1 : page;


var skiped = (page - 1) * recordsPerPage;
queryable = queryable.Skip(skiped).Take(recordsPerPage);


#endregion

return queryable.ToList();
}


#endregion
Expand Down
12 changes: 12 additions & 0 deletions Src/Libraries/3-Domain/Domain/Team/Data/IInvitationRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using TaskoMask.Domain.Core.Data;
using TaskoMask.Domain.Team.Entities;

namespace TaskoMask.Domain.Team.Data
{
public interface IInvitationRepository : IBaseRepository<Invitation>
{
Task<int> OrganizationsCountByInvitedMemberIdAsync(string invitedMemberId);
}
}
4 changes: 3 additions & 1 deletion Src/Libraries/3-Domain/Domain/Team/Data/IMemberRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using TaskoMask.Domain.Core.Data;
using System.Collections.Generic;
using TaskoMask.Domain.Core.Data;
using TaskoMask.Domain.Team.Entities;

namespace TaskoMask.Domain.Team.Data
{
public interface IMemberRepository : IUserRepository<Member>
{
IEnumerable<Member> Search(int page, int recordsPerPage, string term, out int pageSize, out int totalItemCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface IOrganizationRepository : IBaseRepository<Organization>
{
Task<IEnumerable<Organization>> GetListByOwnerMemberIdAsync(string ownerMemberId);
Task<bool> ExistByNameAsync(string id, string name);
Task<long> CountByOwnerMemberIdAsync(string ownerMemberId);

}
}
5 changes: 5 additions & 0 deletions Src/Libraries/3-Domain/Domain/Team/Entities/Invitation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public Invitation(string inviterMemberId, string invitedMemberId, string[] board
/// </summary>
public string[] BoardsId { get; private set; }

/// <summary>
///
/// </summary>
public string OrganizationId { get; private set; }


#endregion

Expand Down

0 comments on commit 9478f6f

Please sign in to comment.