示例#1
0
        public async Task <IActionResult> Get([FromQuery] ScoresQuery filter = null)
        {
            if (filter?.IsApproved != true)
            {
                var isAdmin = await _auth.AuthorizeAsync(User, "AppAdmin").ConfigureAwait(false);

                // if the user is requesting unapproved scores, they must be an admin
                if (!isAdmin.Succeeded)
                {
                    return(Unauthorized());
                }
            }

            return(Ok(GetScores(filter)));
        }
示例#2
0
        public IQueryable <ScoreViewModel> GetScores(ScoresQuery filter = null)
        {
            var isApproved    = filter?.IsApproved;
            var approvedSince = filter?.ApprovedSince;
            var top           = filter?.Top;
            var orderBy       = filter?.OrderBy;
            var boardId       = filter?.BoardId;
            var createdSince  = filter?.CreatedSince;

            var query = _ctx.Scores.AsQueryable();

            if (orderBy is string orderString)
            {
                var order   = orderString.First();
                var noOrder = string.Concat(orderString.Skip(1));

                if (order == '-')
                {
                    query = query.OrderByDescending(noOrder);
                }
                else if (order == '+')
                {
                    query = query.OrderBy(noOrder);
                }
                // if a direction was not provided, simply order by ascending
                else
                {
                    query = query.OrderBy(orderString);
                }
            }
            // no order by provided, order by most recent first
            else
            {
                query = query.OrderByDescending(s => s.CreatedDate);
            }


            if (isApproved is bool approved)
            {
                if (approved)
                {
                    query = query.Where(s => s.ApprovedDate != null);
                }
                else
                {
                    query = query.Where(s => s.ApprovedDate == null);
                }
            }

            if (boardId is string bid)
            {
                query = query.Where(s => s.BoardId == bid);
            }

            if (createdSince is DateTimeOffset createdDate)
            {
                query = query.Where(s => s.CreatedDate > createdDate);
            }

            if (approvedSince is DateTimeOffset approvedDate)
            {
                query = query.Where(s => s.ApprovedDate > approvedSince);
            }

            if (top is int t)
            {
                query = query.Take(t);
            }

            return(_mapper.ProjectTo <ScoreViewModel>(query));
        }