Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add followed posts #10

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading