Skip to content

Commit 50d21ff

Browse files
committed
feat: add create post endpoint
1 parent 00a1e58 commit 50d21ff

15 files changed

Lines changed: 338 additions & 12 deletions

File tree

controller/post_controller.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package controller
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
7+
"github.com/Lab-RPL-ITS/twitter-clone-api/service"
8+
"github.com/Lab-RPL-ITS/twitter-clone-api/utils"
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
type (
13+
PostController interface {
14+
Create(ctx *gin.Context)
15+
}
16+
17+
postController struct {
18+
postService service.PostService
19+
}
20+
)
21+
22+
func NewPostController(ps service.PostService) PostController {
23+
return &postController{
24+
postService: ps,
25+
}
26+
}
27+
28+
func (c *postController) Create(ctx *gin.Context) {
29+
var post dto.PostCreateRequest
30+
if err := ctx.ShouldBind(&post); err != nil {
31+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
32+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
33+
return
34+
}
35+
36+
result, err := c.postService.CreatePost(ctx.Request.Context(), post)
37+
if err != nil {
38+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_POST, err.Error(), nil)
39+
ctx.JSON(http.StatusBadRequest, res)
40+
return
41+
}
42+
43+
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_POST, result)
44+
ctx.JSON(http.StatusOK, res)
45+
}

controller/user_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewUserController(us service.UserService) UserController {
3030
func (c *userController) Register(ctx *gin.Context) {
3131
var user dto.UserCreateRequest
3232
if err := ctx.ShouldBind(&user); err != nil {
33-
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
33+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER_DATA_FROM_BODY, err.Error(), nil)
3434
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
3535
return
3636
}
@@ -63,7 +63,7 @@ func (c *userController) Me(ctx *gin.Context) {
6363
func (c *userController) Login(ctx *gin.Context) {
6464
var req dto.UserLoginRequest
6565
if err := ctx.ShouldBind(&req); err != nil {
66-
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_DATA_FROM_BODY, err.Error(), nil)
66+
response := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_USER_DATA_FROM_BODY, err.Error(), nil)
6767
ctx.AbortWithStatusJSON(http.StatusBadRequest, response)
6868
return
6969
}

dto/post_dto.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dto
2+
3+
import (
4+
"errors"
5+
6+
"github.com/google/uuid"
7+
)
8+
9+
const (
10+
// Failed
11+
MESSAGE_FAILED_GET_POST_DATA_FROM_BODY = "failed get data from body"
12+
MESSAGE_FAILED_CREATE_POST = "failed create post"
13+
14+
// Succcess
15+
MESSAGE_SUCCESS_CREATE_POST = "success create post"
16+
)
17+
18+
var (
19+
ErrCreatePost = errors.New("failed to create post")
20+
ErrGetPostById = errors.New("post not found")
21+
ErrParseParentID = errors.New("failed to parse parent id")
22+
)
23+
24+
type (
25+
PostCreateRequest struct {
26+
Text string `json:"text" form:"text" binding:"required"`
27+
UserID string `json:"user_id" form:"user_id" binding:"required"`
28+
ParentID *uuid.UUID `json:"parent_id," form:"parent_id"`
29+
}
30+
31+
PostResponse struct {
32+
ID string `json:"id"`
33+
Text string `json:"text"`
34+
ParentID *uuid.UUID `json:"parent_id"`
35+
User UserResponse `json:"user"`
36+
}
37+
)

dto/user_dto.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77

88
const (
99
// Failed
10-
MESSAGE_FAILED_GET_DATA_FROM_BODY = "failed get data from body"
11-
MESSAGE_FAILED_REGISTER_USER = "failed create user"
12-
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
13-
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
14-
MESSAGE_FAILED_GET_USER = "failed get user"
15-
MESSAGE_FAILED_LOGIN = "failed login"
16-
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
17-
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
10+
MESSAGE_FAILED_GET_USER_DATA_FROM_BODY = "failed get data from body"
11+
MESSAGE_FAILED_REGISTER_USER = "failed create user"
12+
MESSAGE_FAILED_TOKEN_NOT_VALID = "token not valid"
13+
MESSAGE_FAILED_TOKEN_NOT_FOUND = "token not found"
14+
MESSAGE_FAILED_GET_USER = "failed get user"
15+
MESSAGE_FAILED_LOGIN = "failed login"
16+
MESSAGE_FAILED_PROSES_REQUEST = "failed proses request"
17+
MESSAGE_FAILED_DENIED_ACCESS = "denied access"
1818

1919
// Success
2020
MESSAGE_SUCCESS_REGISTER_USER = "success create user"
@@ -24,7 +24,7 @@ const (
2424

2525
var (
2626
ErrCreateUser = errors.New("failed to create user")
27-
ErrGetUserById = errors.New("failed to get user by id")
27+
ErrGetUserById = errors.New("user not found")
2828
ErrUsernameAlreadyExists = errors.New("username already exist")
2929
ErrUsernameNotFound = errors.New("username not found")
3030
ErrPasswordNotMatch = errors.New("password not match")

entity/post_entity.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package entity
2+
3+
import "github.com/google/uuid"
4+
5+
type Post struct {
6+
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
7+
Text string `gorm:"not null" json:"text"`
8+
9+
Parent *Post `gorm:"foreignkey:ParentID" json:"parent,omitempty"`
10+
ParentID *uuid.UUID `gorm:"type:uuid" json:"parent_id,omitempty"`
11+
12+
UserID uuid.UUID `gorm:"not null" json:"user_id"`
13+
User User `gorm:"foreignkey:UserID" json:"user"`
14+
15+
Timestamp
16+
}

entity/user_entity.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ type User struct {
1414
Password string `gorm:"not null" json:"password"`
1515
ImageUrl *string `json:"image_url"`
1616

17+
Posts []Post `gorm:"foreignkey:UserID" json:"posts,omitempty"`
18+
1719
Timestamp
1820
}
1921

migrations/json/posts.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"text": "Your heart is the size of an ocean. Go find yourself in its hidden depths."
4+
},
5+
{
6+
"text": "The Bay of Bengal is hit frequently by cyclones. The months of November and May, in particular, are dangerous in this regard."
7+
},
8+
{
9+
"text": "Thinking is the capital, Enterprise is the way, Hard Work is the solution."
10+
},
11+
{
12+
"text": "Life is a gamble. You can get hurt, but people die in plane crashes, lose their arms and legs in car accidents; people die every day. Same with fighters: some die, some get hurt, some go on. You just don't let yourself believe it will happen to you."
13+
}
14+
]

migrations/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
func Migrate(db *gorm.DB) error {
99
if err := db.AutoMigrate(
1010
&entity.User{},
11+
&entity.Post{},
1112
); err != nil {
1213
return err
1314
}

migrations/seeds/post_seed.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package seeds
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"os"
7+
8+
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
9+
"gorm.io/gorm"
10+
)
11+
12+
func ListPostSeeder(db *gorm.DB) error {
13+
jsonFile, err := os.Open("./migrations/json/posts.json")
14+
if err != nil {
15+
return err
16+
}
17+
18+
jsonData, _ := io.ReadAll(jsonFile)
19+
20+
var listPost []entity.Post
21+
if err := json.Unmarshal(jsonData, &listPost); err != nil {
22+
return err
23+
}
24+
25+
hasTable := db.Migrator().HasTable(&entity.Post{})
26+
if !hasTable {
27+
if err := db.Migrator().CreateTable(&entity.Post{}); err != nil {
28+
return err
29+
}
30+
}
31+
32+
var user entity.User
33+
err = db.Find(&user).Limit(2).Error
34+
if err != nil {
35+
return err
36+
}
37+
38+
return nil
39+
}

provider/core.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ func RegisterDependencies(injector *do.Injector) {
2222
})
2323

2424
ProvideUserDependencies(injector)
25+
ProvidePostDependencies(injector)
2526
}

0 commit comments

Comments
 (0)