File size: 2,717 Bytes
f5cd2d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.rods.backtestingstrategies.controller;

import com.rods.backtestingstrategies.entity.StockSymbol;
import com.rods.backtestingstrategies.repository.StockSymbolRepository;
import com.rods.backtestingstrategies.service.MarketDataService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/symbols")
@CrossOrigin
public class SymbolController {

    private final MarketDataService marketDataService;
    private final StockSymbolRepository symbolRepository;

    public SymbolController(MarketDataService marketDataService,
                            StockSymbolRepository symbolRepository) {
        this.marketDataService = marketDataService;
        this.symbolRepository = symbolRepository;
    }

    /**
     * Search symbols by keyword (matches symbol prefix or company name).
     * Optionally filter by exchange.
     */
    @GetMapping("/search")
    public List<StockSymbol> searchSymbols(
            @RequestParam String query,
            @RequestParam(required = false) String exchange
    ) {
        System.out.println("Symbol search request: query=" + query + ", exchange=" + exchange);

        if (query == null || query.length() < 1) {
            return List.of();
        }

        if (exchange != null && !exchange.isBlank()) {
            return marketDataService.searchSymbolsByExchange(query.trim(), exchange.trim());
        }

        return marketDataService.searchSymbols(query.trim());
    }

    /**
     * Get all symbols for a specific exchange.
     */
    @GetMapping("/exchange/{exchange}")
    public List<StockSymbol> getByExchange(@PathVariable String exchange) {
        return marketDataService.getSymbolsByExchange(exchange);
    }

    /**
     * Get symbols filtered by sector.
     */
    @GetMapping("/sector/{sector}")
    public List<StockSymbol> getBySector(@PathVariable String sector) {
        return symbolRepository.findBySector(sector);
    }

    /**
     * Get all available exchanges.
     */
    @GetMapping("/exchanges")
    public List<String> getExchanges() {
        return symbolRepository.findAllExchanges();
    }

    /**
     * Get all available sectors.
     */
    @GetMapping("/sectors")
    public List<String> getSectors() {
        return symbolRepository.findAllSectors();
    }

    /**
     * Get summary stats about the ticker database.
     */
    @GetMapping("/stats")
    public Map<String, Object> getStats() {
        return Map.of(
                "totalSymbols", symbolRepository.count(),
                "exchanges", symbolRepository.findAllExchanges(),
                "sectors", symbolRepository.findAllSectors()
        );
    }
}