Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
align with new service api.
  • Loading branch information
JaredGordon committed Sep 2, 2015
1 parent d47210f commit 663fe78
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 134 deletions.
Expand Up @@ -18,8 +18,6 @@
import java.util.List;
import java.util.Set;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.nanotrader.data.domain.Quote;

public interface QuoteService {
Expand All @@ -32,16 +30,14 @@ public interface QuoteService {

List<Quote> findAllQuotes();

Page<Quote> findAllQuotes(PageRequest pageRequest);
List<Quote> topGainers();

List<Quote> findBySymbolIn(Set<String> symbols);
List<Quote> topLosers();

List<Quote> findQuoteEntries(int firstResult, int maxResults);
List<Quote> findBySymbolIn(Set<String> symbols);

void saveQuote(Quote quote);

Quote updateQuote(Quote quote);

Quote findBySymbol(String symbol);

}
Expand Up @@ -15,12 +15,15 @@
*/
package org.springframework.nanotrader.data.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.nanotrader.data.domain.Quote;
import org.springframework.nanotrader.data.repository.QuoteRepository;
import org.springframework.stereotype.Service;
Expand All @@ -31,6 +34,8 @@
@Transactional
public class QuoteServiceImpl implements QuoteService {

public static final Integer TOP_N = 3;

@Autowired
QuoteRepository quoteRepository;

Expand All @@ -51,29 +56,39 @@ public List<Quote> findAllQuotes() {
return quoteRepository.findAll();
}

public List<Quote> findQuoteEntries(int firstResult, int maxResults) {
return quoteRepository.findAll(new org.springframework.data.domain.PageRequest(firstResult / maxResults, maxResults)).getContent();
}

public void saveQuote(Quote quote) {
quoteRepository.save(quote);
}

public Quote updateQuote(Quote quote) {
return quoteRepository.save(quote);
}

public Quote findBySymbol(String symbol) {
return quoteRepository.findBySymbol(symbol);
}

@Override
public List<Quote> findBySymbolIn(Set<String> symbols) {
if(symbols == null || symbols.size() < 1) {
return new ArrayList<Quote>();
}
return quoteRepository.findBySymbolIn(symbols);
}

@Override
public Page<Quote> findAllQuotes(PageRequest pageRequest) {
return quoteRepository.findAll(pageRequest);
public List<Quote> topGainers() {
Page<Quote> winners = quoteRepository.findAll(new PageRequest(0, TOP_N, new Sort(Direction.DESC, "change1")));
List<Quote> l = new ArrayList<Quote>(TOP_N);
for (Quote q : winners) {
l.add(q);
}
return l;
}

@Override
public List<Quote> topLosers() {
Page<Quote> losers = quoteRepository.findAll(new PageRequest(0, TOP_N, new Sort(Direction.ASC, "change1")));
List<Quote> l = new ArrayList<Quote>(TOP_N);
for (Quote q : losers) {
l.add(q);
}
return l;
}
}
Expand Up @@ -16,7 +16,6 @@
package org.springframework.nanotrader.data.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
Expand All @@ -28,10 +27,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.nanotrader.data.domain.Account;
import org.springframework.nanotrader.data.domain.Accountprofile;
import org.springframework.nanotrader.data.domain.Holding;
Expand Down Expand Up @@ -70,8 +66,6 @@ public class TradingServiceImpl implements TradingService {

private static String CANCELLED_STATUS = "cancelled";

private static Integer TOP_N = 3;

@Autowired
private AccountProfileRepository accountProfileRepository;

Expand Down Expand Up @@ -574,22 +568,8 @@ public PortfolioSummary findPortfolioSummary(Integer accountId) {
// TODO: Defensive coding
public MarketSummary findMarketSummary() {
MarketSummary marketSummary = marketSummaryRepository.findMarketSummary();
// get top losing stocks
Page<Quote> losers = quoteService.findAllQuotes(new PageRequest(0, TOP_N, new Sort(Direction.ASC, "change1")));

// get top gaining stocks
Page<Quote> winners = quoteService.findAllQuotes(new PageRequest(0, TOP_N, new Sort(Direction.DESC, "change1")));

List<Quote> topLosers = new ArrayList<Quote>(TOP_N);
for (Quote q : losers) {
topLosers.add(q);
}
List<Quote> topGainers = new ArrayList<Quote>(TOP_N);
for (Quote q : winners) {
topGainers.add(q);
}
marketSummary.setTopLosers(topLosers);
marketSummary.setTopGainers(topGainers);
marketSummary.setTopLosers(quoteService.topLosers());
marketSummary.setTopGainers(quoteService.topGainers());
marketSummary.setSummaryDate(new Date());
return marketSummary;
}
Expand Down
Expand Up @@ -141,7 +141,7 @@ public boolean modifyQuote(Quote obj) {
public void init() {
int from = 0;
int to = 10;
data = quoteService.findQuoteEntries(from, to);
data = quoteService.findAllQuotes();
if (data == null) {
throw new IllegalStateException("Find entries implementation for 'Quote' illegally returned null");
}
Expand Down

This file was deleted.

0 comments on commit 663fe78

Please sign in to comment.