Пример #1
0
        public async Task <ActionResult <Core2Group> > Get(string id)
        {
            StringValues requested = this.Request.Query[QueryKeys.Attributes];
            StringValues excluded  = this.Request.Query[QueryKeys.ExcludedAttributes];

            try
            {
                Core2Group group = (Core2Group)await this._provider.GetById(id).ConfigureAwait(false);

                if (group == null)
                {
                    ErrorResponse notFoundError = new ErrorResponse(string.Format(CultureInfo.InvariantCulture, ErrorDetail.NotFound, id), ErrorDetail.Status404);
                    return(this.NotFound(notFoundError));
                }

                group = ColumnsUtility.FilterAttributes(requested, excluded, group, this.alwaysRetuned);

                this.Response.ContentType = ControllerConstants.DefaultContentType;
                return(group);
            }
            catch (Exception e)
            {
                this._logger.LogError(e.ToString());
                ErrorResponse databaseException = new ErrorResponse(ErrorDetail.DatabaseError, ErrorDetail.Status500);
                return(this.StatusCode(500, databaseException));

                throw;
            }
        }
Пример #2
0
        public async Task <ListResponse <Resource> > Query(string query, IEnumerable <string> requested, IEnumerable <string> excluded)
        {
            IEnumerable <Core2Group> groups;

            if (!string.IsNullOrWhiteSpace(query))
            {
                groups = new FilterGroups(this._context).FilterGen(query);
            }
            else
            {
                groups = await this._context.CompleteGroups().ToListAsync().ConfigureAwait(false);
            }

            NameValueCollection  keyedValues = HttpUtility.ParseQueryString(query);
            IEnumerable <string> keys        = keyedValues.AllKeys;
            string countString = keyedValues[QueryKeys.Count];
            string startIndex  = keyedValues[QueryKeys.StartIndex];

            if (startIndex == null)
            {
                startIndex = ControllerConstants.DefaultStartIndexString;
            }

            int start = int.Parse(startIndex, CultureInfo.InvariantCulture);

            if (start < this._defaultStartIndex)
            {
                start = this._defaultStartIndex;
            }

            int?count = null;
            int total = groups.Count();

            groups = groups.OrderBy(d => d.DisplayName).Skip(start - 1);

            if (countString != null)
            {
                count  = int.Parse(countString, CultureInfo.InvariantCulture);
                groups = groups.Take(count.Value);
            }

            groups = groups.Select(u =>
                                   ColumnsUtility.FilterAttributes(requested, excluded, u, this._alwaysReturnedAttributes)).ToList();



            ListResponse <Resource> list = new ListResponse <Resource>()
            {
                TotalResults = total,
                StartIndex   = groups.Any() ? start : (int?)null,
                Resources    = groups,
            };

            if (count.HasValue)
            {
                list.ItemsPerPage = count.Value;
            }
            return(list);
        }