// 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 }
// 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 }
// RetrieveLikingsOnArticles retrieves likings on article. func RetrieveLikingsOnArticles(c *gin.Context) ([]model.User, int, bool, bool, int, int, error) { var article model.Article var likings []model.User var retrieveListForm form.RetrieveListForm var hasPrev, hasNext bool var currentPage, count int articleId := c.Params.ByName("id") log.Debugf("Liking params : %v", c.Params) c.BindWith(&retrieveListForm, binding.Form) log.Debugf("retrieveListForm %+v\n", retrieveListForm) if db.ORM.First(&article, articleId).RecordNotFound() { return likings, currentPage, hasPrev, hasNext, count, http.StatusNotFound, errors.New("Article is not found.") } likings, currentPage, hasPrev, hasNext, count = likingRetriever.RetrieveLikings(article, retrieveListForm.CurrentPage) return likings, currentPage, hasPrev, hasNext, count, http.StatusOK, nil }
// RetrieveLikingsOnLocations retrieves likings on location. func RetrieveLikingsOnLocations(c *gin.Context) ([]*model.PublicUser, int, bool, bool, int, int, error) { var location model.Location var likings []*model.PublicUser var retrieveListForm form.RetrieveListForm var hasPrev, hasNext bool var currentPage, count int locationId := c.Params.ByName("id") log.Debugf("Liking params : %v", c.Params) c.BindWith(&retrieveListForm, binding.Form) log.Debugf("retrieveListForm %+v\n", retrieveListForm) if db.ORM.First(&location, locationId).RecordNotFound() { return likings, currentPage, hasPrev, hasNext, count, http.StatusNotFound, errors.New("Location is not found.") } likings, currentPage, hasPrev, hasNext, count = likingRetriever.RetrieveLikings(location, retrieveListForm.CurrentPage) return likings, currentPage, hasPrev, hasNext, count, http.StatusOK, nil }