diff --git a/endpoint/endpoint.go b/endpoint/endpoint.go index 8b87e7a..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,8 @@ 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 bd6e731..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"` } @@ -48,21 +42,15 @@ func Validate(in interface{}) error { var err error if obj, ok := in.(*CreateNewPostRequest); ok { err = validation.ValidateStruct(in, - validation.Field(&obj.Title, validation.Required, validation.Length(0, 10)), - validation.Field(obj.Images, validation.Required, validation.By(validationImages(obj.Images))), + validation.Field(&obj.Title, validation.Required, validation.Length(10, 0)), + validation.Field(&obj.Images, validation.Required, validation.By(validationImages(obj.Images))), 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(0, 10)), - 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(0, 10)), + validation.Field(&obj.UserPostID, validation.Required), + validation.Field(&obj.Text, validation.Required, validation.Length(10, 0)), validation.Field(&obj.Status, validation.Required, validation.In(helper.ACTIVED, helper.DELETED, helper.INACTIVED)), ) } @@ -75,7 +63,7 @@ func validationImages(in []*Image) validation.RuleFunc { images := value.([]*Image) for _, v := range images { err = validation.ValidateStruct(v, - validation.Field(v.Path, is.URL), + validation.Field(&v.Path, is.URL), ) } return err diff --git a/mocks/testcases/create-new-post.go b/mocks/testcases/create-new-post.go index dae92f2..b3887d7 100644 --- a/mocks/testcases/create-new-post.go +++ b/mocks/testcases/create-new-post.go @@ -23,7 +23,7 @@ var newRepositoryRequest = &model.CreateNewPostRequestRepository{ Images: "[{\"path\":\"http://localhost\"}]", Tags: tags, Status: helper.ACTIVED, - // ActorID: 1, + ActorID: 1, } type CreateNewUserPost struct { diff --git a/mocks/testcases/get-list-user-post.go b/mocks/testcases/get-list-user-post.go index dfbdde0..cec9434 100644 --- a/mocks/testcases/get-list-user-post.go +++ b/mocks/testcases/get-list-user-post.go @@ -119,12 +119,13 @@ var ( actorResponse = &model.UserResponse{ ID: 1, Name: sql.NullString{String: "John Doe", Valid: true}, - PhotoURL: sql.NullString{String: "sample", Valid: true}, + PhotoURL: sql.NullString{String: "www.instagram.com/htm-medium=?p9878y2y3", 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}, + Status: 10, } actor = &model.Actor{ ID: 1, @@ -153,8 +154,8 @@ var ( Text: commentResponse.Comment, CreatedAt: commentResponse.CreatedAt, UpdatedAt: commentResponse.UpdatedAt, - CreatedBy: actorResponse, - UpdatedBy: actorResponse, + CreatedBy: actor, + UpdatedBy: actor, } totalComment = helper.SetPointerInt64(1) userPostResponse = []*model.UserPostResponse{ diff --git a/mocks/testcases/get_comments.go b/mocks/testcases/get_comments.go index 002264b..ef42c59 100644 --- a/mocks/testcases/get_comments.go +++ b/mocks/testcases/get_comments.go @@ -53,16 +53,16 @@ var ( Text: "comment", CreatedAt: current, UpdatedAt: current, - CreatedBy: actorResponse, - UpdatedBy: actorResponse, + CreatedBy: actor, + UpdatedBy: actor, }, { ID: 2, UserPostID: 1, Text: "ini juga comment", CreatedAt: current, UpdatedAt: current, - CreatedBy: actorResponse, - UpdatedBy: actorResponse, + CreatedBy: actor, + UpdatedBy: actor, }, } ) diff --git a/model/repository_request.go b/model/repository_request.go index 16b842b..0d45c28 100644 --- a/model/repository_request.go +++ b/model/repository_request.go @@ -20,6 +20,7 @@ type CreateCommentRequestRepository struct { UserPostID int64 Text string ActorID int64 + Status int64 } type CreateNewPostRequestRepository struct { diff --git a/model/usecase_request.go b/model/usecase_request.go index 2a57ab6..ddb8150 100644 --- a/model/usecase_request.go +++ b/model/usecase_request.go @@ -28,6 +28,7 @@ type UpdatePostRequest struct { type CreateCommentRequest struct { UserPostID int64 `json:"user_post_id"` Text string `json:"comment"` + Status int64 `json:"status"` } type ActorFromContext struct { diff --git a/model/usecase_response.go b/model/usecase_response.go index 50b1e76..b3d1333 100644 --- a/model/usecase_response.go +++ b/model/usecase_response.go @@ -33,13 +33,13 @@ type Metadata struct { } type Comment struct { - 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"` + 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 *Actor `json:"created_by"` + UpdatedBy *Actor `json:"updated_by"` } type Actor struct { diff --git a/repository/mysql/comment.go b/repository/mysql/comment.go index 48077b7..d2a45e6 100644 --- a/repository/mysql/comment.go +++ b/repository/mysql/comment.go @@ -92,13 +92,14 @@ func (r *Comment) Create(ctx context.Context, req *model.CreateCommentRequestRep var err error _, unixTime := helper.GetCurrentTimeUTC() - query.WriteString("INSERT INTO user_post_comments (user_post_id, `text`, status, created_by, updated_by, created_at, updated_at)") - query.WriteString("VALUES(:user_post_id. :comment, :status, :actor, :actor, :current, :current)") + query.WriteString("INSERT INTO user_post_comments (user_post_id, `text`, status, created_by, updated_by, created_at, updated_at) ") + query.WriteString("VALUES(:user_post_id, :comment, :status, :actor, :actor, :current, :current)") params := map[string]interface{}{ "user_post_id": req.UserPostID, "comment": req.Text, "actor": req.ActorID, "current": unixTime, + "status": req.Status, } if ctx != nil { diff --git a/repository/mysql/userpost.go b/repository/mysql/userpost.go index c88e6cd..2a40659 100644 --- a/repository/mysql/userpost.go +++ b/repository/mysql/userpost.go @@ -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 } 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 d2aee74..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), decodeGetByID, encodeResponse, opts...) + processUpdate := kithttp.NewServer(endpoint.MakeUpdateStatusOrTitle(ctx, fs), decodeCreateComment, encodeResponse, opts...) r := mux.NewRouter() @@ -100,7 +100,6 @@ func decodeCreatePost(ctx context.Context, r *http.Request) (interface{}, error) if err := json.NewDecoder(r.Body).Decode(reqBody); err != nil { return nil, err } - return reqBody, nil } diff --git a/usecase/parser.go b/usecase/parser.go index 099295a..9722306 100644 --- a/usecase/parser.go +++ b/usecase/parser.go @@ -77,13 +77,13 @@ func (p *Post) getDetailComment(ctx context.Context, comment *model.CommentRespo level.Error(logger).Log("error_get_actor_created", err) return nil, err } - commentResp.CreatedBy = actorCreated + commentResp.CreatedBy = p.parsingUserResponse(ctx, 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 + commentResp.UpdatedBy = p.parsingUserResponse(ctx, actorUpdated) return commentResp, nil } diff --git a/usecase/usecase.go b/usecase/usecase.go index fcebf3c..a9c6c00 100644 --- a/usecase/usecase.go +++ b/usecase/usecase.go @@ -28,7 +28,6 @@ func NewPost(repoPost repository.PostI, repoComment repository.CommentI, logger func (p *Post) GetListPost(ctx context.Context, params *model.GetListRequest) (*model.UserPostWithMetadata, error) { logger := kitlog.With(p.logger, "method", "GetListPost") var limit, offset int64 = 10, 0 - if params.Page != nil && params.Limit != nil { limit = helper.GetInt64FromPointer(params.Limit) offset = (helper.GetInt64FromPointer(params.Page) - 1) * limit @@ -56,16 +55,19 @@ func (p *Post) GetListPost(ctx context.Context, params *model.GetListRequest) (* level.Error(logger).Log("error_append_list", err) return nil, err } - total, err := p.repoPost.GetMetadataPost(ctx, req) if err != nil { level.Error(logger).Log("error_get_metadata", err) return nil, err } + offsetPage := helper.GetInt64FromPointer(total) % limit + if offsetPage > 0 { + offsetPage = 1 + } metadata := &model.Metadata{ Page: helper.GetInt64FromPointer(params.Page), - TotalPage: helper.GetInt64FromPointer(total) / limit, + TotalPage: helper.GetInt64FromPointer(total)/limit + offsetPage, Total: helper.GetInt64FromPointer(total), } @@ -164,6 +166,7 @@ func (p *Post) CreateNewPost(ctx context.Context, requestBody *model.CreateNewPo Tags: requestBody.Tags, Status: requestBody.Status, // ActorID: actor["id"].(int64), + ActorID: 1, // for now use default as admin }); err != nil { level.Error(logger).Log("error_create_post", err) return err @@ -173,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) @@ -223,12 +226,15 @@ func (p *Post) GetCommentsByPostID(ctx context.Context, id int64) ([]*model.Comm } func (p *Post) CreateCommentOnPost(ctx context.Context, req *model.CreateCommentRequest) error { + // TODO: implement authorization and authenticationn logger := kitlog.With(p.logger, "method", "CreateCommentOnPost") - actor := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Data + // actor := ctx.Value(helper.ACTORKEY).(*model.ActorFromContext).Data if err := p.repoComment.Create(ctx, &model.CreateCommentRequestRepository{ UserPostID: req.UserPostID, Text: req.Text, - ActorID: actor["id"].(int64), + Status: req.Status, + // ActorID: actor["id"].(int64), + ActorID: 1, // TODO: actor not existed yet using admin as default }); err != nil { level.Error(logger).Log("error_create_comment", err) return err diff --git a/usecase/usecase_test.go b/usecase/usecase_test.go index 4afed54..e2d1787 100644 --- a/usecase/usecase_test.go +++ b/usecase/usecase_test.go @@ -124,6 +124,7 @@ var _ = Describe("Usecase", func() { Expect(err).NotTo(BeNil()) Expect(resp).To(BeNil()) } else { + fmt.Println(resp) Expect(resp).To(Equal(data.ResponseUsecase.Result)) } }