Esempio n. 1
0
        public async Task <Article> Execute(GetArticleById query)
        {
            using IDbConnection conn = DbConnection.GetDbConnection();

            string CacheKey   = $"Article-{query.Id}";
            string CacheValue = await _distributedCache.GetStringAsync(CacheKey);

            var sql = @$ "select * from Articles 
                         where Id = {query.Id}";

            Article Article;

            conn.Close();

            if (CacheValue != null)
            {
                try
                {
                    Article = JsonConvert.DeserializeObject <Article>(JsonConvert.DeserializeObject(CacheValue).ToString(), new JsonSerializerSettings {
                        NullValueHandling   = NullValueHandling.Ignore,
                        ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
                        ContractResolver    = new PrivateResolver()
                    });
                }
                catch (Exception e)
                {
                    log.LogError(e.Message);
                    log.LogError(e.StackTrace);
                    log.LogError(e.Source);
                    log.LogError(e.InnerException.Message);
                    return(null);
                }
            }

            else
            {
                Article = (await conn.QueryAsync <Article>(sql)).First();
                await Task.Run(async() => await _distributedCache.AddCache(CacheKey, JsonConvert.SerializeObject(Article)));
            }

            db.Entry(Article).State = EntityState.Unchanged;
            db.Entry(Article).Collection(u => u.Votes).Load();
            db.Entry(Article).Collection(u => u.Comments).Load();
            db.Entry(Article).Reference(u => u.User).Load();

            return(Article);
        }
        public async Task <ArticleViewModel> Execute(GetViewArticleById query)
        {
            using IDbConnection conn = context.GetDbConnection();

            string CacheKey   = $"ViewArticle-{query.Id}";
            string CacheValue = await _distributedCache.GetStringAsync(CacheKey);

            ArticleViewModel article;

            if (CacheValue != null)
            {
                article = JsonConvert.DeserializeObject <ArticleViewModel>(CacheValue);
            }

            else
            {
                var sql = @$ "select * from Articles a
                             JOIN AspNetUsers u
                             ON a.UserId = u.Id
                             where a.Id = {query.Id}";

                article = (await conn.QueryAsync <Article, User, ArticleViewModel>
                               (sql, (article, user) => {
                    var articleView = _mapper.Map <ArticleViewModel>(article);
                    articleView.User = user;
                    return(articleView);
                })
                           ).First();

                await Task.Run(async() => await _distributedCache.AddCache(CacheKey, article));
            }

            article.Votes = conn.Query <Vote>(@$ "SELECT * FROM Votes WHERE ArticleId = {query.Id}").ToList();
            var Comments = conn.Query <Comment>(@$ "SELECT * FROM Comments WHERE ArticleId = {query.Id}").ToList();

            foreach (var a in Comments)
            {
                db.Entry(a).State = EntityState.Unchanged;
                db.Entry(a).Reference(u => u.User).Load();
            }
            article.Comments = Comments;
            conn.Close();
            return(article);
            //return _mapper.Map<ArticleViewModel>(await db.Articles.FindAsync(query.Id));
        }