Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored orderbook publishing with DTOs #68

Merged
merged 1 commit into from
Aug 8, 2024
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
Expand Up @@ -49,7 +49,7 @@ public ResponseEntity<OpenOrderDTO> receiveUpdate(@RequestBody OpenOrderDTO payl
}

natsService.publish(Event.MARKET_DATA_UPDATE, dataUpdate);
orderBookService.publishOrderBook(payload.getProduct(), exchange);
orderBookService.publishOrderBook(payload.getProduct(), exchange.toLowerCase());
mdService.updateMarketData(dataUpdate.get(ticker));

dataUpdate.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
public interface OrderBookService {
void publishOrderBook() throws JsonProcessingException;
void publishOrderBook(String product) throws JsonProcessingException;
void publishOrderBook(String product, String exchange);
void publishOrderBook(String product, String exchange) throws JsonProcessingException;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.joe.trading.marketdataservice.services.orderbook;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.joe.trading.marketdataservice.model.OrderBook;
import com.joe.trading.marketdataservice.services.enums.Ticker;
import com.joe.trading.shared.dtos.OrderBook;
import com.joe.trading.shared.dtos.OrderBookUpdateDto;
import com.joe.trading.shared.events.Event;
import com.joe.trading.shared.nats.NatsService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -48,94 +49,73 @@ public void publishOrderBook() throws JsonProcessingException {

@Override
public void publishOrderBook(String product) throws JsonProcessingException{
fullOrderBook.putAll(getOpenOrders(product));
fullOrderBook.putAll(getClosedOrders(product));

publishEvent(product);
this.publishOrderBook(product, exchange1Url);
this.publishOrderBook(product, exchange2Url);
}

@Override
public void publishOrderBook(String product, String exchange){
fullOrderBook.putAll(getOpenAndClosedOrdersFromOneExchange(product, exchange));

publishEvent(product);
public void publishOrderBook(String product, String exchange) throws JsonProcessingException {
getOpenAndClosedOrdersFromOneExchange(product, exchange);
}

private void publishEvent(String product){
private void publishEvent(String product, OrderBookUpdateDto book){
switch (Ticker.valueOf(product.toUpperCase())){
case IBM -> this.publishOne(Event.IBM_ORDER_BOOK);
case AAPL -> this.publishOne(Event.AAPL_ORDER_BOOK);
case AMZN -> this.publishOne(Event.AMZN_ORDER_BOOK);
case MSFT -> this.publishOne(Event.MSFT_ORDER_BOOK);
case NFLX -> this.publishOne(Event.NFLX_ORDER_BOOK);
case ORCL -> this.publishOne(Event.ORCL_ORDER_BOOK);
case TSLA -> this.publishOne(Event.TSLA_ORDER_BOOK);
case GOOGL -> this.publishOne(Event.GOOGL_ORDER_BOOK);
case IBM -> this.publishOne(Event.IBM_ORDER_BOOK, book);
case AAPL -> this.publishOne(Event.AAPL_ORDER_BOOK, book);
case AMZN -> this.publishOne(Event.AMZN_ORDER_BOOK, book);
case MSFT -> this.publishOne(Event.MSFT_ORDER_BOOK, book);
case NFLX -> this.publishOne(Event.NFLX_ORDER_BOOK, book);
case ORCL -> this.publishOne(Event.ORCL_ORDER_BOOK, book);
case TSLA -> this.publishOne(Event.TSLA_ORDER_BOOK, book);
case GOOGL -> this.publishOne(Event.GOOGL_ORDER_BOOK, book);
}
}


private void publishOne(Event event){
private void publishOne(Event event, OrderBookUpdateDto book){
try {
natsService.publish(event, fullOrderBook);
fullOrderBook.clear();
natsService.publish(event, book);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private List<OrderBook> getFromExchange(String baseUrl, String ticker, String state){
private OrderBookUpdateDto getFromExchange(String baseUrl, String ticker, String state, String key){
String url = baseUrl+"/orderbook/"+ticker+"/"+state;
ResponseEntity<OrderBook[]> response = restTemplate.getForEntity(url, OrderBook[].class);

return Arrays.asList(response.getBody());
}

private Map<String, List<OrderBook>> getOpenOrders(String ticker){
String ex1Key = ticker+"_EX1_OPEN";
String ex2Key = ticker+"_EX2_OPEN";

Map<String, List<OrderBook>> result = new HashMap<>();

result.put(ex1Key, getFromExchange(exchange1Url, ticker, "open"));
result.put(ex2Key, getFromExchange(exchange2Url, ticker, "open"));

return result;
}

private Map<String, List<OrderBook>> getClosedOrders(String ticker){
String ex1Key = ticker+"_EX1_CLOSED";
String ex2Key = ticker+"_EX2_CLOSED";

Map<String, List<OrderBook>> result = new HashMap<>();
OrderBookUpdateDto book = new OrderBookUpdateDto();
book.setOrderBooks(Arrays.stream(response.getBody()).toList());
book.setSourceExchange(key);

result.put(ex1Key, getFromExchange(exchange1Url, ticker, "closed"));
result.put(ex2Key, getFromExchange(exchange2Url, ticker, "closed"));

return result;
return book;
}

private Map<String, List<OrderBook>> getOpenAndClosedOrdersFromOneExchange(String ticker, String exchange){
private void getOpenAndClosedOrdersFromOneExchange(String ticker, String exchange){
String openKey;
String closedKey;

Map<String, List<OrderBook>> result = new HashMap<>();

if (exchange1Url.contains(exchange.toLowerCase())){
openKey = ticker+"_EX1_OPEN";
closedKey = ticker+"_EX1_CLOSED";

result.put(openKey, getFromExchange(exchange1Url, ticker, "open"));
result.put(closedKey, getFromExchange(exchange1Url, ticker, "closed"));
OrderBookUpdateDto book1 = getFromExchange(exchange1Url, ticker, "open", openKey);
OrderBookUpdateDto book2 = getFromExchange(exchange1Url, ticker, "closed", closedKey);

this.publishEvent(ticker, book1);
this.publishEvent(ticker, book2);

}
else{
openKey = ticker+"_EX2_OPEN";
closedKey = ticker+"_EX2_CLOSED";

result.put(openKey, getFromExchange(exchange2Url, ticker, "open"));
result.put(closedKey, getFromExchange(exchange2Url, ticker, "closed"));
}
OrderBookUpdateDto book1 = getFromExchange(exchange2Url, ticker, "open", openKey);
OrderBookUpdateDto book2 = getFromExchange(exchange2Url, ticker, "closed", closedKey);

return result;
this.publishEvent(ticker, book1);
this.publishEvent(ticker, book2);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.joe.trading.order_processing.config;

import com.joe.trading.order_processing.entities.User;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -9,10 +11,6 @@
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import com.joe.trading.order_processing.entities.User;

import lombok.AllArgsConstructor;

@Configuration
@AllArgsConstructor
public class ApplicationConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.joe.trading.order_processing.config;

import java.io.IOException;
import java.util.Base64;

import org.springframework.lang.NonNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.joe.trading.order_processing.entities.User;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.lang.NonNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import java.io.IOException;
import java.util.Base64;

@Component
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.joe.trading.order_processing.config;

import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
Expand All @@ -10,8 +11,6 @@
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import lombok.AllArgsConstructor;

@Configuration
@EnableWebSecurity
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
package com.joe.trading.order_processing.controllers;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.joe.trading.order_processing.entities.Order;
import com.joe.trading.order_processing.entities.User;
import com.joe.trading.order_processing.entities.dto.OrderRequestDTO;
Expand All @@ -25,6 +10,14 @@
import com.joe.trading.order_processing.entities.enums.Ticker;
import com.joe.trading.order_processing.services.OrderService;
import com.joe.trading.order_processing.services.validation.OrderValidationService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/orders")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package com.joe.trading.order_processing.controllers;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.joe.trading.order_processing.entities.User;
import com.joe.trading.order_processing.entities.dto.CreatePortfolioRequestDTO;
import com.joe.trading.order_processing.entities.dto.PortfolioFilterRequestDto;
Expand All @@ -20,8 +8,12 @@
import com.joe.trading.order_processing.services.PortfolioService;
import com.joe.trading.shared.dtos.PaginatedResponseDto;
import com.joe.trading.shared.exceptions.ResourceNotFoundException;

import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.joe.trading.order_processing.entities;

import com.joe.trading.order_processing.entities.dto.PortfolioResponseDTO;
import com.joe.trading.order_processing.entities.enums.PortfolioState;
import com.joe.trading.order_processing.entities.enums.Ticker;
import com.joe.trading.shared.dtos.PortfolioEventDto;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,7 +11,9 @@
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package com.joe.trading.order_processing.entities;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import com.joe.trading.shared.auth.AccountType;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.joe.trading.shared.auth.AccountType;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Entity(name = "users")
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.joe.trading.order_processing.entities.dto;

import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.*;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.joe.trading.order_processing.entities.dto;

import java.time.LocalDateTime;

import jakarta.validation.constraints.Min;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
Expand Down
Loading
Loading