Skip to content

Commit

Permalink
feat: get order
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Salamakha committed Nov 3, 2024
1 parent 6267079 commit 8547f38
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 4 deletions.
36 changes: 32 additions & 4 deletions internal/handlers/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func mapUserOrder(o *ent.Order, log *zap.Logger) (*models.UserOrder, error) {
}, nil
}

func mapUserOrdersToResponse(entOrders []*ent.Order, log *zap.Logger) ([]*models.UserOrder, error) {
func mapUserOrdersToResponse(log *zap.Logger, entOrders ...*ent.Order) ([]*models.UserOrder, error) {
modelOrders := make([]*models.UserOrder, len(entOrders))
for i, o := range entOrders {
order, err := mapUserOrder(o, log)
Expand All @@ -175,7 +175,7 @@ func mapUserOrdersToResponse(entOrders []*ent.Order, log *zap.Logger) ([]*models
return modelOrders, nil
}

func mapOrdersToResponse(entOrders []*ent.Order, log *zap.Logger) ([]*models.Order, error) {
func mapOrdersToResponse(log *zap.Logger, entOrders ...*ent.Order) ([]*models.Order, error) {
modelOrders := make([]*models.Order, len(entOrders))
for i, o := range entOrders {
uo, err := mapUserOrder(o, log)
Expand Down Expand Up @@ -243,7 +243,7 @@ func (o Order) ListUserOrdersFunc(repository domain.OrderRepository) orders.GetU
}
}

mappedOrders, err := mapUserOrdersToResponse(items, o.logger)
mappedOrders, err := mapUserOrdersToResponse(o.logger, items...)
if err != nil {
o.logger.Error(messages.ErrMapOrder, zap.Error(err))
return orders.NewGetUserOrdersDefault(http.StatusInternalServerError).
Expand Down Expand Up @@ -306,7 +306,7 @@ func (o Order) ListAllOrdersFunc(repository domain.OrderRepository) orders.GetAl
}
}

mappedOrders, err := mapOrdersToResponse(items, o.logger)
mappedOrders, err := mapOrdersToResponse(o.logger, items...)
if err != nil {
o.logger.Error(messages.ErrMapOrder, zap.Error(err))
return orders.NewGetAllOrdersDefault(http.StatusInternalServerError).
Expand Down Expand Up @@ -414,3 +414,31 @@ func (o Order) UpdateOrderFunc(repository domain.OrderRepository) orders.UpdateO
return orders.NewUpdateOrderOK().WithPayload(mappedOrder)
}
}

func (o Order) GetOrderFunc(repository domain.OrderRepository) orders.GetOrderHandlerFunc {
return func(p orders.GetOrderParams, principal *models.Principal) middleware.Responder {
ctx := p.HTTPRequest.Context()
orderID := int(p.OrderID)

order, err := repository.Get(ctx, orderID)
if err != nil {
if ent.IsNotFound(err) {
o.logger.Error(messages.ErrOrderNotFound, zap.Error(err))
return orders.NewGetOrderNotFound()
} else {
o.logger.Error(messages.ErrGetOrder, zap.Error(err))
return orders.NewGetOrderDefault(http.StatusInternalServerError).
WithPayload(buildInternalErrorPayload(messages.ErrGetOrder, err.Error()))
}
}

mappedOrder, err := mapOrdersToResponse(o.logger, order)
if err != nil {
o.logger.Error(messages.ErrGetOrder, zap.Error(err))
return orders.NewGetOrderDefault(http.StatusInternalServerError).
WithPayload(buildInternalErrorPayload(messages.ErrGetOrder, err.Error()))
}

return orders.NewGetOrderOK().WithPayload(mappedOrder[0])
}
}
46 changes: 46 additions & 0 deletions internal/integration-tests/orders/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,52 @@ func TestIntegration_UpdateOrder(t *testing.T) {
})
}

func TestIntegration_GetOrder(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

ctx := context.Background()
client := common.SetupClient()
equip, err := createEquipment(ctx, client, auth)
assert.NoError(t, err)

createParams := orders.NewCreateOrderParamsWithContext(ctx)
desc := "test description"
eqID := equip.ID
rentStart := strfmt.DateTime(time.Now())
rentEnd := strfmt.DateTime(time.Now().Add(time.Hour * 24))
createParams.Data = &models.OrderCreateRequest{
Description: desc,
EquipmentID: eqID,
RentEnd: &rentEnd,
RentStart: &rentStart,
}
order, err := client.Orders.CreateOrder(createParams, auth)
require.NoError(t, err)

// var orderID int64 = 1 //order.Payload.ID

t.Run("Get Order", func(t *testing.T) {
params := orders.NewGetOrderParamsWithContext(ctx)
params.OrderID = *order.Payload.ID

res, err := client.Orders.GetOrder(params, auth)
require.NoError(t, err)
assert.Equal(t, *order.Payload.ID, *res.Payload.ID)
assert.Equal(t, "test", *res.Payload.Description)
})

t.Run("Get Order Not Found", func(t *testing.T) {
params := orders.NewGetOrderParamsWithContext(ctx)
params.OrderID = 999999

res, err := client.Orders.GetOrder(params, auth)
require.Equal(t, res.Code(), http.StatusNotFound)
require.NoError(t, err)
})
}

func createEquipment(ctx context.Context, client *client.Be, auth runtime.ClientAuthInfoWriterFunc) (*models.EquipmentResponse, error) {
params := equipment.NewCreateNewEquipmentParamsWithContext(ctx)
model, err := setParameters(ctx, client, auth)
Expand Down
1 change: 1 addition & 0 deletions internal/messages/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var (
ErrQueryOrders = "can't get orders"
ErrQueryTotalOrders = "error while getting total of orders"
ErrUpdateOrder = "update order failed"
ErrGetOrder = "can't get order"
ErrEquipmentIsNotFree = "requested equipment is not free"
ErrCheckEqStatusFailed = "error while checking if equipment is available for period"
ErrSmallRentPeriod = "small rent period"
Expand Down
13 changes: 13 additions & 0 deletions internal/repositories/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,16 @@ func (r *orderRepository) getFullOrder(ctx context.Context, order *ent.Order) (*

return order, nil
}

func (r *orderRepository) Get(ctx context.Context, id int) (*ent.Order, error) {
tx, err := middlewares.TxFromContext(ctx)
if err != nil {
return nil, err
}
order, err := tx.Order.Get(ctx, id)
if err != nil {
return nil, err
}

return order, nil
}
48 changes: 48 additions & 0 deletions internal/repositories/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,54 @@ func (s *OrderSuite) TestOrderRepository_Update_WrongOwner() {
require.Nil(t, updated)
}

func (s *OrderSuite) TestGetOrderRepository_Get_OK() {
t := s.T()
ctx := s.ctx
crtx, err := s.client.Tx(ctx)
require.NoError(t, err)
ctx = context.WithValue(ctx, middlewares.TxContextKey, crtx)

description := "test"
eqID := int64(1)
startDate := strfmt.DateTime(time.Now().UTC())
endDate := strfmt.DateTime(time.Now().UTC().Add(time.Hour * 24 * 5))
data := &models.OrderCreateRequest{
Description: description,
EquipmentID: &eqID,
RentEnd: &endDate,
RentStart: &startDate,
}
createdOrder, err := s.orderRepository.Create(ctx, data, s.user.ID, []int{s.equipments[0].ID})
require.NoError(t, err)
require.NoError(t, crtx.Commit())

tx, err := s.client.Tx(ctx)
require.NoError(t, err)
ctx = context.WithValue(ctx, middlewares.TxContextKey, tx)

order, err := s.orderRepository.Get(ctx, createdOrder.ID)
require.NoError(t, err)
require.Equal(t, order.ID, createdOrder.ID)
require.Equal(t, order.Description, createdOrder.Description)
require.NoError(t, tx.Rollback())
}

func (s *OrderSuite) TestGetOrderRepository_Get_NotFound() {
t := s.T()
ctx := s.ctx
crtx, err := s.client.Tx(ctx)
require.NoError(t, err)
ctx = context.WithValue(ctx, middlewares.TxContextKey, crtx)

tx, err := s.client.Tx(ctx)
require.NoError(t, err)
ctx = context.WithValue(ctx, middlewares.TxContextKey, tx)

_, err = s.orderRepository.Get(ctx, 999)
require.EqualError(t, err, "ent: order not found")
require.NoError(t, tx.Rollback())
}

func (s *OrderSuite) TestOrderRepository_getQuantity() {
t := s.T()
valid := int(1)
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type OrderRepository interface {
OrdersTotal(ctx context.Context, ownerId *int) (int, error)
Create(ctx context.Context, data *models.OrderCreateRequest, ownerId int, equipmentIDs []int) (*ent.Order, error)
Update(ctx context.Context, id int, data *models.OrderUpdateRequest, ownerId int) (*ent.Order, error)
Get(ctx context.Context, id int) (*ent.Order, error)
}

type OrderRepositoryWithFilter interface {
Expand Down
29 changes: 29 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,36 @@ paths:
required: true
schema:
$ref: '#/definitions/OrderCreateRequest'

/v1/orders/{orderId}:
get:

summary: Get order by id
security:
- Bearer: [ ]
tags:
- Orders
operationId: GetOrder
parameters:
- name: orderId
in: path
required: true
description: order id
type: integer
responses:
200:
description: Success
schema:
$ref: "#/definitions/Order"
404:
description: Not Found
schema:
type: string
default:
description: Unexpected error.
schema:
$ref: "#/definitions/SwaggerError"

put:
summary: Edit order by id
security:
Expand Down

0 comments on commit 8547f38

Please sign in to comment.