diff --git a/mocks/testcases/get-detail-user-post.go b/mocks/testcases/get-detail-user-post.go index afc9762..122dcdc 100644 --- a/mocks/testcases/get-detail-user-post.go +++ b/mocks/testcases/get-detail-user-post.go @@ -31,7 +31,7 @@ var ( IsLiked: true, CommentCounts: 1, Status: 10, - Actor: actorResponse, + Actor: actor, CreatedAt: current, UpdatedAt: current, } diff --git a/mocks/testcases/get-list-user-post.go b/mocks/testcases/get-list-user-post.go index 1366edc..dfbdde0 100644 --- a/mocks/testcases/get-list-user-post.go +++ b/mocks/testcases/get-list-user-post.go @@ -121,10 +121,21 @@ var ( Name: sql.NullString{String: "John Doe", Valid: true}, PhotoURL: sql.NullString{String: "sample", Valid: true}, Role: sql.NullInt64{Int64: 99, Valid: true}, + Regency: sql.NullString{String: "regency", Valid: true}, + District: sql.NullString{String: "district", Valid: true}, + Village: sql.NullString{String: "village", Valid: true}, + RW: sql.NullString{String: "rw", Valid: true}, + } + actor = &model.Actor{ + ID: 1, + Name: "John Doe", + PhotoURL: "www.instagram.com/htm-medium=?p9878y2y3", + Role: 99, Regency: "regency", District: "district", Village: "village", - RW: sql.NullString{String: "rw", Valid: true}, + RW: "rw", + Status: 10, } metadataResponse = helper.SetPointerInt64(2) commentResponse = &model.CommentResponse{ @@ -159,7 +170,7 @@ var ( IsLiked: true, CommentCounts: 1, Status: 10, - Actor: actorResponse, + Actor: actor, CreatedAt: current, UpdatedAt: current, }, { @@ -174,7 +185,7 @@ var ( IsLiked: true, CommentCounts: 1, Status: 10, - Actor: actorResponse, + Actor: actor, CreatedAt: current, UpdatedAt: current, }, diff --git a/model/repository_response.go b/model/repository_response.go index bd3a9c7..30e66ce 100644 --- a/model/repository_response.go +++ b/model/repository_response.go @@ -26,9 +26,9 @@ type UserResponse struct { Name sql.NullString `db:"name"` PhotoURL sql.NullString `db:"photo_url"` Role sql.NullInt64 `db:"role"` - Regency string `db:"regency_name"` - District string `db:"district_name"` - Village string `db:"village_name"` + Regency sql.NullString `db:"regency_name"` + District sql.NullString `db:"district_name"` + Village sql.NullString `db:"village_name"` RW sql.NullString `db:"rw"` Status int64 `db:"status"` } @@ -43,6 +43,7 @@ type CommentResponse struct { ID int64 `db:"id"` UserPostID int64 `db:"user_post_id"` Comment string `db:"comment"` + Status int64 `db:"status"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` CreatedBy int64 `db:"created_by"` diff --git a/model/usecase_request.go b/model/usecase_request.go index 201e579..2a57ab6 100644 --- a/model/usecase_request.go +++ b/model/usecase_request.go @@ -12,22 +12,22 @@ type GetListRequest struct { } type CreateNewPostRequest struct { - Title string - ImagePathURL string - Images string - Tags *string - Status int64 + Title string `json:"title"` + ImagePathURL string `json:"image_path_url"` + Images string `json:"images"` + Tags *string `json:"tags,omitempty"` + Status int64 `json:"status"` } type UpdatePostRequest struct { - ID int64 - Status *int64 - Title *string + ID int64 `json:"id"` + Status *int64 `json:"status,omitempty"` + Title *string `json:"title,omitempty"` } type CreateCommentRequest struct { - UserPostID int64 - Text string + UserPostID int64 `json:"user_post_id"` + Text string `json:"comment"` } type ActorFromContext struct { diff --git a/model/usecase_response.go b/model/usecase_response.go index 224d003..50b1e76 100644 --- a/model/usecase_response.go +++ b/model/usecase_response.go @@ -5,39 +5,51 @@ import ( ) type UserPostResponse struct { - ID int64 - Title string - Tag *string - ImagePath string - Images string - LastUserPostCommentID *int64 - LastComment *Comment - LikesCount int64 - IsLiked bool - CommentCounts int64 - Status int64 - Actor *UserResponse - CreatedAt time.Time - UpdatedAt time.Time + ID int64 `json:"id"` + Title string `json:"title"` + Tag *string `json:"tags,omitempty"` + ImagePath string `json:"image_path,omitempty"` + Images string `json:"images"` + LastUserPostCommentID *int64 `json:"last_user_post_comment_id,omitempty"` + LastComment *Comment `json:"last_comment,omitempty"` + LikesCount int64 `json:"likes_count"` + IsLiked bool `json:"is_liked"` + CommentCounts int64 `json:"comment_counts"` + Status int64 `json:"status"` + Actor *Actor `json:"actor"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } type UserPostWithMetadata struct { - Data []*UserPostResponse - Metadata *Metadata + Data []*UserPostResponse `json:"data"` + Metadata *Metadata `json:"metadata"` } type Metadata struct { - Page int64 - TotalPage int64 - Total int64 + Page int64 `json:"page"` + TotalPage int64 `json:"total_page"` + Total int64 `json:"total"` } type Comment struct { - ID int64 - UserPostID int64 - Text string - CreatedAt time.Time - UpdatedAt time.Time - CreatedBy *UserResponse - UpdatedBy *UserResponse + ID int64 `json:"id"` + UserPostID int64 `json:"user_post_id"` + Text string `json:"comment"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + CreatedBy *UserResponse `json:"created_by"` + UpdatedBy *UserResponse `json:"updated_by"` +} + +type Actor struct { + ID int64 `json:"id"` + Name string `json:"name"` + PhotoURL string `json:"photo_url,omitempty"` + Role int64 `json:"role,omitempty"` + Regency string `json:"regency,omitempty"` + District string `json:"district,omitempty"` + Village string `json:"village,omitempty"` + RW string `json:"rw,omitempty"` + Status int64 `json:"status,omitempty"` } diff --git a/repository/mysql/comment.go b/repository/mysql/comment.go index f5fe997..48077b7 100644 --- a/repository/mysql/comment.go +++ b/repository/mysql/comment.go @@ -22,10 +22,10 @@ func NewComment(conn *sqlx.DB) *Comment { func (r *Comment) GetLastComment(ctx context.Context, id int64) (*model.CommentResponse, error) { var query bytes.Buffer - var result *model.CommentResponse + var result = &model.CommentResponse{} var err error - query.WriteString("SELECT id, user_post_id, `text`, status, created_by, updated_by, created_at, updated_at FROM user_post_comments") + query.WriteString("SELECT id, user_post_id, `text` as comment, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_post_comments ") query.WriteString("WHERE id = ?") if ctx != nil { @@ -72,13 +72,13 @@ func (r *Comment) GetCommentsByPostID(ctx context.Context, id int64) ([]*model.C var result = make([]*model.CommentResponse, 0) var err error - query.WriteString("SELECT id, user_post_id, `text`, status, created_by, updated_by, created_at, updated_at FROM user_post_comments") + query.WriteString("SELECT id, user_post_id, `text` as comment, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_post_comments ") query.WriteString("WHERE user_post_id = ?") if ctx != nil { - err = r.conn.SelectContext(ctx, result, query.String(), id) + err = r.conn.SelectContext(ctx, &result, query.String(), id) } else { - err = r.conn.Select(result, query.String(), id) + err = r.conn.Select(&result, query.String(), id) } if err != nil { diff --git a/repository/mysql/userpost.go b/repository/mysql/userpost.go index 484bfe7..c88e6cd 100644 --- a/repository/mysql/userpost.go +++ b/repository/mysql/userpost.go @@ -29,7 +29,7 @@ func (r *UserPost) GetListPost(ctx context.Context, request *model.UserPostReque query.WriteString("SELECT id, text, tags, image_path, images, last_user_post_comment_id, likes_count, comments_count, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_posts") query, params := querySelectParams(ctx, query, request) if request.Limit != nil && request.Offset != nil { - query.WriteString("LIMIT ?, ?") + query.WriteString(" LIMIT ?, ?") params = append(params, request.Offset, request.Limit) } if request.OrderBy != nil && request.SortBy != nil { @@ -56,11 +56,10 @@ func (r *UserPost) GetMetadataPost(ctx context.Context, request *model.UserPostR query.WriteString("SELECT COUNT(1) FROM user_posts") query, params := querySelectParams(ctx, query, request) - query.WriteString("FROM user_posts") if ctx != nil { - err = r.conn.GetContext(ctx, total, query.String(), params...) + err = r.conn.GetContext(ctx, &total, query.String(), params...) } else { - err = r.conn.Get(total, query.String(), params...) + err = r.conn.Get(&total, query.String(), params...) } if err != nil { @@ -75,9 +74,9 @@ func (r *UserPost) GetListPostByMe(ctx context.Context, request *model.UserPostB var result = make([]*model.PostResponse, 0) var err error - query.WriteString("SELECT id, text, tags. image_path, images, last_user_post_comment_id, likes_count, comments_count, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_posts") + query.WriteString("SELECT id, text, tags, image_path, images, last_user_post_comment_id, likes_count, comments_count, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_posts") query, params := querySelectParams(ctx, query, request.UserPostRequest) - query.WriteString("AND created_by = ? ") + query.WriteString(" AND created_by = ? ") params = append(params, request.ActorID) if request.Limit != nil && request.Offset != nil { query.WriteString("LIMIT ?, ?") @@ -106,16 +105,15 @@ func (r *UserPost) GetMetadataPostByMe(ctx context.Context, request *model.UserP var total *int64 var err error - query.WriteString("SELECT COUNT(1) FROM user_posts") + query.WriteString("SELECT COUNT(1) FROM user_posts ") query, params := querySelectParams(ctx, query, request.UserPostRequest) - query.WriteString("AND created_by = ? ") - query.WriteString("FROM user_posts") + query.WriteString(" AND created_by = ? ") params = append(params, request.ActorID) if ctx != nil { - err = r.conn.GetContext(ctx, total, query.String(), params...) + err = r.conn.GetContext(ctx, &total, query.String(), params...) } else { - err = r.conn.Get(total, query.String(), params...) + err = r.conn.Get(&total, query.String(), params...) } if err != nil { @@ -127,15 +125,15 @@ func (r *UserPost) GetMetadataPostByMe(ctx context.Context, request *model.UserP func (r *UserPost) GetActor(ctx context.Context, id int64) (*model.UserResponse, error) { var query bytes.Buffer - var result *model.UserResponse + var result = &model.UserResponse{} var err error query.WriteString("SELECT u.id, u.name, u.photo_url, u.`role`, u.rw, reg.name as regency_name, dis.name as district_name, vil.name as village_name") - query.WriteString("FROM `user` u ") + query.WriteString(` FROM user u `) query.WriteString(`LEFT JOIN areas reg ON reg.id = u.kabkota_id LEFT JOIN areas dis ON dis.id = u.kec_id LEFT JOIN areas vil ON vil.id = u.kel_id`) - query.WriteString("WHERE u.id = ? ") + query.WriteString(" WHERE u.id = ? ") if ctx != nil { err = r.conn.GetContext(ctx, result, query.String(), id) } else { @@ -155,11 +153,11 @@ func (r *UserPost) GetActor(ctx context.Context, id int64) (*model.UserResponse, func (r *UserPost) GetDetailPost(ctx context.Context, id int64) (*model.PostResponse, error) { var query bytes.Buffer - var result *model.PostResponse + var result = &model.PostResponse{} var err error query.WriteString("SELECT id, text, tags, image_path, images, last_user_post_comment_id, likes_count, comments_count, status, created_by, updated_by, FROM_UNIXTIME(created_at) as created_at, FROM_UNIXTIME(updated_at) as updated_at FROM user_posts") - query.WriteString("WHERE id = ?") + query.WriteString(" WHERE id = ?") if ctx != nil { err = r.conn.GetContext(ctx, result, query.String(), id) } else { diff --git a/transport/grpc/transport.go b/transport/grpc/transport.go index 0d03f0b..c568bc5 100644 --- a/transport/grpc/transport.go +++ b/transport/grpc/transport.go @@ -202,9 +202,9 @@ func encodeActor(ctx context.Context, r interface{}) *transportUserPost.Actor { Name: actorResp.Name.String, PhotoUrl: actorResp.PhotoURL.String, Role: actorResp.Role.Int64, - Regency: actorResp.Regency, - District: actorResp.District, - Village: actorResp.Village, + Regency: actorResp.Regency.String, + District: actorResp.District.String, + Village: actorResp.Village.String, Rw: actorResp.RW.String, } } diff --git a/usecase/parser.go b/usecase/parser.go new file mode 100644 index 0000000..099295a --- /dev/null +++ b/usecase/parser.go @@ -0,0 +1,113 @@ +package usecase + +import ( + "context" + + kitlog "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" + "github.com/sapawarga/userpost-service/helper" + "github.com/sapawarga/userpost-service/model" +) + +func (p *Post) getDetailOfUserPost(ctx context.Context, post *model.PostResponse) (*model.UserPostResponse, error) { + logger := kitlog.With(p.logger, "method", "getDetailOfUserPost") + userPost := &model.UserPostResponse{ + ID: post.ID, + Title: post.Title, + Tag: helper.SetPointerString(post.Tag.String), + ImagePath: post.ImagePath.String, + Images: post.Images.String, + LikesCount: post.LikesCount, + CommentCounts: post.CommentCounts, + Status: post.Status, + CreatedAt: post.CreatedAt, + UpdatedAt: post.UpdatedAt, + } + if post.LastUserPostCommentID.Valid { + comment, err := p.repoComment.GetLastComment(ctx, post.LastUserPostCommentID.Int64) + if err != nil { + level.Error(logger).Log("error_get_comment", err) + return nil, err + } + detailComment, err := p.getDetailComment(ctx, comment) + if err != nil { + level.Error(logger).Log("error_get_comment", err) + return nil, err + } + userPost.LastUserPostCommentID = helper.SetPointerInt64(post.LastUserPostCommentID.Int64) + userPost.LastComment = detailComment + } + if post.CreatedBy.Valid { + user, err := p.repoPost.GetActor(ctx, post.CreatedBy.Int64) + if err != nil { + level.Error(logger).Log("error_get_actor", err) + return nil, err + } + userPost.Actor = p.parsingUserResponse(ctx, user) + } + + return userPost, nil +} + +func (p *Post) parsingUserResponse(ctx context.Context, user *model.UserResponse) *model.Actor { + return &model.Actor{ + ID: user.ID, + Name: user.Name.String, + PhotoURL: user.PhotoURL.String, + Role: user.Role.Int64, + Regency: user.Regency.String, + District: user.District.String, + Village: user.Village.String, + RW: user.RW.String, + Status: user.Status, + } +} + +func (p *Post) getDetailComment(ctx context.Context, comment *model.CommentResponse) (*model.Comment, error) { + logger := kitlog.With(p.logger, "method", "getDetailComment") + commentResp := &model.Comment{ + ID: comment.ID, + UserPostID: comment.UserPostID, + Text: comment.Comment, + CreatedAt: comment.CreatedAt, + UpdatedAt: comment.UpdatedAt, + } + actorCreated, err := p.repoPost.GetActor(ctx, comment.CreatedBy) + if err != nil { + level.Error(logger).Log("error_get_actor_created", err) + return nil, err + } + commentResp.CreatedBy = actorCreated + actorUpdated, err := p.repoPost.GetActor(ctx, comment.UpdatedBy) + if err != nil { + level.Error(logger).Log("error_get_actor_updated", err) + return nil, err + } + commentResp.UpdatedBy = actorUpdated + return commentResp, nil +} + +func (p *Post) appendListUserPost(ctx context.Context, resp []*model.PostResponse) (userPosts []*model.UserPostResponse, err error) { + // TODO: using actor TODO + + // actorID := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Get("id").(int64) + for _, v := range resp { + userPost, err := p.getDetailOfUserPost(ctx, v) + if err != nil { + return nil, err + } + isLiked, err := p.repoPost.CheckIsExistLikeOnPostBy(ctx, &model.AddOrRemoveLikeOnPostRequest{ + UserPostID: v.ID, + // ActorID: actorID, + TypeEntity: helper.TYPE_USERPOST, + }) + if err != nil { + return nil, err + } + if isLiked { + userPost.IsLiked = isLiked + } + userPosts = append(userPosts, userPost) + } + return userPosts, nil +} diff --git a/usecase/usecase.go b/usecase/usecase.go index 5860438..fcebf3c 100644 --- a/usecase/usecase.go +++ b/usecase/usecase.go @@ -267,92 +267,3 @@ func (p *Post) LikeOrDislikePost(ctx context.Context, id int64) error { return nil } - -func (p *Post) getDetailOfUserPost(ctx context.Context, post *model.PostResponse) (*model.UserPostResponse, error) { - logger := kitlog.With(p.logger, "method", "getDetailOfUserPost") - userPost := &model.UserPostResponse{ - ID: post.ID, - Title: post.Title, - Tag: helper.SetPointerString(post.Tag.String), - ImagePath: post.ImagePath.String, - Images: post.Images.String, - LikesCount: post.LikesCount, - CommentCounts: post.CommentCounts, - Status: post.Status, - CreatedAt: post.CreatedAt, - UpdatedAt: post.UpdatedAt, - } - if post.LastUserPostCommentID.Valid { - comment, err := p.repoComment.GetLastComment(ctx, post.LastUserPostCommentID.Int64) - if err != nil { - level.Error(logger).Log("error_get_comment", err) - return nil, err - } - detailComment, err := p.getDetailComment(ctx, comment) - if err != nil { - level.Error(logger).Log("error_get_comment", err) - return nil, err - } - userPost.LastUserPostCommentID = helper.SetPointerInt64(post.LastUserPostCommentID.Int64) - userPost.LastComment = detailComment - } - if post.CreatedBy.Valid { - user, err := p.repoPost.GetActor(ctx, post.CreatedBy.Int64) - if err != nil { - level.Error(logger).Log("error_get_actor", err) - return nil, err - } - userPost.Actor = user - } - - return userPost, nil -} - -func (p *Post) getDetailComment(ctx context.Context, comment *model.CommentResponse) (*model.Comment, error) { - logger := kitlog.With(p.logger, "method", "getDetailComment") - commentResp := &model.Comment{ - ID: comment.ID, - UserPostID: comment.UserPostID, - Text: comment.Comment, - CreatedAt: comment.CreatedAt, - UpdatedAt: comment.UpdatedAt, - } - actorCreated, err := p.repoPost.GetActor(ctx, comment.CreatedBy) - if err != nil { - level.Error(logger).Log("error_get_actor_created", err) - return nil, err - } - commentResp.CreatedBy = actorCreated - actorUpdated, err := p.repoPost.GetActor(ctx, comment.UpdatedBy) - if err != nil { - level.Error(logger).Log("error_get_actor_updated", err) - return nil, err - } - commentResp.UpdatedBy = actorUpdated - return commentResp, nil -} - -func (p *Post) appendListUserPost(ctx context.Context, resp []*model.PostResponse) (userPosts []*model.UserPostResponse, err error) { - // TODO: using actor TODO - - // actorID := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Get("id").(int64) - for _, v := range resp { - userPost, err := p.getDetailOfUserPost(ctx, v) - if err != nil { - return nil, err - } - isLiked, err := p.repoPost.CheckIsExistLikeOnPostBy(ctx, &model.AddOrRemoveLikeOnPostRequest{ - UserPostID: v.ID, - // ActorID: actorID, - TypeEntity: helper.TYPE_USERPOST, - }) - if err != nil { - return nil, err - } - if isLiked { - userPost.IsLiked = isLiked - } - userPosts = append(userPosts, userPost) - } - return userPosts, nil -}