Skip to content

Commit

Permalink
#45 update query for update user post and add decoder of handler http
Browse files Browse the repository at this point in the history
  • Loading branch information
setiadijoe committed Jun 3, 2021
1 parent 86d93d1 commit 797c739
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions repository/mysql/userpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ func (r *UserPost) UpdateStatusOrTitle(ctx context.Context, request *model.Updat
var err error
_, unixTime := helper.GetCurrentTimeUTC()

query.WriteString("UPDATE user_posts")
query.WriteString("UPDATE user_posts SET ")
if request.Status != nil {
query.WriteString("status` = :status")
query.WriteString("status = :status")
params["status"] = request.Status
first = false
}
Expand Down
14 changes: 13 additions & 1 deletion transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func MakeHTTPHandler(ctx context.Context, fs usecase.UsecaseI, logger kitlog.Log
processCreatePost := kithttp.NewServer(endpoint.MakeCreateNewPost(ctx, fs), decodeCreatePost, encodeResponse, opts...)
processCreateComment := kithttp.NewServer(endpoint.MakeCreateComment(ctx, fs), decodeCreateComment, encodeResponse, opts...)
processLikeDislike := kithttp.NewServer(endpoint.MakeLikeOrDislikePost(ctx, fs), decodeGetByID, encodeResponse, opts...)
processUpdate := kithttp.NewServer(endpoint.MakeUpdateStatusOrTitle(ctx, fs), decodeGetByID, encodeResponse, opts...)
processUpdate := kithttp.NewServer(endpoint.MakeUpdateStatusOrTitle(ctx, fs), decodeUpdateStatusOrTitle, encodeResponse, opts...)

r := mux.NewRouter()

Expand Down Expand Up @@ -114,6 +114,18 @@ func decodeCreateComment(ctx context.Context, r *http.Request) (interface{}, err
return reqBody, nil
}

func decodeUpdateStatusOrTitle(ctx context.Context, r *http.Request) (interface{}, error) {
params := mux.Vars(r)
_, id := helper.ConvertFromStringToInt64(params["id"])
reqBody := &endpoint.UpdateStatusOrTitle{}
if err := json.NewDecoder(r.Body).Decode(reqBody); err != nil {
return nil, err
}

reqBody.ID = id
return reqBody, nil
}

func decodeNoRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return r, nil
}
Expand Down
2 changes: 1 addition & 1 deletion usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (p *Post) CreateNewPost(ctx context.Context, requestBody *model.CreateNewPo
}

func (p *Post) UpdateTitleOrStatus(ctx context.Context, requestBody *model.UpdatePostRequest) error {
logger := kitlog.With(p.logger, "method", "UpdateTItleOrStatus")
logger := kitlog.With(p.logger, "method", "UpdateTitleOrStatus")
_, err := p.repoPost.GetDetailPost(ctx, requestBody.ID)
if err != nil {
level.Error(logger).Log("error_get_detail", err)
Expand Down

0 comments on commit 797c739

Please sign in to comment.