Skip to content

Commit

Permalink
#45 fix maintainability for using single object, remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
setiadijoe committed Jun 3, 2021
1 parent 797c739 commit 224db27
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 36 deletions.
8 changes: 4 additions & 4 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ func MakeCreateNewPost(ctx context.Context, usecase usecase.UsecaseI) endpoint.E

func MakeUpdateStatusOrTitle(ctx context.Context, usecase usecase.UsecaseI) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(*UpdateStatusOrTitle)
req := request.(*CreateCommentRequest)
if err := Validate(req); err != nil {
return nil, err
}

if err = usecase.UpdateTitleOrStatus(ctx, &model.UpdatePostRequest{
ID: req.ID,
ID: req.UserPostID,
Status: req.Status,
Title: req.Title,
Title: helper.SetPointerString(req.Text),
}); err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func MakeCreateComment(ctx context.Context, usecase usecase.UsecaseI) endpoint.E

if err = usecase.CreateCommentOnPost(ctx, &model.CreateCommentRequest{
UserPostID: req.UserPostID,
Text: req.Comment,
Text: req.Text,
Status: helper.GetInt64FromPointer(req.Status),
}); err != nil {
return nil, err
Expand Down
16 changes: 2 additions & 14 deletions endpoint/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ type Image struct {
Path string `json:"path"`
}

type UpdateStatusOrTitle struct {
ID int64 `json:"id"`
Status *int64 `json:"status"`
Title *string `json:"title"`
}

type CreateCommentRequest struct {
UserPostID int64 `json:"user_post_id"`
Comment string `json:"comment"`
Text string `json:"text"`
Status *int64 `json:"status"`
}

Expand All @@ -53,16 +47,10 @@ func Validate(in interface{}) error {
validation.Field(&obj.Tags, validation.Required),
validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)),
)
} else if obj, ok := in.(*UpdateStatusOrTitle); ok {
err = validation.ValidateStruct(in,
validation.Field(&obj.ID, validation.Required),
validation.Field(&obj.Title, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)),
)
} else if obj, ok := in.(*CreateCommentRequest); ok {
err = validation.ValidateStruct(in,
validation.Field(&obj.UserPostID, validation.Required),
validation.Field(&obj.Comment, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.Text, validation.Required, validation.Length(10, 0)),
validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)),
)
}
Expand Down
10 changes: 5 additions & 5 deletions transport/grpc/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ func encodeStatusResponse(ctx context.Context, r interface{}) (interface{}, erro
func decodeUpdateUserPost(ctx context.Context, r interface{}) (interface{}, error) {
req := r.(*transportUserPost.UpdateUserPostRequest)

return &endpoint.UpdateStatusOrTitle{
ID: req.GetId(),
Status: helper.SetPointerInt64(req.GetStatus()),
Title: helper.SetPointerString(req.GetTitle()),
return &endpoint.CreateCommentRequest{
UserPostID: req.GetId(),
Status: helper.SetPointerInt64(req.GetStatus()),
Text: req.GetTitle(),
}, nil
}

Expand Down Expand Up @@ -277,7 +277,7 @@ func decodeCreateCommentRequest(ctx context.Context, r interface{}) (interface{}

return &endpoint.CreateCommentRequest{
UserPostID: req.GetUserPostId(),
Comment: req.GetComment(),
Text: req.GetComment(),
Status: helper.SetPointerInt64(req.GetStatus()),
}, nil
}
14 changes: 1 addition & 13 deletions 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), decodeUpdateStatusOrTitle, encodeResponse, opts...)
processUpdate := kithttp.NewServer(endpoint.MakeUpdateStatusOrTitle(ctx, fs), decodeCreateComment, encodeResponse, opts...)

r := mux.NewRouter()

Expand Down Expand Up @@ -114,18 +114,6 @@ 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

0 comments on commit 224db27

Please sign in to comment.