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
// RetrieveLocation retrieves a location.
func RetrieveLocation(c *gin.Context) (model.Location, bool, int64, int, error) {
	var location model.Location
	var currentUserId int64
	isAuthor := false
	id := c.Params.ByName("id")
	if db.ORM.First(&location, "id = ?", id).RecordNotFound() {
		return location, isAuthor, currentUserId, http.StatusNotFound, errors.New("Location is not found.")
	}
	currentUser, err := userService.CurrentUser(c)
	if err == nil {
		currentUserId = currentUser.Id
		isAuthor = currentUser.Id == location.UserId
	}
	assignRelatedUser(&location)
	var commentList model.CommentList
	comments, currentPage, hasPrev, hasNext, _ := commentService.RetrieveComments(location)
	commentList.Comments = comments
	commentService.SetCommentPageMeta(&commentList, currentPage, hasPrev, hasNext, location.CommentCount)
	location.CommentList = commentList
	var likingList model.LikingList
	likings, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLikings(location)
	likingList.Likings = likings
	currentUserlikedCount := db.ORM.Model(&location).Where("id =?", currentUserId).Association("Likings").Count()
	log.Debugf("Current user like count : %d", currentUserlikedCount)
	likingMeta.SetLikingPageMeta(&likingList, currentPage, hasPrev, hasNext, location.LikingCount, currentUserlikedCount)
	location.LikingList = likingList
	return location, isAuthor, currentUserId, http.StatusOK, nil
}
Esempio n. 3
0
// RetrieveCommentsOnLocations retrieve comments on a location.
func RetrieveCommentsOnLocation(c *gin.Context) ([]model.Comment, int, bool, bool, int, int, error) {
	var location model.Location
	var comments []model.Comment
	var retrieveListForm form.RetrieveListForm
	var hasPrev, hasNext bool
	var currentPage, count int
	locationId := c.Params.ByName("id")
	if db.ORM.First(&location, locationId).RecordNotFound() {
		return comments, currentPage, hasPrev, hasNext, count, http.StatusNotFound, errors.New("Location is not found.")
	}

	c.BindWith(&retrieveListForm, binding.Form)
	comments, currentPage, hasPrev, hasNext, count = commentService.RetrieveComments(location, retrieveListForm.CurrentPage)
	return comments, currentPage, hasPrev, hasNext, count, http.StatusOK, nil
}
Esempio n. 4
0
// RetrieveCommentsOnArticles retrieve comments on an article.
func RetrieveCommentsOnArticle(c *gin.Context) ([]model.Comment, int, bool, bool, int, int, error) {
	var article model.Article
	var comments []model.Comment
	var retrieveListForm form.RetrieveListForm
	var hasPrev, hasNext bool
	var currentPage, count int
	articleId := c.Params.ByName("id")
	if db.ORM.First(&article, articleId).RecordNotFound() {
		return comments, currentPage, hasPrev, hasNext, count, http.StatusNotFound, errors.New("Article is not found.")
	}
	c.BindWith(&retrieveListForm, binding.Form)
	log.Debugf("retrieveListForm : %v", retrieveListForm)
	comments, currentPage, hasPrev, hasNext, count = commentService.RetrieveComments(article, retrieveListForm.CurrentPage)
	return comments, currentPage, hasPrev, hasNext, count, http.StatusOK, nil
}