Esempio n. 1
0
// RetrieveArticle retrieve an article.
func RetrieveArticle(c *gin.Context) (model.Article, bool, int64, int, error) {
	var article model.Article
	var count int
	var currentUserId int64
	var isAuthor bool
	id := c.Params.ByName("id")
	if db.ORM.First(&article, "id = ?", id).RecordNotFound() {
		return article, isAuthor, currentUserId, http.StatusNotFound, errors.New("Article is not found.")
	}
	log.Debugf("Article : %s\n", article)
	log.Debugf("Count : %s\n", count)
	currentUser, err := userService.CurrentUser(c)
	if err == nil {
		currentUserId = currentUser.Id
		isAuthor = currentUser.Id == article.UserId
	}

	assignRelatedUser(&article)
	var commentList model.CommentList
	comments, currentPage, hasPrev, hasNext, _ := commentService.RetrieveComments(article)
	commentList.Comments = comments
	commentService.SetCommentPageMeta(&commentList, currentPage, hasPrev, hasNext, article.CommentCount)
	article.CommentList = commentList
	var likingList model.LikingList
	likings, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLikings(article)
	likingList.Likings = likings
	currentUserlikedCount := db.ORM.Model(&article).Where("id =?", currentUserId).Association("Likings").Count()
	log.Debugf("Current user like count : %d", currentUserlikedCount)
	likingMeta.SetLikingPageMeta(&likingList, currentPage, hasPrev, hasNext, article.LikingCount, currentUserlikedCount)
	article.LikingList = likingList
	return article, isAuthor, currentUserId, http.StatusOK, nil
}
Esempio n. 2
0
// assignRelatedUser assign related user to the article.
func assignRelatedUser(article *model.Article) {
	var tempUser model.User
	if db.ORM.Model(&article).Select(config.UserPublicFields).Related(&tempUser).RecordNotFound() {
		log.Warn("user is not found.")
	}
	article.Author = model.PublicUser{User: &tempUser}
}
Esempio n. 3
0
// UpdateArticleLikingCount updates a liking count on article.
func UpdateArticleLikingCount(article *model.Article) (int, error) {
	article.LikingCount = db.ORM.Model(article).Association("Likings").Count()
	if db.ORM.Save(article).Error != nil {
		return http.StatusInternalServerError, errors.New("Article liking's count is not updated.")
	}
	return http.StatusOK, nil
}
Esempio n. 4
0
// UpdateArticle updates an article.
func UpdateArticle(c *gin.Context) (model.Article, int, error) {
	var article model.Article
	var form ArticleForm
	id := c.Params.ByName("id")
	c.BindWith(&form, binding.Form)
	if db.ORM.First(&article, id).RecordNotFound() {
		return article, http.StatusNotFound, errors.New("Article is not found.")
	}
	status, err := userPermission.CurrentUserIdentical(c, article.UserId)
	if err != nil {
		return article, status, err
	}
	article.Title = form.Title
	article.Url = form.Url
	article.ImageName = form.ImageName
	article.Content = form.Content
	if db.ORM.Save(&article).Error != nil {
		return article, http.StatusBadRequest, errors.New("Article is not updated.")
	}
	return article, http.StatusOK, nil
}