-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from RyaWcksn/DEV-0001
Dev 0001
- Loading branch information
Showing
34 changed files
with
807 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/RyaWcksn/ecommerce/dto" | ||
"github.com/RyaWcksn/ecommerce/pkgs/datetime" | ||
"github.com/RyaWcksn/ecommerce/pkgs/errors" | ||
"github.com/RyaWcksn/ecommerce/pkgs/validations" | ||
) | ||
|
||
// CreateOrderHandler implements IHandler | ||
func (h *HandlerImpl) CreateOrderHandler(w http.ResponseWriter, r *http.Request) error { | ||
ctx := r.Context() | ||
|
||
payload := dto.CreateOrderRequest{} | ||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
h.log.Errorf("[ERR] While read body := %v", err) | ||
return errors.GetError(errors.InternalServer, err) | ||
} | ||
if err = json.Unmarshal(body, &payload); err != nil { | ||
h.log.Errorf("[ERR] While unmarshal body := %v", err) | ||
return errors.GetError(errors.InternalServer, err) | ||
} | ||
|
||
if err = validations.Validate(payload); err != nil { | ||
h.log.Errorf("[ERR] While validating body := %v", err) | ||
return errors.GetError(errors.InvalidRequest, err) | ||
} | ||
|
||
status, err := h.serviceImpl.CreateOrder(ctx, &payload) | ||
if err != nil { | ||
h.log.Errorf("[ERR] From service layer := %v", err) | ||
return err | ||
} | ||
|
||
resp := dto.CreateOrderResponse{ | ||
Code: http.StatusOK, | ||
Message: "ok", | ||
ResponseTime: datetime.GetDateString(), | ||
Order: *status, | ||
} | ||
|
||
return ResponseJson(w, resp) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/RyaWcksn/ecommerce/apis/v1/services" | ||
"github.com/RyaWcksn/ecommerce/constants" | ||
"github.com/RyaWcksn/ecommerce/entities" | ||
"github.com/RyaWcksn/ecommerce/pkgs/logger" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func TestHandlerImpl_CreateOrderHandler(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
s := services.NewMockIService(ctrl) | ||
l := logger.New("", "", "") | ||
r := httptest.NewRecorder() | ||
type args struct { | ||
w http.ResponseWriter | ||
r *http.Request | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantMock func() | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
args: args{ | ||
w: r, | ||
r: func() *http.Request { | ||
req := httptest.NewRequest( | ||
http.MethodPost, constants.CreateOrderEndpoint, strings.NewReader( | ||
`{ | ||
"productId": [1], | ||
"sellerId": 1 | ||
}`, | ||
), | ||
) | ||
ctx := context.Background() | ||
req.Header.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRob3JpemVkIjp0cnVlLCJlbWFpbCI6InVzZXJAbWFpbC5jb20iLCJleHAiOjE2ODA4NDA2MTAsImlkIjoxLCJyb2xlIjoic2VsbGVyIn0.BqlpOmIFBGlWZYKvoRqPhD8_q4Smwob0QV47vIVz0QU") | ||
ctx = context.WithValue(ctx, "id", "1") | ||
return req.WithContext(ctx) | ||
}(), | ||
}, | ||
wantMock: func() { | ||
s.EXPECT().CreateOrder(gomock.Any(), gomock.Any()).Return( | ||
&entities.OrderStatus{ | ||
Message: constants.PendingMessage, | ||
Status: constants.Pending, | ||
}, nil, | ||
) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt.wantMock() | ||
t.Run(tt.name, func(t *testing.T) { | ||
h := NewHandlerImpl(s, l) | ||
if err := h.CreateOrderHandler(tt.args.w, tt.args.r); (err != nil) != tt.wantErr { | ||
t.Errorf("HandlerImpl.CreateOrderHandler() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
package repositories | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/RyaWcksn/ecommerce/entities" | ||
) | ||
|
||
//go:generate mockgen -source order.go -destination order_mock.go -package repositories | ||
type IOrder interface{} | ||
type IOrder interface { | ||
CreateOrder(ctx context.Context, entity *entities.CreateOrder) error | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.