Esempio n. 1
0
        public async Task <IActionResult> Share([FromQuery] ArticlePrimaryKey key)
        {
            try
            {
                await _service.Share(key).CAF();

                return(Ok());
            }
            catch (ArticleNotFoundException)
            {
                _logger.LogWarning("Attempt to share a non-existent article: {ArticleKey}", key.ToString());
                return(BadRequest());
            }
        }
Esempio n. 2
0
        public async Task AddOrUpdateTagsPerArticle(ArticlePrimaryKey articlePrimaryKey, IEnumerable <string> titles)
        {
            var article = await(await _client.Article().FindAsync(x => x.Id == articlePrimaryKey.Id).CAF())
                          .FirstOrDefaultAsync().CAF();

            if (article?.TagsIds?.Count > 0)
            {
                await UpdateTags(article, titles).CAF();
            }
            else
            {
                await AddTags(article, titles).CAF();
            }

            await _client.Article().FindOneAndReplaceAsync(x => x.Id == articlePrimaryKey.Id, article).CAF();
        }
Esempio n. 3
0
        public async Task <IActionResult> UpdateArticleView([FromRoute] string articleId)
        {
            var    key    = new ArticlePrimaryKey(articleId);
            string userId = this.GetViewerUserId();

            try
            {
                bool authorizedFor = await AuthorizedFor(key, userId).CAF();

                if (!authorizedFor)
                {
                    return(Forbid());
                }
            }
            catch (ArticleNotFoundException)
            {
                return(NotFound());
            }

            var articleVm = await _articleService.Get(key, userId).CAF();

            if (articleVm is null)
            {
                return(NotFound());
            }

            ViewBag.Key = key;

            var vm = _mapper.Map <UpdateArticleViewModel>(articleVm.Article);

            vm.TagsAsString = ITagService.TagsToString(articleVm.Tags);

            var series = await _articleService.GetSeriesFor(userId).CAF();

            // A finished series will not be included in the list above
            bool currSeriesIsFinished = articleVm.Article.SeriesId is not null &&
                                        series.All(x => x.Id != articleVm.Article.SeriesId);

            if (currSeriesIsFinished)
            {
                ViewBag.CurrentSeries = await _articleService.GetSeries(articleVm.Article.SeriesId).CAF();
            }

            ViewBag.Series = series;
            return(View("UpdateArticle", vm));
        }
Esempio n. 4
0
        public async Task AddOrUpdateTagsPerArticle(ArticlePrimaryKey articlePrimaryKey, IEnumerable <string> titles)
        {
            var article = await _context.Article.Where(x => x.CreatedYear == articlePrimaryKey.CreatedYear &&
                                                       x.TitleShrinked == articlePrimaryKey.TitleShrinked)
                          .Include(x => x.Tags)
                          .FirstOrDefaultAsync().ConfigureAwait(false);

            if (article.Tags.Count > 0)
            {
                await UpdateTags(article, titles).ConfigureAwait(false);
            }
            else
            {
                await AddTags(article, titles).ConfigureAwait(false);
            }

            await _context.SaveChangesAsync().ConfigureAwait(false);
        }
Esempio n. 5
0
        public async Task <IActionResult> Index([FromRoute] string articleId)
        {
            var    key          = new ArticlePrimaryKey(articleId);
            string viewerUserId = this.GetViewerUserId();

            var article = await _service.Get(key, viewerUserId).CAF();

            if (article is null)
            {
                _logger.LogInformation("Asked for article that doesn't exist with key: {articleId}", articleId);
                return(Redirect("/"));
            }

            if (article.Article.ForceFullyUnlisted && string.IsNullOrEmpty(viewerUserId))
            {
                return(NotFound());
            }

            return(View("Article", article));
        }
Esempio n. 6
0
        public async ValueTask <IActionResult> UpdateArticlePost([FromRoute] string articleId,
                                                                 [FromForm] UpdateArticleViewModel viewModel, [FromServices] ITagService tagService)
        {
            var key = new ArticlePrimaryKey(articleId);

            if (!ModelState.IsValid)
            {
                return(View("UpdateArticle", viewModel));
            }

            string userId = this.GetViewerUserId();

            try
            {
                bool authorizedFor = await AuthorizedFor(key, userId).CAF();

                if (!authorizedFor)
                {
                    return(Forbid());
                }
            }
            catch (ArticleNotFoundException)
            {
                _logger.LogInformation("User: {UserId} attempted to update a non-existing article with key: {articleId}",
                                       userId, key.Id);
                return(NotFound());
            }

            await _articleService.Update(userId, key, viewModel).CAF();

            await tagService.AddOrUpdateTagsPerArticle(key, viewModel.TagsAsString).CAF();

            _logger.LogInformation("Updated an article by user: {UserId} with article key: {articleId}",
                                   userId, key.Id);

            return(RedirectToAction("UpdateArticleView", new
            {
                articleId = key.Id
            }));
        }
Esempio n. 7
0
 private async Task <bool> AuthorizedFor(ArticlePrimaryKey key, string viewerUserId)
 {
     return(await _articleService.AuthorizedFor(key, viewerUserId).ConfigureAwait(false) ||
            User.IsInRole(UserRoles.AdminRole));
 }