Example #1
0
        public async Task <IActionResult> GetNews([FromQuery] int skip = 0, [FromQuery] int take = 10, [FromQuery] string query = "", [FromQuery] DateTime?startDate = null, [FromQuery] DateTime?endDate = null, [FromQuery] bool descending = true)
        {
            var result = HttpContext.Items["result"] as ResponseModel;

            var newsQuery = _newsRepo.GetAll();

            if (descending)
            {
                newsQuery = newsQuery.OrderByDescending(n => n.CreatedTime);
            }
            else
            {
                newsQuery = newsQuery.OrderBy(n => n.CreatedTime);
            }

            if (!query.IsNullOrEmpty())
            {
                newsQuery = newsQuery.Where(n => n.Title.Contains(query, StringComparison.InvariantCultureIgnoreCase));
            }

            if (startDate.HasValue)
            {
                newsQuery = newsQuery.Where(n => n.CreatedTime >= startDate);
            }
            if (endDate.HasValue)
            {
                newsQuery = newsQuery.Where(n => n.CreatedTime <= endDate);
            }

            var news = await newsQuery.Skip(skip).Take(take).ToListAsync();

            if (news.IsNullOrEmpty())
            {
                result.SuccessMessage = "No news have been found.";
                _logger.Log(LogPriority.Warning, "No news have been found.");
            }
            else
            {
                result.SuccessMessage = $"{news.Count} news have been found.";
                result.Result         = news;
            }

            return(Ok(result));
        }
        public async Task <List <News> > ObtainNews()
        {
            _logger.Log(LogPriority.Info, "Obtaining news...");
            List <News> obtainedNews = Obtain();

            _logger.Log(LogPriority.Info, $"Obtained {obtainedNews.Count} news.");

            if (!obtainedNews.IsNullOrEmpty())
            {
                List <News> insertedNews = await _newsRepo.AddMany(obtainedNews);

                int insertedCount = obtainedNews.Count - insertedNews.Count;
                int updatedCount  = obtainedNews.Count - insertedCount;

                _logger.Log(LogPriority.Info, $"Inserted {insertedCount} and updated {updatedCount} news.");
            }

            return(obtainedNews);
        }
Example #3
0
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.Exception != null && !context.ExceptionHandled)
            {
                var result = context.HttpContext.Items["result"] as BaseResponseModel;
                result.ErrorMessage = context.Exception.ToStr();

                var    request = context.HttpContext.Request;
                string source  = $"{request.Method} {request.Scheme}://{request.Host}{request.Path}{request.QueryString}";
                _logger.Log(LogPriority.Error, $"An error occured while performing a request to {source}: \r\n{context.Exception.ToStr()}");

                context.Exception        = null;
                context.ExceptionHandled = true;

                context.Result = new BadRequestObjectResult(result);
            }

            base.OnActionExecuted(context);
        }