Skip to content

Commit

Permalink
Merge pull request #46 from sapawarga/bugfix-query
Browse files Browse the repository at this point in the history
Bugfix query
  • Loading branch information
setiadijoe authored Jun 2, 2021
2 parents 757d025 + b9c96dd commit 380bdf8
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 156 deletions.
2 changes: 1 addition & 1 deletion mocks/testcases/get-detail-user-post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
IsLiked: true,
CommentCounts: 1,
Status: 10,
Actor: actorResponse,
Actor: actor,
CreatedAt: current,
UpdatedAt: current,
}
Expand Down
17 changes: 14 additions & 3 deletions mocks/testcases/get-list-user-post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -159,7 +170,7 @@ var (
IsLiked: true,
CommentCounts: 1,
Status: 10,
Actor: actorResponse,
Actor: actor,
CreatedAt: current,
UpdatedAt: current,
}, {
Expand All @@ -174,7 +185,7 @@ var (
IsLiked: true,
CommentCounts: 1,
Status: 10,
Actor: actorResponse,
Actor: actor,
CreatedAt: current,
UpdatedAt: current,
},
Expand Down
7 changes: 4 additions & 3 deletions model/repository_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
Expand Down
20 changes: 10 additions & 10 deletions model/usecase_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 38 additions & 26 deletions model/usecase_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
10 changes: 5 additions & 5 deletions repository/mysql/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 14 additions & 16 deletions repository/mysql/userpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 ?, ?")
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions transport/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
Loading

0 comments on commit 380bdf8

Please sign in to comment.