Skip to content

Commit

Permalink
Merge pull request #49 from sapawarga/bugfix-query
Browse files Browse the repository at this point in the history
#45 fix for order by and sort by parameter
  • Loading branch information
setiadijoe authored Jun 3, 2021
2 parents 40fc2ff + 5b565c1 commit 1ec0e1d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 8 additions & 0 deletions endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package endpoint
import (
"context"
"encoding/json"
"strings"

"errors"

"github.com/go-kit/kit/endpoint"
"github.com/sapawarga/userpost-service/helper"
Expand All @@ -13,6 +16,11 @@ import (
func MakeGetListUserPost(ctx context.Context, usecase usecase.UsecaseI) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(*GetListUserPostRequest)
cASC := strings.Compare(helper.GetStringFromPointer(req.SortBy), "ASC")
cDESC := strings.Compare(helper.GetStringFromPointer(req.SortBy), "DESC")
if req.SortBy != nil && (cASC*cDESC == 1) {
return nil, errors.New("must_between_ASC_DESC")
}
// TODO: for get metadata from headers grpc needs to update when using authorization
resp, err := usecase.GetListPost(ctx, &model.GetListRequest{
ActivityName: req.ActivityName,
Expand Down
3 changes: 1 addition & 2 deletions repository/mysql/userpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ func (r *UserPost) GetListPost(ctx context.Context, request *model.UserPostReque
params = append(params, request.Offset, request.Limit)
}
if request.OrderBy != nil && request.SortBy != nil {
query.WriteString(" ORDER BY ? ?")
params = append(params, request.OrderBy, request.SortBy)
query.WriteString(fmt.Sprintf(" ORDER BY %s %s", *request.OrderBy, *request.SortBy))
}
if ctx != nil {
err = r.conn.SelectContext(ctx, &result, query.String(), params...)
Expand Down
2 changes: 1 addition & 1 deletion usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *Post) GetListPost(ctx context.Context, params *model.GetListRequest) (*
Offset: helper.SetPointerInt64(offset),
Limit: helper.SetPointerInt64(limit),
SortBy: params.SortBy,
OrderBy: params.ActivityName,
OrderBy: params.OrderBy,
}

listData, err := p.repoPost.GetListPost(ctx, req)
Expand Down

0 comments on commit 1ec0e1d

Please sign in to comment.