-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from imrishuroy/ft/feature-posts
add feature post APIs
- Loading branch information
Showing
10 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS feature_posts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.