Skip to content

Commit

Permalink
get/list/list clubs posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ARUMANDESU committed Jun 9, 2024
1 parent 6903574 commit 0db8d55
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 2 deletions.
5 changes: 3 additions & 2 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (h *Handler) InitRoutes() *gin.Engine {
clubPath.GET("/:id", h.ClubHandler.GetClubHandler)

clubPath.GET("/:id/events", h.EventHandler.ListClubEventsHandler)
clubPath.GET("/:id/posts", h.PostHandler.ListClubPostsHandler)

clubPathAuth := clubPath.Group("")
{
Expand Down Expand Up @@ -197,8 +198,8 @@ func (h *Handler) InitRoutes() *gin.Engine {

postPath := router.Group("/posts")
{
//postPath.GET("/:id", h.UsrHandler.GetUserIDMiddleware(), h.PostHandler.GetPostHandler)
//postPath.GET("", h.PostHandler.ListPublishedPostsHandler)
postPath.GET("/:id", h.UsrHandler.GetUserIDMiddleware(), h.PostHandler.GetPostHandler)
postPath.GET("", h.PostHandler.ListPublishedPostsHandler)

postPathAuth := postPath.Group("")
{
Expand Down
116 changes: 116 additions & 0 deletions internal/handler/post/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package posthandler

import (
postv1 "github.com/ARUMANDESU/uniclubs-protos/gen/go/posts/post"
"github.com/ARUMANDESU/university-clubs-backend/internal/domain"
"github.com/ARUMANDESU/university-clubs-backend/internal/handler/utils"
"github.com/gin-gonic/gin"
"log/slog"
"net/http"
"strings"
)

func (h *Handler) GetPostHandler(c *gin.Context) {
const op = "handler.post.getPostHandler"
log := h.log.With(slog.String("op", op))

postID := c.Params.ByName("id")
if postID == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "post id parameter must be provided"})
return
}

post, err := h.postsClient.GetPost(c, &postv1.GetPostRequest{
Id: postID,
UserId: 0,
})
if err != nil {
handleErrors(c, log, "failed to update post", err)
return
}

c.JSON(http.StatusOK, gin.H{"post": domain.PostFromPb(post)})
}

func (h *Handler) ListPublishedPostsHandler(c *gin.Context) {
const op = "handler.post.listPublishedPostsHandler"
log := h.log.With(slog.String("op", op))

page, err := utils.GetIntFromQuery(c, "page")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

pageSize, err := utils.GetIntFromQuery(c, "page_size")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

request := &postv1.ListPostsRequest{
Page: int32(page),
PageSize: int32(pageSize),
Query: c.Query("query"),
SortBy: c.Query("sort_by"),
SortOrder: c.Query("sort_order"),
}

tags := strings.Split(c.Query("tags"), ",")
if len(tags) > 0 && tags[0] != "" {
request.Tags = tags
}

res, err := h.postsClient.ListPosts(c, request)
if err != nil {
handleErrors(c, log, "failed to list published posts", err)
return
}

c.JSON(http.StatusOK, gin.H{"posts": domain.PostsFromPb(res.GetPosts()), "metadata": res.GetMetadata()})
}

func (h *Handler) ListClubPostsHandler(c *gin.Context) {
const op = "handler.post.listClubPostsHandler"
log := h.log.With(slog.String("op", op))

clubID, err := utils.GetIntFromParams(c.Params, "id")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

page, err := utils.GetIntFromQuery(c, "page")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

pageSize, err := utils.GetIntFromQuery(c, "page_size")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

request := &postv1.ListPostsRequest{
ClubId: clubID,
Page: int32(page),
PageSize: int32(pageSize),
Query: c.Query("query"),
SortBy: c.Query("sort_by"),
SortOrder: c.Query("sort_order"),
}

tags := strings.Split(c.Query("tags"), ",")
if len(tags) > 0 && tags[0] != "" {
request.Tags = tags
}

res, err := h.postsClient.ListPosts(c, request)
if err != nil {
handleErrors(c, log, "failed to list club posts", err)
return
}

c.JSON(http.StatusOK, gin.H{"posts": domain.PostsFromPb(res.GetPosts()), "metadata": res.GetMetadata()})
}

0 comments on commit 0db8d55

Please sign in to comment.