Skip to content

Commit

Permalink
Merge pull request #11 from codermuss/ft/create_post
Browse files Browse the repository at this point in the history
add create post
  • Loading branch information
codermuss authored Jul 28, 2024
2 parents 6b542b4 + bc6b0d4 commit f2e75bb
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 87 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ server:
mock:
mockgen -package mockdb -destination db/mock/store.go github.com/mustafayilmazdev/musarchive/db/sqlc Store

.PHONY: postgres migrateup1 migrateup migratedown migratedown1 new_migration test sqlc server
locale:
musale --json=locales/assets/en.json --output=locales/localekeys.go -p=localization

.PHONY: postgres migrateup1 migrateup migratedown migratedown1 new_migration test sqlc server mock locale
96 changes: 96 additions & 0 deletions api/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,108 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgtype"
db "github.com/mustafayilmazdev/musarchive/db/sqlc"
localization "github.com/mustafayilmazdev/musarchive/locales"
"github.com/mustafayilmazdev/musarchive/token"
"github.com/mustafayilmazdev/musarchive/util"
)

type createPostRequest struct {
Title string `json:"title" binding:"required"`
Content string `json:"content" binding:"required"`
CoverImage pgtype.Text `json:"cover_image"`
Categories *[]int `json:"categories"`
Tags *[]int `json:"tags"`
}

func (server *Server) CreatePost(ctx *gin.Context) {
locale := ctx.Query(util.Locale)
var req createPostRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusBadRequest,
Message: ResponseMessage{
Type: ERROR,
Content: err.Error(),
},
})
return
}
authPayload := ctx.MustGet(authorizationPayloadKey).(*token.Payload)
arg := db.InsertPostParams{
UserID: pgtype.Int4{
Valid: true,
Int32: int32(authPayload.UserID),
},
Title: req.Title,
Content: req.Content,
CoverImage: req.CoverImage,
}

post, err := server.store.InsertPost(ctx, arg)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Message: ResponseMessage{
Type: ERROR,
Content: err.Error(),
},
})
return
}

if req.Categories != nil {
for _, categoryID := range *req.Categories {
postCategoryArg := db.InsertPostCategoryParams{
PostID: post.ID,
CategoryID: int32(categoryID),
}
_, err = server.store.InsertPostCategory(ctx, postCategoryArg)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Message: ResponseMessage{
Type: ERROR,
Content: err.Error(),
},
})
return
}
}
}

if req.Tags != nil {
for _, v := range *req.Tags {
postTag := db.InsertPostTagParams{
PostID: post.ID,
TagID: int32(v),
}
_, err = server.store.InsertPostTag(ctx, postTag)

if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Message: ResponseMessage{
Type: ERROR,
Content: err.Error(),
},
})
return
}
}
}

BuildResponse(ctx, BaseResponse{
Code: http.StatusOK,
Data: post,
Message: ResponseMessage{
Type: SUCCESS,
Content: server.lm.Translate(locale, localization.Post_InsertSuccess),
},
})
}

func (server *Server) GetPosts(ctx *gin.Context) {
localeValue := ctx.Query(util.Locale)
pageStr := ctx.Query(util.Page)
Expand Down
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (server *Server) setupRouter() {
{
authRoutes.GET("/posts", server.GetPosts)
authRoutes.GET("/followed_posts", server.GetFollowedPosts)
authRoutes.POST("/create_post", server.CreatePost)
}

// Serve the bundled static files
Expand Down
7 changes: 3 additions & 4 deletions db/migration/000001_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ CREATE TABLE "posts" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"user_id" int,
"title" varchar(255) NOT NULL,
"summary" text NOT NULL,
"content" text NOT NULL,
"cover_image" varchar(255),
"created_at" timestamptz DEFAULT (now()),
"updated_at" timestamptz DEFAULT (now()),
"likes" int DEFAULT 0
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now()),
"likes" int NOT NULL DEFAULT 0
);

CREATE TABLE "tags" (
Expand Down
11 changes: 5 additions & 6 deletions db/query/posts.sql
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
-- name: InsertPost :one
INSERT INTO posts (user_id, title, summary, content, cover_image, created_at, updated_at, likes)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
INSERT INTO posts (user_id, title, content, cover_image, created_at, updated_at, likes)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;

-- name: GetPost :one
SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes
SELECT id, user_id, title, content, cover_image, created_at, updated_at, likes
FROM posts
WHERE id = $1;

-- name: GetPosts :many
SELECT id, user_id, title, summary, content, cover_image, created_at, updated_at, likes
SELECT id, user_id, title, content, cover_image, created_at, updated_at, likes
FROM posts
ORDER BY id LIMIT $1 OFFSET $2;

-- name: UpdatePost :one
UPDATE posts
SET
title = COALESCE(sqlc.narg(title),title),
summary = COALESCE(sqlc.narg(summary),summary),
content = COALESCE(sqlc.narg(content),content),
cover_image = COALESCE(sqlc.narg(cover_image),cover_image),
likes = COALESCE(sqlc.narg(likes),likes)
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
SELECT p.id, p.user_id, p.title, 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
Expand Down
4 changes: 2 additions & 2 deletions db/query/user_posts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ DELETE FROM user_posts
WHERE user_id = $1 AND post_id = $2;

-- name: GetUserPosts :many
SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes
SELECT b.id, b.title, b.content, b.cover_image, b.created_at, b.updated_at, b.likes
FROM posts b
JOIN user_posts up ON up.post_id = b.id
WHERE up.user_id = $1;

-- name: GetUserPost :one
SELECT b.id, b.title, b.summary, b.content, b.cover_image, b.created_at, b.updated_at, b.likes
SELECT b.id, b.title, b.content, b.cover_image, b.created_at, b.updated_at, b.likes
FROM posts b
JOIN user_posts up ON up.post_id = b.id
WHERE up.user_id = $1 AND b.id = $2;
17 changes: 8 additions & 9 deletions db/sqlc/models.go

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

47 changes: 19 additions & 28 deletions db/sqlc/posts.sql.go

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

Loading

0 comments on commit f2e75bb

Please sign in to comment.