Exemplo n.º 1
0
 public IActionResult GetMembershipRequests([FromBody] MembershipRequestFilter filter)
 {
     try
     {
         var email = User.Claims.SingleOrDefault(c => c.Type == "email")?.Value;
         if (string.IsNullOrEmpty(email))
         {
             return(BadRequest("Authorization failed."));
         }
         return(Ok(_service.GetClubMembershipRequests(filter, email)));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Exemplo n.º 2
0
 public Page <MembershipRequestDto> GetClubMembershipRequests(MembershipRequestFilter filter, string email)
 {
     try
     {
         var existingMember = _unitOfWork.Users.Get(t => t.Email == email).FirstOrDefault();
         if (existingMember == null)
         {
             throw new ArgumentException("User not found");
         }
         var club = _unitOfWork.Clubs.GetAsQueryable(t => t.Id == filter.EntityId).Include(t => t.MembershipRequests).ThenInclude(t => t.User).FirstOrDefault();
         if (club == null)
         {
             throw new ArgumentException("Club not found");
         }
         var allRequests = string.IsNullOrEmpty(filter.SearchBy) ?
                           club.MembershipRequests.Where(t => t.Status == filter.Status).ToList() :
                           club.MembershipRequests.Where(t => t.Nickname.ToLower().Contains(filter.SearchBy.ToLower()) && t.Status == filter.Status).ToList();
         var skip         = filter.Page == 0 ? 0 : (filter.Page - 1) * (filter.PageSize ?? 25);
         var take         = filter.PageSize ?? 25;
         var orderByField = string.IsNullOrEmpty(filter.OrderBy) ? nameof(MembershipRequest.Created) : filter.OrderBy;
         var allCount     = allRequests.Count;
         var slice        = (filter.Asc ?
                             allRequests.OrderBy(t => typeof(MembershipRequest).GetProperty(orderByField).GetValue(t)) :
                             allRequests.OrderByDescending(t => typeof(MembershipRequest).GetProperty(orderByField).GetValue(t)))
                            .Skip(skip)
                            .Take(take);
         var page = new Page <MembershipRequestDto>(filter.Page, filter.PageSize ?? 25, allCount, slice.Select(t => t.ToDto()));
         return(page);
     }
     catch (ArgumentException)
     {
         throw;
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message);
         throw new Exception("Error while fetching club membership requests. Please try a bit later");
     }
 }