Exemple #1
0
        public static PagedCollectionModel <T> GeneratePagedCollectionResult <T>(this IUrlHelper urlHelper, PagedCollectionResultModel <T> pagedCollectionResult, string method = "GET") where T : class, new()
        {
            var responseData = new PagedCollectionFactoryModel <T>(urlHelper, pagedCollectionResult.Skip,
                                                                   pagedCollectionResult.Take, pagedCollectionResult.Terms, pagedCollectionResult.Total,
                                                                   pagedCollectionResult.Items, pagedCollectionResult.AdditionalData, method)
                               .Generate();

            return(responseData);
        }
Exemple #2
0
        /// <summary>
        ///     <para> Create Content Result with response type is <see cref="PagedCollectionModel{LogEntity}" /> </para>
        ///     <para>
        ///         Search for <see cref="LogEntity.Id" />, <see cref="LogEntity.Message" />,
        ///         <see cref="LogEntity.Level" />, <see cref="LogEntity.CreatedTime" /> (with string
        ///         format is <c> "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFFK" </c>, ex: "2017-08-24T00:56:29.6271125+07:00")
        ///     </para>
        /// </summary>
        /// <returns></returns>
        /// <remarks>
        ///     <para>
        ///         Logger write Log with **message queue** so when create a log it *near real-time log*
        ///     </para>
        ///     <para>
        ///         Base on Http Request will return <c> ContentType XML </c> when Request Header
        ///         Accept or ContentType is XML, else return <c> ContentType Json </c>
        ///     </para>
        /// </remarks>
        public static ContentResult GetLogsContentResult(HttpContext context)
        {
            int skip = 0;

            if (context.Request.Query.TryGetValue("skip", out var skipStr))
            {
                if (int.TryParse(skipStr, out var skipInt))
                {
                    skip = skipInt;
                }
            }

            int take = 1000;

            if (context.Request.Query.TryGetValue("take", out var takeStr))
            {
                if (int.TryParse(takeStr, out var takeInt))
                {
                    take = takeInt;
                }
            }

            context.Request.Query.TryGetValue("terms", out var terms);

            Expression <Func <LogEntity, bool> > predicate = null;

            var termsNormalize = StringHelper.Normalize(terms);

            if (!string.IsNullOrWhiteSpace(termsNormalize))
            {
                predicate = x => x.Id.ToUpperInvariant().Contains(termsNormalize) ||
                            x.Message.ToUpperInvariant().Contains(termsNormalize) ||
                            x.Level.ToString().ToUpperInvariant().Contains(termsNormalize) ||
                            x.CreatedTime.ToString(Puppy.Core.Constants.StandardFormat.DateTimeOffSetFormat).Contains(termsNormalize);
            }

            var logs = Get(out long total, predicate: predicate, orders: x => x.CreatedTime, isOrderByDescending: true, skip: skip, take: take);

            ContentResult contentResult;

            string endpoint = context.Request.Host.Value + LoggerConfig.ViewLogUrl;

            var collectionModel = new PagedCollectionFactoryModel <LogEntity>(endpoint, skip, take, terms, total, logs, null, HttpMethod.Get.Method).Generate();

            if (context.Request.Headers[HeaderKey.Accept] == ContentType.Xml ||
                context.Request.Headers[HeaderKey.ContentType] == ContentType.Xml)
            {
                contentResult = new ContentResult
                {
                    ContentType = ContentType.Xml,
                    StatusCode  = (int)HttpStatusCode.OK,
                    Content     = XmlHelper.ToXmlStringViaJson(collectionModel)
                };
            }
            else
            {
                contentResult = new ContentResult
                {
                    ContentType = ContentType.Json,
                    StatusCode  = (int)HttpStatusCode.OK,
                    Content     = JsonConvert.SerializeObject(collectionModel, Puppy.Core.Constants.StandardFormat.JsonSerializerSettings)
                };
            }

            return(contentResult);
        }