// 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 }
// 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 }
// SetCommentPageMeta set comment's page meta. func SetCommentPageMeta(commentList *model.CommentList, currentPage int, hasPrev bool, hasNext bool, count int) { if len(commentList.Comments) == 0 { commentList.Comments = make([]model.Comment, 0) } commentList.CurrentPage = currentPage commentList.HasPrev = hasPrev commentList.HasNext = hasNext commentList.Count = count }