Пример #1
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
}
Пример #2
0
// SetLikedPageMeta set likedList's page meta.
func SetLikedPageMeta(likedList *model.LikedList, currentPage int, hasPrev bool, hasNext bool, count int) {
	if len(likedList.Liked) == 0 {
		likedList.Liked = make([]model.User, 0)
	}
	likedList.CurrentPage = currentPage
	likedList.HasPrev = hasPrev
	likedList.HasNext = hasNext
	likedList.Count = count
}