Esempio n. 1
0
 public IHttpActionResult GetDatum(string datumTypeId, string id, ContentListShape?shape = null)
 {
     try {
         var datumId = id;
         var rctx    = ProtoCmsRuntimeContext.Current;
         var ct      = FindDatumType(datumTypeId);
         CheckUserHasPermissionToViewDatum(ct, rctx);
         var finder  = ct.FinderBase();
         var content = finder.FindByIdBase(datumId);
         if (content == null)
         {
             throw new HttpException(404, $"ProtoCMS: no {ct.Id} datum found with id '{datumId}'.");
         }
         var getter = new DatumShapedApiModelBuilder();
         var result = getter.GetDatumShaped(content, ct, rctx, shape ?? ContentListShape.Normal);
         return(JsonProto(result));
     } catch (HttpException hexc) {
         throw RestfulApiError(hexc);
     }
 }
        public DatumListApiResult(ProtoCmsRuntimeContext cmsContext, DatumType contentType, string search,
                                  string sortColumn, bool?isDescending, int offset, int limit, ContentListShape?shape,
                                  IDatumFinder finder = null)
        {
            finder = finder ?? contentType.FinderBase();
            var contentShape = shape ?? ContentListShape.Normal;

            if (!string.IsNullOrWhiteSpace(search))
            {
                finder = finder.SearchBase(search.Trim());
            }
            if (!string.IsNullOrWhiteSpace(sortColumn))
            {
                SortColumn = sortColumn;
                if (isDescending.HasValue)
                {
                    SortIsDescending = isDescending.Value;
                    finder           = finder.SortBase(sortColumn, isDescending.Value);
                }
                else
                {
                    SortIsDescending = contentType.DefaultSortDescending;
                    finder           = finder.SortBase(sortColumn, contentType.DefaultSortDescending);
                }
            }
            else
            {
                SortColumn = contentType.DefaultSortFieldName;
                if (isDescending.HasValue)
                {
                    SortIsDescending = isDescending.Value;
                    finder           = finder.SortBase(contentType.DefaultSortFieldName, isDescending.Value);
                }
                else
                {
                    SortIsDescending = contentType.DefaultSortDescending;
                    finder           = finder.SortBase(contentType.DefaultSortFieldName, contentType.DefaultSortDescending);
                }
            }

            if (offset < 0)
            {
                offset = 0;
            }
            if (limit < 1)
            {
                limit = 100;
            }
            var currentPage = offset / limit + 1;

            var paginated = finder.PaginatedBase(currentPage, limit);
            var rawData   = paginated.CurrentPageQueryableBase.DirectCastTo <IQueryable <object> >().ToArray();

            CurrentPage    = paginated.CurrentPage;
            TotalPage      = paginated.TotalPage;
            StartNumbering = paginated.StartNumbering;
            GlobalCount    = paginated.Count;
            CurrentCount   = rawData.Length;
            Offset         = paginated.Offset;
            Limit          = paginated.Limit;
            SearchKeywords = search;
            ResultShape    = $"{contentShape.ToString()} ({(int) contentShape})";
            var data   = new List <dynamic>();
            var getter = new DatumShapedApiModelBuilder();

            foreach (var rd in rawData)
            {
                data.Add(getter.GetDatumShaped(rd, contentType, cmsContext, contentShape));
            }
            Data = data.ToArray();
        }