58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package service
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/constants"
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
"apskel-pos-be/internal/models"
|
||
|
|
"apskel-pos-be/internal/processor"
|
||
|
|
"apskel-pos-be/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CustomerOrderService serves the customer app's order history.
|
||
|
|
type CustomerOrderService interface {
|
||
|
|
List(ctx context.Context, customerID uuid.UUID, query models.ListCustomerOrdersQuery) *contract.Response
|
||
|
|
Detail(ctx context.Context, customerID, orderID uuid.UUID) *contract.Response
|
||
|
|
}
|
||
|
|
|
||
|
|
type CustomerOrderServiceImpl struct {
|
||
|
|
orders *processor.CustomerOrderProcessor
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCustomerOrderService(orders *processor.CustomerOrderProcessor) *CustomerOrderServiceImpl {
|
||
|
|
return &CustomerOrderServiceImpl{orders: orders}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *CustomerOrderServiceImpl) List(ctx context.Context, customerID uuid.UUID, query models.ListCustomerOrdersQuery) *contract.Response {
|
||
|
|
orders, err := s.orders.List(ctx, customerID, query)
|
||
|
|
if err != nil {
|
||
|
|
return customerOrderErrorResponse(err)
|
||
|
|
}
|
||
|
|
return contract.BuildSuccessResponse(orders)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *CustomerOrderServiceImpl) Detail(ctx context.Context, customerID, orderID uuid.UUID) *contract.Response {
|
||
|
|
order, err := s.orders.Detail(ctx, customerID, orderID)
|
||
|
|
if err != nil {
|
||
|
|
return customerOrderErrorResponse(err)
|
||
|
|
}
|
||
|
|
return contract.BuildSuccessResponse(order)
|
||
|
|
}
|
||
|
|
|
||
|
|
func customerOrderErrorResponse(err error) *contract.Response {
|
||
|
|
code := constants.InternalServerErrorCode
|
||
|
|
switch {
|
||
|
|
case errors.Is(err, repository.ErrCustomerOrderNotFound):
|
||
|
|
code = constants.NotFoundErrorCode
|
||
|
|
case errors.Is(err, processor.ErrInvalidWalletQuery):
|
||
|
|
code = constants.ValidationErrorCode
|
||
|
|
}
|
||
|
|
return contract.BuildErrorResponse([]*contract.ResponseError{
|
||
|
|
contract.NewResponseError(code, constants.RequestEntity, err.Error()),
|
||
|
|
})
|
||
|
|
}
|