Skip to content

Commit

Permalink
Merge pull request #65 from imrishuroy/ft/feature-posts
Browse files Browse the repository at this point in the history
add feature post APIs
  • Loading branch information
imrishuroy authored Sep 19, 2024
2 parents 1d7da21 + 207ff35 commit e0a9853
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 2 deletions.
96 changes: 96 additions & 0 deletions api/feature_posts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package api

import (
"firebase.google.com/go/v4/auth"
"github.com/gin-gonic/gin"
db "github.com/imrishuroy/legal-referral/db/sqlc"
"net/http"
"strconv"
)

type saveFeaturePostReq struct {
PostID int32 `json:"post_id"`
UserID string `json:"user_id"`
}

func (server *Server) saveFeaturePost(ctx *gin.Context) {
var req saveFeaturePostReq
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(400, errorResponse(err))
return
}

authPayload := ctx.MustGet(authorizationPayloadKey).(*auth.Token)
if authPayload.UID != req.UserID {
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized"})
return
}

arg := db.SaveFeaturePostParams{
PostID: req.PostID,
UserID: req.UserID,
}

err := server.store.SaveFeaturePost(ctx, arg)
if err != nil {

errorCode := db.ErrorCode(err)
if errorCode == db.UniqueViolation {
ctx.JSON(400, gin.H{"message": "Post already saved"})
return
}
ctx.JSON(500, errorResponse(err))
return
}

ctx.JSON(200, gin.H{"message": "success"})
}

func (server *Server) unSaveFeaturePost(ctx *gin.Context) {
postIdStr := ctx.Param("post_id")

postID, err := strconv.Atoi(postIdStr)
if err != nil {
ctx.JSON(400, errorResponse(err))
return
}

authPayload := ctx.MustGet(authorizationPayloadKey).(*auth.Token)
if authPayload.UID == "" {
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized"})
return
}

arg := db.UnSaveFeaturePostParams{
PostID: int32(postID),
UserID: authPayload.UID,
}

err = server.store.UnSaveFeaturePost(ctx, arg)

if err != nil {
ctx.JSON(500, errorResponse(err))
return
}

ctx.JSON(200, gin.H{"message": "success"})
}

func (server *Server) listFeaturePosts(ctx *gin.Context) {

userID := ctx.Param("user_id")

authPayload := ctx.MustGet(authorizationPayloadKey).(*auth.Token)
if authPayload.UID != userID {
ctx.JSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized"})
return
}

posts, err := server.store.ListFeaturePosts(ctx)
if err != nil {
ctx.JSON(500, errorResponse(err))
return
}

ctx.JSON(200, posts)
}
7 changes: 6 additions & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,14 @@ func (server *Server) setupRouter() {

// save post
auth.POST("/saved-posts", server.savePost)
auth.DELETE("/saved-posts/:saved_post_id", server.unsavePost)
auth.DELETE("/saved-posts/:saved_post_id", server.unSavePost)
auth.GET("/saved-posts/:user_id", server.listSavedPosts)

// feature posts
auth.POST("/feature-posts", server.saveFeaturePost)
auth.DELETE("/feature-posts/:post_id", server.unSaveFeaturePost)
auth.GET("/feature-posts/:user_id", server.listFeaturePosts)

}

func CORSMiddleware() gin.HandlerFunc {
Expand Down
2 changes: 1 addition & 1 deletion api/saved_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (server *Server) savePost(ctx *gin.Context) {

}

func (server *Server) unsavePost(ctx *gin.Context) {
func (server *Server) unSavePost(ctx *gin.Context) {

savedPostParam := ctx.Param("saved_post_id")
// convert this to int32
Expand Down
1 change: 1 addition & 0 deletions db/migration/000030_feature_posts.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS feature_posts;
9 changes: 9 additions & 0 deletions db/migration/000030_feature_posts.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE TABLE feature_posts (
feature_post_id SERIAL PRIMARY KEY,
post_id INT NOT NULL,
user_id VARCHAR NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
FOREIGN KEY (post_id) REFERENCES posts(post_id),
FOREIGN KEY (user_id) REFERENCES users(user_id),
UNIQUE (post_id, user_id)
);
24 changes: 24 additions & 0 deletions db/query/feature_posts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- name: SaveFeaturePost :exec
INSERT INTO feature_posts (
user_id,
post_id
) VALUES (
$1, $2
) RETURNING *;

-- name: UnSaveFeaturePost :exec
DELETE FROM feature_posts
WHERE
user_id = $1 AND post_id = $2;

-- name: ListFeaturePosts :many
SELECT
feature_posts.feature_post_id,
sqlc.embed(posts),
feature_posts.created_at
FROM
feature_posts
JOIN
posts ON feature_posts.post_id = posts.post_id
ORDER BY
feature_posts.created_at DESC;
95 changes: 95 additions & 0 deletions db/sqlc/feature_posts.sql.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/sqlc/models.go

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

3 changes: 3 additions & 0 deletions db/sqlc/querier.go

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

Binary file removed files.zip
Binary file not shown.

0 comments on commit e0a9853

Please sign in to comment.