Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 - 2025 The Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.elasticsoftware.cryptotrading.query;

public enum OrderState {
CREATED,
PLACED,
REJECTED,
FILLED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2022 - 2025 The Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.elasticsoftware.cryptotrading.query;

import org.elasticsoftware.akces.annotations.QueryModelEventHandler;
import org.elasticsoftware.akces.annotations.QueryModelInfo;
import org.elasticsoftware.akces.query.QueryModel;
import org.elasticsoftware.cryptotrading.aggregates.orders.events.BuyOrderCreatedEvent;
import org.elasticsoftware.cryptotrading.aggregates.orders.events.BuyOrderFilledEvent;
import org.elasticsoftware.cryptotrading.aggregates.orders.events.BuyOrderPlacedEvent;
import org.elasticsoftware.cryptotrading.aggregates.orders.events.BuyOrderRejectedEvent;
import org.elasticsoftware.cryptotrading.aggregates.orders.events.UserOrderProcessesCreatedEvent;

import java.util.ArrayList;
import java.util.List;

@QueryModelInfo(value = "OrdersQueryModel", version = 1, indexName = "Users")
public class OrdersQueryModel implements QueryModel<OrdersQueryModelState> {
@Override
public String getName() {
return "OrdersQueryModel";
}

@Override
public Class<OrdersQueryModelState> getStateClass() {
return OrdersQueryModelState.class;
}

@Override
public String getIndexName() {
return "Users";
}

@QueryModelEventHandler(create = true)
public OrdersQueryModelState create(UserOrderProcessesCreatedEvent event, OrdersQueryModelState isNull) {
return new OrdersQueryModelState(event.userId(), List.of());
}

@QueryModelEventHandler(create = false)
public OrdersQueryModelState addOrder(BuyOrderCreatedEvent event, OrdersQueryModelState currentState) {
OrdersQueryModelState.BuyOrder buyOrder = new OrdersQueryModelState.BuyOrder(
event.orderId(),
event.market(),
event.amount(),
event.clientReference(),
OrderState.CREATED
);
List<OrdersQueryModelState.BuyOrder> orders = new ArrayList<>(currentState.openBuyOrders());
orders.add(buyOrder);
return new OrdersQueryModelState(currentState.userId(), orders);
}

@QueryModelEventHandler(create = false)
public OrdersQueryModelState orderPlaced(BuyOrderPlacedEvent event, OrdersQueryModelState currentState) {
// Update order state to PLACED
List<OrdersQueryModelState.BuyOrder> orders = currentState.openBuyOrders().stream()
.map(order -> order.orderId().equals(event.orderId())
? new OrdersQueryModelState.BuyOrder(order.orderId(), order.market(), order.amount(), order.clientReference(), OrderState.PLACED)
: order)
.toList();
return new OrdersQueryModelState(currentState.userId(), orders);
}

@QueryModelEventHandler(create = false)
public OrdersQueryModelState orderFilled(BuyOrderFilledEvent event, OrdersQueryModelState currentState) {
// Update order state to FILLED
List<OrdersQueryModelState.BuyOrder> orders = currentState.openBuyOrders().stream()
.map(order -> order.orderId().equals(event.orderId())
? new OrdersQueryModelState.BuyOrder(order.orderId(), order.market(), order.amount(), order.clientReference(), OrderState.FILLED)
: order)
.toList();
return new OrdersQueryModelState(currentState.userId(), orders);
}

@QueryModelEventHandler(create = false)
public OrdersQueryModelState orderRejected(BuyOrderRejectedEvent event, OrdersQueryModelState currentState) {
// Update order state to REJECTED
List<OrdersQueryModelState.BuyOrder> orders = currentState.openBuyOrders().stream()
.map(order -> order.orderId().equals(event.orderId())
? new OrdersQueryModelState.BuyOrder(order.orderId(), order.market(), order.amount(), order.clientReference(), OrderState.REJECTED)
: order)
.toList();
return new OrdersQueryModelState(currentState.userId(), orders);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 - 2025 The Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.elasticsoftware.cryptotrading.query;

import org.elasticsoftware.akces.annotations.QueryModelStateInfo;
import org.elasticsoftware.akces.query.QueryModelState;
import org.elasticsoftware.cryptotrading.aggregates.orders.data.CryptoMarket;

import java.math.BigDecimal;
import java.util.List;

@QueryModelStateInfo(type = "Orders")
public record OrdersQueryModelState(String userId, List<BuyOrder> openBuyOrders) implements QueryModelState {
@Override
public String getIndexKey() {
return userId();
}

public record BuyOrder(String orderId, CryptoMarket market, BigDecimal amount, String clientReference, OrderState state) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 - 2025 The Original Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.elasticsoftware.cryptotrading.web;

import org.elasticsoftware.akces.query.models.QueryModelIdNotFoundException;
import org.elasticsoftware.akces.query.models.QueryModelNotFoundException;
import org.elasticsoftware.akces.query.models.QueryModels;
import org.elasticsoftware.cryptotrading.query.OrdersQueryModel;
import org.elasticsoftware.cryptotrading.query.OrdersQueryModelState;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/v{version:1}/accounts")
public class OrdersQueryController {
private final QueryModels queryModels;

public OrdersQueryController(QueryModels queryModels) {
this.queryModels = queryModels;
}

@GetMapping("/{accountId}/orders")
Mono<ResponseEntity<OrdersQueryModelState>> getOpenOrders(@PathVariable("accountId") String accountId) {
return Mono.fromCompletionStage(queryModels.getHydratedState(OrdersQueryModel.class, accountId))
.map(ResponseEntity::ok)
.onErrorResume(throwable -> {
if (throwable instanceof QueryModelNotFoundException || throwable instanceof QueryModelIdNotFoundException) {
return Mono.just(ResponseEntity.notFound().build());
} else {
return Mono.just(ResponseEntity.internalServerError().build());
}
});
}

@GetMapping("/{accountId}/orders/{orderId}")
Mono<ResponseEntity<OrdersQueryModelState.BuyOrder>> getOrderById(
@PathVariable("accountId") String accountId,
@PathVariable("orderId") String orderId) {
return Mono.fromCompletionStage(queryModels.getHydratedState(OrdersQueryModel.class, accountId))
.map(state -> state.openBuyOrders().stream()
.filter(order -> order.orderId().equals(orderId))
.findFirst()
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build()))
.onErrorResume(throwable -> {
if (throwable instanceof QueryModelNotFoundException || throwable instanceof QueryModelIdNotFoundException) {
return Mono.just(ResponseEntity.notFound().build());
} else {
return Mono.just(ResponseEntity.internalServerError().build());
}
});
}
}
Loading
Loading