Skip to content

Commit

Permalink
Merge pull request #10 from codermuss/ft/followed_posts
Browse files Browse the repository at this point in the history
✨ add followed posts
  • Loading branch information
codermuss authored Jul 28, 2024
2 parents 5574df0 + ef3a323 commit 6b542b4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 4 deletions.
71 changes: 68 additions & 3 deletions api/post.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package api

import (
"fmt"
"net/http"
"strconv"

"github.com/gin-gonic/gin"
db "github.com/mustafayilmazdev/musarchive/db/sqlc"
localization "github.com/mustafayilmazdev/musarchive/locales"
"github.com/mustafayilmazdev/musarchive/token"
"github.com/mustafayilmazdev/musarchive/util"
"github.com/rs/zerolog/log"
)

func (server *Server) GetBlogs(ctx *gin.Context) {
func (server *Server) GetPosts(ctx *gin.Context) {
localeValue := ctx.Query(util.Locale)
pageStr := ctx.Query(util.Page)
sizeStr := ctx.Query(util.Size)
page := util.PageCount
size := util.SizeCount
log.Info().Msg(pageStr + " " + sizeStr)

if pageStr != "" {
var err error
page, err = strconv.Atoi(pageStr)
Expand Down Expand Up @@ -72,3 +73,67 @@ func (server *Server) GetBlogs(ctx *gin.Context) {
Data: blogs,
})
}

func (server *Server) GetFollowedPosts(ctx *gin.Context) {
localeValue := ctx.Query(util.Locale)
pageStr := ctx.Query(util.Page)
sizeStr := ctx.Query(util.Size)
page := util.PageCount
size := util.SizeCount

if pageStr != "" {
var err error
page, err = strconv.Atoi(pageStr)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusBadRequest,
Message: ResponseMessage{
Type: ERROR,
Content: server.lm.Translate(localeValue, localization.Pagination_pageError, pageStr),
},
})
return
}

}

if sizeStr != "" {
var err error
size, err = strconv.Atoi(sizeStr)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusBadRequest,
Message: ResponseMessage{
Type: ERROR,
Content: server.lm.Translate(localeValue, localization.Pagination_sizeError, sizeStr),
},
})
return
}

}
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
fmt.Println(authPayload.UserID)
arg := db.GetFollowedPostsParams{
FollowerID: int32(authPayload.UserID),
Limit: int32(size),
Offset: int32((page - 1) * size),
}

blogs, err := server.store.GetFollowedPosts(ctx, arg)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Message: ResponseMessage{
Type: ERROR,
Content: server.lm.Translate(localeValue, localization.Errors_InternalError, err.Error()),
},
})
return
}

BuildResponse(ctx, BaseResponse{
Code: http.StatusOK,
Data: blogs,
})
}
3 changes: 2 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func (server *Server) setupRouter() {
}
authRoutes := router.Group("/v1").Use(authMiddleware(server.tokenMaker))
{
authRoutes.GET("/blogs", server.GetBlogs)
authRoutes.GET("/posts", server.GetPosts)
authRoutes.GET("/followed_posts", server.GetFollowedPosts)
}

// Serve the bundled static files
Expand Down
15 changes: 15 additions & 0 deletions db/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions db/query/posts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ UPDATE posts
WHERE id = sqlc.arg(id)
RETURNING *;

-- name: GetFollowedPosts :many
SELECT p.id, p.user_id, p.title, p.summary, p.content, p.cover_image, p.created_at, p.updated_at, p.likes
FROM posts p
JOIN user_followers f ON p.user_id = f.user_id
WHERE f.follower_id = $1
ORDER BY p.id LIMIT $2 OFFSET $3;

-- name: DeletePost :exec
DELETE FROM posts
WHERE id = $1;
44 changes: 44 additions & 0 deletions db/sqlc/posts.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b542b4

Please sign in to comment.