示例#1
0
 private IActionResult CreatedAtAction(ArticleProjection projection)
 {
     return(CreatedAtAction(
                nameof(GetBySlug),
                new { slug = projection.Slug },
                projection));
 }
        public IHttpActionResult Create(ArticleProjection articleProjection)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest("Invalid article!"));
            }

            var tags       = this.GetTags(articleProjection);
            var categoryId = this.GetCategoryId(articleProjection.Category);
            var article    = new Article()
            {
                Title       = articleProjection.Title,
                Content     = articleProjection.Content,
                DateCreated = DateTime.Now,
                CategoryID  = categoryId,
                Tags        = tags,
                AuthorID    = this.GetUserId()
            };

            this.ForumSystemData.Articles.Add(article);
            this.ForumSystemData.SaveChanges();

            articleProjection.ID          = article.ID;
            articleProjection.DateCreated = article.DateCreated;
            articleProjection.Tags        = article.Tags.AsQueryable().Select(TagProjection.FromTag).ToList();

            return(this.Ok(articleProjection));
        }
        private ICollection <Tag> GetTags(ArticleProjection article)
        {
            var allTags = new HashSet <string>();

            allTags.UnionWith(article.Tags.Select(t => t.Name));
            allTags.UnionWith(article.Title.Split(' '));

            var tags = new HashSet <Tag>();

            foreach (var tagName in allTags)
            {
                var tag = this.ForumSystemData.Tags
                          .All()
                          .Where(t => t.Name == tagName)
                          .FirstOrDefault();

                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagName
                    };

                    this.ForumSystemData.Tags.Add(tag);
                    this.ForumSystemData.SaveChanges();
                }

                tags.Add(tag);
            }

            return(tags);
        }
示例#4
0
        protected int GetLikesValue(ArticleProjection item)
        {
            var article    = this.context.Articles.Find(item.ArticleId);
            int likesCount = article.Likes.Count(l => l.Value == true);
            int hatesCount = article.Likes.Count(l => l.Value == false);

            return(likesCount - hatesCount);
        }
示例#5
0
        protected bool?GetUserVote(ArticleProjection item)
        {
            var    article = this.context.Articles.Find(item.ArticleId);
            string userId  = this.userIdProvider.GetUserId();
            var    like    = article.Likes.FirstOrDefault(l => l.UserId == userId);

            if (like == null)
            {
                return(null);
            }

            return(like.Value);
        }
示例#6
0
        public void ListViewArticles_InsertItem()
        {
            var articleProjection = new ArticleProjection();

            this.TryUpdateModel(articleProjection);
            var categoryId = int.Parse(articleProjection.CategoryName);

            if (string.IsNullOrEmpty(articleProjection.Title) || string.IsNullOrEmpty(articleProjection.Content))
            {
                ErrorSuccessNotifier.AddMessage(new NotificationMessage()
                {
                    Text     = "Cannot create article with emptry title/content.",
                    AutoHide = false,
                    Type     = MessageType.Warning
                });

                return;
            }

            var article = new Article()
            {
                Title       = articleProjection.Title,
                Content     = articleProjection.Content,
                AuthorId    = this.userIdProvider.GetUserId(),
                DateCreated = DateTime.Now,
                CategoryId  = categoryId
            };

            if (this.ModelState.IsValid)
            {
                this.context.Articles.Add(article);
                this.TryUpdateOrShowMessage(new NotificationMessage()
                {
                    Text     = "Article was created successfully.",
                    AutoHide = false,
                    Type     = MessageType.Success
                });
            }
        }
        public IHttpActionResult Create(ArticleProjection articleProjection)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest("Invalid article!");
            }

            var tags = this.GetTags(articleProjection);
            var categoryId = this.GetCategoryId(articleProjection.Category);
            var article = new Article()
            {
                Title = articleProjection.Title,
                Content = articleProjection.Content,
                DateCreated = DateTime.Now,
                CategoryID = categoryId,
                Tags = tags,
                AuthorID = this.GetUserId()
            };

            this.ForumSystemData.Articles.Add(article);
            this.ForumSystemData.SaveChanges();

            articleProjection.ID = article.ID;
            articleProjection.DateCreated = article.DateCreated;
            articleProjection.Tags = article.Tags.AsQueryable().Select(TagProjection.FromTag).ToList();

            return this.Ok(articleProjection);
        }
        private ICollection<Tag> GetTags(ArticleProjection article)
        {
            var allTags = new HashSet<string>();
            allTags.UnionWith(article.Tags.Select(t => t.Name));
            allTags.UnionWith(article.Title.Split(' '));

            var tags = new HashSet<Tag>();

            foreach (var tagName in allTags)
            {
                var tag = this.ForumSystemData.Tags
                              .All()
                              .Where(t => t.Name == tagName)
                              .FirstOrDefault();
                
                if (tag == null)
                {
                    tag = new Tag()
                    {
                        Name = tagName
                    };

                    this.ForumSystemData.Tags.Add(tag);
                    this.ForumSystemData.SaveChanges();
                }

                tags.Add(tag);
            }

            return tags;
        }