diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 9ab5109..7a50597 100644 --- a/endpoint/endpoint.go +++ b/endpoint/endpoint.go @@ -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 } @@ -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 diff --git a/endpoint/request.go b/endpoint/request.go index f790335..92aa23f 100644 --- a/endpoint/request.go +++ b/endpoint/request.go @@ -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"` } @@ -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)), ) } diff --git a/transport/grpc/transport.go b/transport/grpc/transport.go index c568bc5..b3a27ac 100644 --- a/transport/grpc/transport.go +++ b/transport/grpc/transport.go @@ -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 } @@ -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 } diff --git a/transport/http/handler.go b/transport/http/handler.go index d7c6f37..8e812cb 100644 --- a/transport/http/handler.go +++ b/transport/http/handler.go @@ -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() @@ -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 }