diff --git a/mocks/transport.go b/mocks/transport.go index b67a412..553aec5 100644 --- a/mocks/transport.go +++ b/mocks/transport.go @@ -24,7 +24,7 @@ func (m *MockOrderBookService) GetUserByPublicKey(ctx context.Context, publicKey return m.User, m.Error } -func (m *MockOrderBookService) ProcessOrder(ctx context.Context, input service.ProcessOrderInput) (models.Order, error) { +func (m *MockOrderBookService) CreateOrder(ctx context.Context, input service.CreateOrderInput) (models.Order, error) { return *m.Order, m.Error } diff --git a/service/process_order.go b/service/create_order.go similarity index 79% rename from service/process_order.go rename to service/create_order.go index 858dfc3..036e9e4 100644 --- a/service/process_order.go +++ b/service/create_order.go @@ -12,7 +12,7 @@ import ( "github.com/shopspring/decimal" ) -type ProcessOrderInput struct { +type CreateOrderInput struct { UserId uuid.UUID Price decimal.Decimal Symbol models.Symbol @@ -25,7 +25,7 @@ var ( ErrClashingOrderId = errors.New("order with that ID already exists") ) -func (s *Service) ProcessOrder(ctx context.Context, input ProcessOrderInput) (models.Order, error) { +func (s *Service) CreateOrder(ctx context.Context, input CreateOrderInput) (models.Order, error) { existingOrder, err := s.orderBookStore.FindOrderById(ctx, input.ClientOrderID, true) @@ -49,12 +49,12 @@ func (s *Service) ProcessOrder(ctx context.Context, input ProcessOrderInput) (mo return models.Order{}, models.ErrOrderAlreadyExists } - logctx.Error(ctx, "did not follow any cases when processing order", logger.String("clientOrderId", input.ClientOrderID.String()), logger.String("userId", input.UserId.String()), logger.String("price", input.Price.String()), logger.String("size", input.Size.String()), logger.String("symbol", input.Symbol.String()), logger.String("side", input.Side.String())) + logctx.Error(ctx, "did not follow any cases when creating order", logger.String("clientOrderId", input.ClientOrderID.String()), logger.String("userId", input.UserId.String()), logger.String("price", input.Price.String()), logger.String("size", input.Size.String()), logger.String("symbol", input.Symbol.String()), logger.String("side", input.Side.String())) return models.Order{}, models.ErrUnexpectedError } -func (s *Service) createNewOrder(ctx context.Context, input ProcessOrderInput, userId uuid.UUID) (models.Order, error) { +func (s *Service) createNewOrder(ctx context.Context, input CreateOrderInput, userId uuid.UUID) (models.Order, error) { orderId := uuid.New() logctx.Info(ctx, "creating new order", logger.String("orderId", orderId.String()), logger.String("clientOrderId", input.ClientOrderID.String())) diff --git a/service/process_order_test.go b/service/create_order_test.go similarity index 89% rename from service/process_order_test.go rename to service/create_order_test.go index cf94d48..3119dba 100644 --- a/service/process_order_test.go +++ b/service/create_order_test.go @@ -13,7 +13,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestService_ProcessOrder(t *testing.T) { +func TestService_CreateOrder(t *testing.T) { ctx := context.Background() mockBcClient := &mocks.MockBcClient{IsVerified: true} @@ -32,7 +32,7 @@ func TestService_ProcessOrder(t *testing.T) { } t.Run("unexpected error from store - should return `ErrUnexpectedError` error", func(t *testing.T) { - input := service.ProcessOrderInput{ + input := service.CreateOrderInput{ UserId: userId, Price: price, Symbol: symbol, @@ -43,14 +43,14 @@ func TestService_ProcessOrder(t *testing.T) { svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Error: assert.AnError}, mockBcClient) - order, err := svc.ProcessOrder(ctx, input) + order, err := svc.CreateOrder(ctx, input) assert.ErrorIs(t, err, models.ErrUnexpectedError) assert.Equal(t, models.Order{}, order) }) t.Run("no previous order - should create new order", func(t *testing.T) { - input := service.ProcessOrderInput{ + input := service.CreateOrderInput{ UserId: userId, Price: price, Symbol: symbol, @@ -61,7 +61,7 @@ func TestService_ProcessOrder(t *testing.T) { svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: nil}, mockBcClient) - newOrder, err := svc.ProcessOrder(ctx, input) + newOrder, err := svc.CreateOrder(ctx, input) assert.NoError(t, err) // TODO: I am not asserting against the full order as timestamp is always different @@ -77,7 +77,7 @@ func TestService_ProcessOrder(t *testing.T) { }) t.Run("existing order with different userId - should return `ErrClashingOrderId` error", func(t *testing.T) { - input := service.ProcessOrderInput{ + input := service.CreateOrderInput{ UserId: userId, Price: price, Symbol: symbol, @@ -88,14 +88,14 @@ func TestService_ProcessOrder(t *testing.T) { svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{UserId: uuid.MustParse("b577273e-12de-4acc-a4f8-de7fb5b86e37")}}, mockBcClient) - order, err := svc.ProcessOrder(ctx, input) + order, err := svc.CreateOrder(ctx, input) assert.ErrorIs(t, err, service.ErrClashingOrderId) assert.Equal(t, models.Order{}, order) }) t.Run("existing order with same clientOrderId - should return `ErrOrderAlreadyExists` error", func(t *testing.T) { - input := service.ProcessOrderInput{ + input := service.CreateOrderInput{ UserId: userId, Price: price, Symbol: symbol, @@ -106,7 +106,7 @@ func TestService_ProcessOrder(t *testing.T) { svc, _ := service.New(&mocks.MockOrderBookStore{User: &user, Order: &models.Order{ClientOId: orderId, UserId: userId}}, mockBcClient) - order, err := svc.ProcessOrder(ctx, input) + order, err := svc.CreateOrder(ctx, input) assert.ErrorIs(t, err, models.ErrOrderAlreadyExists) assert.Equal(t, models.Order{}, order) diff --git a/service/service.go b/service/service.go index b80a17c..fac3a25 100644 --- a/service/service.go +++ b/service/service.go @@ -13,7 +13,7 @@ import ( ) type OrderBookService interface { - ProcessOrder(ctx context.Context, input ProcessOrderInput) (models.Order, error) + CreateOrder(ctx context.Context, input CreateOrderInput) (models.Order, error) CancelOrder(ctx context.Context, input CancelOrderInput) (cancelledOrderId *uuid.UUID, err error) GetBestPriceFor(ctx context.Context, symbol models.Symbol, side models.Side) (decimal.Decimal, error) GetOrderById(ctx context.Context, orderId uuid.UUID) (*models.Order, error) diff --git a/transport/rest/process_order.go b/transport/rest/create_order.go similarity index 96% rename from transport/rest/process_order.go rename to transport/rest/create_order.go index 4dbafad..975aa93 100644 --- a/transport/rest/process_order.go +++ b/transport/rest/create_order.go @@ -26,7 +26,7 @@ type CreateOrderResponse struct { OrderId string `json:"orderId"` } -func (h *Handler) ProcessOrder(w http.ResponseWriter, r *http.Request) { +func (h *Handler) CreateOrder(w http.ResponseWriter, r *http.Request) { ctx := r.Context() user := utils.GetUserCtx(ctx) if user == nil { @@ -90,7 +90,7 @@ func (h *Handler) ProcessOrder(w http.ResponseWriter, r *http.Request) { } logctx.Info(ctx, "user trying to create order", logger.String("userId", user.Id.String()), logger.String("price", roundedDecPrice.String()), logger.String("size", decSize.String()), logger.String("clientOrderId", clientOrderId.String())) - order, err := h.svc.ProcessOrder(ctx, service.ProcessOrderInput{ + order, err := h.svc.CreateOrder(ctx, service.CreateOrderInput{ UserId: user.Id, Price: roundedDecPrice, Symbol: symbol, diff --git a/transport/rest/handler.go b/transport/rest/handler.go index d6315c4..fe62ddb 100644 --- a/transport/rest/handler.go +++ b/transport/rest/handler.go @@ -43,7 +43,7 @@ func (h *Handler) initMMRoutes(getUserByApiKey middleware.GetUserByApiKeyFunc) { // ------- CREATE ------- // Place a new order - mmApi.HandleFunc("/order", h.ProcessOrder).Methods("POST") + mmApi.HandleFunc("/order", h.CreateOrder).Methods("POST") // ------- READ ------- // Get an order by client order ID