Ejemplo 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
}
Ejemplo 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
}
Ejemplo n.º 3
0
// RetrieveUser retrieves a user.
func RetrieveUser(c *gin.Context) (*model.PublicUser, bool, int64, int, error) {
	var user model.User
	var currentUserId int64
	var isAuthor bool
	// var publicUser *model.PublicUser
	// publicUser.User = &user
	id := c.Params.ByName("id")
	if db.ORM.Select(config.UserPublicFields).First(&user, id).RecordNotFound() {
		return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusNotFound, errors.New("User is not found.")
	}
	currentUser, err := CurrentUser(c)
	if err == nil {
		currentUserId = currentUser.Id
		isAuthor = currentUser.Id == user.Id
	}
	var likingList model.LikingList
	likings, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLikings(user)
	likingList.Likings = likings
	currentUserlikedCount := db.ORM.Model(&user).Where("id =?", currentUserId).Association("Likings").Count()
	log.Debugf("Current user like count : %d", currentUserlikedCount)
	likingMeta.SetLikingPageMeta(&likingList, currentPage, hasPrev, hasNext, user.LikingCount, currentUserlikedCount)
	user.LikingList = likingList
	var likedList model.LikedList
	liked, currentPage, hasPrev, hasNext, _ := likingRetriever.RetrieveLiked(user)
	likedList.Liked = liked
	likingMeta.SetLikedPageMeta(&likedList, currentPage, hasPrev, hasNext, user.LikedCount)
	user.LikedList = likedList
	return &model.PublicUser{User: &user}, isAuthor, currentUserId, http.StatusOK, nil
}
Ejemplo n.º 4
0
// SetLikingPageMeta set likingList's page meta.
func SetLikingPageMeta(likingList *model.LikingList, currentPage int, hasPrev bool, hasNext bool, count int, currentUserlikedCount int) {
	if len(likingList.Likings) == 0 {
		likingList.Likings = make([]model.User, 0)
	}
	if currentUserlikedCount == 1 {
		likingList.IsLiked = true
	}
	likingList.CurrentPage = currentPage
	likingList.HasPrev = hasPrev
	likingList.HasNext = hasNext
	likingList.Count = count
}