-
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.
- Loading branch information
Showing
12 changed files
with
481 additions
and
9 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
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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:generate mockgen -package=$GOPACKAGE -source=$GOFILE -destination=mocks/$GOFILE | ||
package get_sale_order | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/kiaplayer/clean-architecture-example/internal/domain/entity/document" | ||
) | ||
|
||
type saleOrderService interface { | ||
GetOrderByID(ctx context.Context, id uint64) (*document.SaleOrder, error) | ||
} | ||
|
||
type UseCase struct { | ||
saleOrderService saleOrderService | ||
} | ||
|
||
func NewUseCase(sos saleOrderService) *UseCase { | ||
return &UseCase{ | ||
saleOrderService: sos, | ||
} | ||
} | ||
|
||
func (u *UseCase) Handle(ctx context.Context, id uint64) (saleOrder *document.SaleOrder, err error) { | ||
return u.saleOrderService.GetOrderByID(ctx, id) | ||
} |
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,69 @@ | ||
package get_sale_order | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/kiaplayer/clean-architecture-example/internal/domain/entity/document" | ||
mocks "github.com/kiaplayer/clean-architecture-example/internal/domain/use_case/get_sale_order/mocks" | ||
) | ||
|
||
func TestHandle_Success(t *testing.T) { | ||
// arrange | ||
ctrl := gomock.NewController(t) | ||
ctx := context.Background() | ||
|
||
saleOrderServiceMock := mocks.NewMocksaleOrderService(ctrl) | ||
|
||
useCase := NewUseCase(saleOrderServiceMock) | ||
|
||
saleOrder := &document.SaleOrder{ | ||
Document: document.Document{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
saleOrderServiceMock.EXPECT(). | ||
GetOrderByID(ctx, saleOrder.ID). | ||
Return(saleOrder, nil) | ||
|
||
// act | ||
actualSaleOrder, actualErr := useCase.Handle(ctx, saleOrder.ID) | ||
|
||
// assert | ||
assert.NoError(t, actualErr) | ||
assert.Equal(t, saleOrder, actualSaleOrder) | ||
} | ||
|
||
func TestHandle_Error(t *testing.T) { | ||
// arrange | ||
ctrl := gomock.NewController(t) | ||
ctx := context.Background() | ||
|
||
saleOrderServiceMock := mocks.NewMocksaleOrderService(ctrl) | ||
|
||
useCase := NewUseCase(saleOrderServiceMock) | ||
|
||
saleOrder := &document.SaleOrder{ | ||
Document: document.Document{ | ||
ID: 1, | ||
}, | ||
} | ||
|
||
getErr := errors.New("get error") | ||
|
||
saleOrderServiceMock.EXPECT(). | ||
GetOrderByID(ctx, saleOrder.ID). | ||
Return(nil, getErr) | ||
|
||
// act | ||
actualSaleOrder, actualErr := useCase.Handle(ctx, saleOrder.ID) | ||
|
||
// assert | ||
assert.Nil(t, actualSaleOrder) | ||
assert.ErrorContains(t, actualErr, getErr.Error()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//go:generate mockgen -package=$GOPACKAGE -source=$GOFILE -destination=mocks/$GOFILE | ||
package get_sale_order | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/kiaplayer/clean-architecture-example/internal/domain/entity/document" | ||
) | ||
|
||
type useCase interface { | ||
Handle( | ||
ctx context.Context, | ||
id uint64, | ||
) (saleOrder *document.SaleOrder, err error) | ||
} | ||
|
||
type Handler struct { | ||
useCase useCase | ||
} | ||
|
||
func NewHandler(u useCase) *Handler { | ||
return &Handler{ | ||
useCase: u, | ||
} | ||
} | ||
|
||
func (h *Handler) Handle(ctx context.Context, writer http.ResponseWriter, request *http.Request) { | ||
err := h.checkAccess(request) | ||
if err != nil { | ||
writer.WriteHeader(http.StatusForbidden) | ||
_, _ = writer.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
saleOrderID, err := h.validateAndPrepare(request) | ||
if err != nil { | ||
writer.WriteHeader(http.StatusBadRequest) | ||
_, _ = writer.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
saleOrder, err := h.useCase.Handle(ctx, saleOrderID) | ||
if err != nil { | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_, _ = writer.Write([]byte(fmt.Sprintf("SaleOrder ID = %d", saleOrder.ID))) | ||
|
||
return | ||
} | ||
|
||
func (h *Handler) checkAccess(request *http.Request) error { | ||
if request.Method == http.MethodDelete { | ||
return errors.New("access denied") // demo only | ||
} | ||
return nil | ||
} | ||
|
||
func (h *Handler) validateAndPrepare(request *http.Request) (uint64, error) { | ||
id, err := strconv.ParseInt(request.URL.Query().Get("id"), 10, 64) | ||
if err != nil { | ||
return 0, fmt.Errorf("bad id: %w", err) | ||
} | ||
if id <= 0 { | ||
return 0, errors.New("bad id") | ||
} | ||
|
||
return uint64(id), nil | ||
} |
Oops, something went wrong.