public IActionResult GetClubMembers([FromBody] ClubMembersFilter filter) { try { var email = User.Claims.SingleOrDefault(c => c.Type == "email")?.Value; if (string.IsNullOrEmpty(email)) { return(BadRequest("Authorization failed.")); } return(Ok(_service.GetClubMembers(filter))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public Page <ClubMemberSummary> GetClubMembers(ClubMembersFilter filter) { try { var club = _unitOfWork.Clubs.GetAsQueryable() .Include(t => t.Players) .ThenInclude(t => t.User) .FirstOrDefault(t => t.Id == filter.ClubId); if (club == null) { throw new ArgumentException("Club not found"); } var orderByField = string.IsNullOrEmpty(filter.OrderBy) ? nameof(ClubMemberSummary.Registered) : filter.OrderBy; var all = club.Players.Where(t => string.IsNullOrEmpty(filter.Search) || t.Nickname.ToLower().Contains(filter.Search)) .Select(t => new ClubMemberSummary { Country = t.User?.Country, Email = t.User?.Email, Registered = t.Registered, City = t.User?.City, UserId = t.UserId, IsActive = t.IsActive, Avatar = t.Avatar ?? t.User?.Avatar, FirstName = t.User?.FirstName, LastName = t.User?.LastName, Nickname = t.Nickname ?? t.User?.Nickname, PlayerId = t.Id }).ToList(); all = filter.Asc ? all.OrderBy(t => typeof(ClubMemberSummary).GetProperty(orderByField).GetValue(t)).ToList() : all.OrderByDescending(t => typeof(ClubMemberSummary).GetProperty(orderByField).GetValue(t)).ToList(); var slice = all.Skip((filter.Page - 1) * filter.PageSize ?? 25).Take(filter.PageSize ?? 25); return(new Page <ClubMemberSummary>(filter.Page, filter.PageSize ?? 25, all.Count, slice)); } catch (ArgumentException) { throw; } catch (Exception e) { _logger.LogError(e.Message); throw new Exception("Error while loading club members list. Please try a bit later"); } }