Example #1
0
        public async Task <ActionResult> Get(GetPostModel id)
        {
            Post          model;
            PostViewModel newModel;

            try
            {
                if (!ModelState.IsValid || string.IsNullOrEmpty(id.Id))
                {
                    return(BadRequest());
                }

                var redis = await _redis.GetRedis <Post>(id.Id);

                if (redis == null)
                {
                    model = await _redis.SetRedis(await _postContext.GetPost(id.Id), id.Id);

                    if (model == null)
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    newModel
                        = _mapper.Map <Post, PostViewModel>(redis);
                    newModel.CommentCount = await _postContext.GetCommentCount(id.Id);

                    return(Ok(newModel));
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.InnerException?.ToString() ?? e.Message);
                _mailService.SendMail(string.Empty, e.InnerException?.ToString() ?? e.Message, "error");
                return(StatusCode(500, "Internal Server Error"));
            }

            newModel
                = _mapper.Map <Post, PostViewModel>(model);
            newModel.CommentCount = await _postContext.GetCommentCount(id.Id);

            return(Ok(newModel));
        }