Skip to content

Commit 2647f29

Browse files
committed
refactor: replace uuid with uint64 for post ID
1 parent 7e75210 commit 2647f29

5 files changed

Lines changed: 34 additions & 37 deletions

File tree

controller/post_controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package controller
22

33
import (
4-
"fmt"
54
"net/http"
5+
"strconv"
66

77
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
88
"github.com/Lab-RPL-ITS/twitter-clone-api/service"
99
"github.com/Lab-RPL-ITS/twitter-clone-api/utils"
1010
"github.com/gin-gonic/gin"
11-
"github.com/google/uuid"
1211
)
1312

1413
type (
@@ -41,8 +40,6 @@ func (c *postController) CreatePost(ctx *gin.Context) {
4140
return
4241
}
4342

44-
fmt.Println("userId", userId)
45-
4643
result, err := c.postService.CreatePost(ctx.Request.Context(), userId, post)
4744
if err != nil {
4845
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_CREATE_POST, err.Error(), nil)
@@ -53,9 +50,9 @@ func (c *postController) CreatePost(ctx *gin.Context) {
5350
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_POST, result)
5451
ctx.JSON(http.StatusOK, res)
5552
}
56-
5753
func (c *postController) GetPostById(ctx *gin.Context) {
58-
postId, err := uuid.Parse(ctx.Param("post_id"))
54+
postIdStr := ctx.Param("post_id")
55+
postId, err := strconv.ParseUint(postIdStr, 10, 64)
5956
if err != nil {
6057
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
6158
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
@@ -74,7 +71,8 @@ func (c *postController) GetPostById(ctx *gin.Context) {
7471
}
7572

7673
func (c *postController) DeletePostById(ctx *gin.Context) {
77-
postId, err := uuid.Parse(ctx.Param("post_id"))
74+
postIdStr := ctx.Param("post_id")
75+
postId, err := strconv.ParseUint(postIdStr, 10, 64)
7876
if err != nil {
7977
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
8078
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
@@ -97,7 +95,8 @@ func (c *postController) UpdatePostById(ctx *gin.Context) {
9795
return
9896
}
9997

100-
postId, err := uuid.Parse(ctx.Param("post_id"))
98+
postIdStr := ctx.Param("post_id")
99+
postId, err := strconv.ParseUint(postIdStr, 10, 64)
101100
if err != nil {
102101
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
103102
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)

dto/post_dto.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55

66
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
7-
"github.com/google/uuid"
87
)
98

109
const (
@@ -34,14 +33,14 @@ var (
3433

3534
type (
3635
PostCreateRequest struct {
37-
Text string `json:"text" form:"text" binding:"required"`
38-
ParentID *uuid.UUID `json:"parent_id," form:"parent_id"`
36+
Text string `json:"text" form:"text" binding:"required"`
37+
ParentID *uint64 `json:"parent_id," form:"parent_id"`
3938
}
4039

4140
PostResponse struct {
42-
ID string `json:"id"`
41+
ID uint64 `json:"id"`
4342
Text string `json:"text"`
44-
ParentID *uuid.UUID `json:"parent_id"`
43+
ParentID *uint64 `json:"parent_id"`
4544
User UserResponse `json:"user"`
4645
}
4746

entity/post_entity.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package entity
33
import "github.com/google/uuid"
44

55
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"`
6+
ID uint64 `gorm:"primary_key;autoIncrement" json:"id"`
7+
Text string `gorm:"not null" json:"text"`
88

9-
Parent *Post `gorm:"foreignkey:ParentID" json:"parent,omitempty"`
10-
ParentID *uuid.UUID `gorm:"type:uuid" json:"parent_id,omitempty"`
9+
Parent *Post `gorm:"foreignkey:ParentID" json:"parent,omitempty"`
10+
ParentID *uint64 `json:"parent_id,omitempty"`
1111

1212
UserID uuid.UUID `gorm:"not null" json:"user_id"`
1313
User User `gorm:"foreignkey:UserID" json:"user"`

repository/post_repository.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ import (
55

66
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
77
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
8-
"github.com/google/uuid"
98
"gorm.io/gorm"
109
)
1110

1211
type (
1312
PostRepository interface {
1413
CreatePost(ctx context.Context, tx *gorm.DB, post entity.Post) (entity.Post, error)
15-
GetPostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) (entity.Post, error)
16-
DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error
17-
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error)
14+
GetPostById(ctx context.Context, tx *gorm.DB, postId uint64) (entity.Post, error)
15+
DeletePostById(ctx context.Context, tx *gorm.DB, postId uint64) error
16+
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uint64, post entity.Post) (entity.Post, error)
1817
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
19-
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uuid.UUID, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
18+
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uint64, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
2019
}
2120

2221
postRepository struct {
@@ -42,7 +41,7 @@ func (r *postRepository) CreatePost(ctx context.Context, tx *gorm.DB, post entit
4241
return post, nil
4342
}
4443

45-
func (r *postRepository) GetPostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) (entity.Post, error) {
44+
func (r *postRepository) GetPostById(ctx context.Context, tx *gorm.DB, postId uint64) (entity.Post, error) {
4645
if tx == nil {
4746
tx = r.db
4847
}
@@ -66,7 +65,7 @@ func (r *postRepository) GetPostById(ctx context.Context, tx *gorm.DB, postId uu
6665
return post, nil
6766
}
6867

69-
func (r *postRepository) DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error {
68+
func (r *postRepository) DeletePostById(ctx context.Context, tx *gorm.DB, postId uint64) error {
7069
if tx == nil {
7170
tx = r.db
7271
}
@@ -78,7 +77,7 @@ func (r *postRepository) DeletePostById(ctx context.Context, tx *gorm.DB, postId
7877
return nil
7978
}
8079

81-
func (r *postRepository) UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error) {
80+
func (r *postRepository) UpdatePostById(ctx context.Context, tx *gorm.DB, postId uint64, post entity.Post) (entity.Post, error) {
8281
if tx == nil {
8382
tx = r.db
8483
}
@@ -126,7 +125,7 @@ func (r *postRepository) GetAllPostsWithPagination(ctx context.Context, tx *gorm
126125
}, err
127126
}
128127

129-
func (r *postRepository) GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uuid.UUID, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error) {
128+
func (r *postRepository) GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uint64, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error) {
130129
if tx == nil {
131130
tx = r.db
132131
}

service/post_service.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
type (
1313
PostService interface {
1414
CreatePost(ctx context.Context, userId string, req dto.PostCreateRequest) (dto.PostResponse, error)
15-
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostRepliesResponse, error)
16-
DeletePostById(ctx context.Context, postId uuid.UUID) error
17-
UpdatePostById(ctx context.Context, userId string, postId uuid.UUID, req dto.PostUpdateRequest) (dto.PostResponse, error)
15+
GetPostById(ctx context.Context, postId uint64) (dto.PostRepliesResponse, error)
16+
DeletePostById(ctx context.Context, postId uint64) error
17+
UpdatePostById(ctx context.Context, userId string, postId uint64, req dto.PostUpdateRequest) (dto.PostResponse, error)
1818
GetAllPosts(ctx context.Context, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
1919
}
2020

@@ -58,7 +58,7 @@ func (s *postService) CreatePost(ctx context.Context, userId string, req dto.Pos
5858
}
5959

6060
return dto.PostResponse{
61-
ID: result.ID.String(),
61+
ID: result.ID,
6262
Text: result.Text,
6363
ParentID: req.ParentID,
6464
User: dto.UserResponse{
@@ -71,7 +71,7 @@ func (s *postService) CreatePost(ctx context.Context, userId string, req dto.Pos
7171
}, nil
7272
}
7373

74-
func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostRepliesResponse, error) {
74+
func (s *postService) GetPostById(ctx context.Context, postId uint64) (dto.PostRepliesResponse, error) {
7575
post, err := s.postRepo.GetPostById(ctx, nil, postId)
7676
if err != nil {
7777
return dto.PostRepliesResponse{}, dto.ErrGetPostById
@@ -85,7 +85,7 @@ func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.Po
8585
var data []dto.PostResponse
8686
for _, reply := range replies.Replies {
8787
datum := dto.PostResponse{
88-
ID: reply.ID.String(),
88+
ID: reply.ID,
8989
Text: reply.Text,
9090
ParentID: reply.ParentID,
9191
User: dto.UserResponse{
@@ -102,7 +102,7 @@ func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.Po
102102

103103
return dto.PostRepliesResponse{
104104
PostResponse: dto.PostResponse{
105-
ID: post.ID.String(),
105+
ID: post.ID,
106106
Text: post.Text,
107107
ParentID: post.ParentID,
108108
User: dto.UserResponse{
@@ -124,7 +124,7 @@ func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.Po
124124
nil
125125
}
126126

127-
func (s *postService) DeletePostById(ctx context.Context, postId uuid.UUID) error {
127+
func (s *postService) DeletePostById(ctx context.Context, postId uint64) error {
128128
_, err := s.postRepo.GetPostById(ctx, nil, postId)
129129
if err != nil {
130130
return dto.ErrGetPostById
@@ -137,7 +137,7 @@ func (s *postService) DeletePostById(ctx context.Context, postId uuid.UUID) erro
137137
return nil
138138
}
139139

140-
func (s *postService) UpdatePostById(ctx context.Context, userId string, postId uuid.UUID, req dto.PostUpdateRequest) (dto.PostResponse, error) {
140+
func (s *postService) UpdatePostById(ctx context.Context, userId string, postId uint64, req dto.PostUpdateRequest) (dto.PostResponse, error) {
141141
post, err := s.postRepo.GetPostById(ctx, nil, postId)
142142
if err != nil {
143143
return dto.PostResponse{}, dto.ErrGetPostById
@@ -155,7 +155,7 @@ func (s *postService) UpdatePostById(ctx context.Context, userId string, postId
155155
}
156156

157157
return dto.PostResponse{
158-
ID: result.ID.String(),
158+
ID: result.ID,
159159
Text: result.Text,
160160
ParentID: result.ParentID,
161161
User: dto.UserResponse{
@@ -177,7 +177,7 @@ func (s *postService) GetAllPosts(ctx context.Context, req dto.PaginationRequest
177177
var data []dto.PostResponse
178178
for _, post := range dataWithPaginate.Posts {
179179
datum := dto.PostResponse{
180-
ID: post.ID.String(),
180+
ID: post.ID,
181181
Text: post.Text,
182182
ParentID: post.ParentID,
183183
User: dto.UserResponse{

0 commit comments

Comments
 (0)