Files
TradarrV2/backend/internal/api/handlers/companies.go
Blomios d65222169a feat(watchlist): add company identity catalog
Embed a pinned FinanceDatabase equity snapshot, add authenticated company search and identity-based follows, preserve legacy assets, and map selected Yahoo symbols for collection.

Refs #19

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-22 15:08:22 +01:00

60 lines
1.7 KiB
Go

package handlers
import (
"github.com/gin-gonic/gin"
"github.com/tradarr/backend/internal/companycatalog"
"github.com/tradarr/backend/internal/httputil"
"net/http"
"strconv"
"strings"
"unicode/utf8"
)
type CompanyCatalog = companycatalog.CompanyCatalog
func (h *Handler) SetCompanyCatalog(c CompanyCatalog) { h.companyCatalog = c }
type companyDTO struct {
CompanyID string `json:"company_id"`
Name string `json:"name"`
Exchange string `json:"exchange"`
Country string `json:"country"`
MIC string `json:"mic"`
}
func (h *Handler) SearchCompanies(c *gin.Context) {
query := strings.TrimSpace(c.Query("q"))
limit := 10
params := c.Request.URL.Query()
if values, ok := params["limit"]; ok {
if len(values) != 1 {
c.JSON(400, gin.H{"error": "invalid search parameters"})
return
}
v, err := strconv.Atoi(values[0])
if err != nil {
c.JSON(400, gin.H{"error": "invalid search parameters"})
return
}
limit = v
}
if len(params["q"]) != 1 || !utf8.ValidString(query) || utf8.RuneCountInString(query) < 1 || utf8.RuneCountInString(query) > 100 || limit < 1 || limit > 20 {
c.JSON(400, gin.H{"error": "invalid search parameters"})
return
}
if h.companyCatalog == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "company catalog unavailable"})
return
}
companies, err := h.companyCatalog.SearchCompanies(query, limit)
if err != nil {
c.JSON(503, gin.H{"error": "company catalog unavailable"})
return
}
results := make([]companyDTO, 0, len(companies))
for _, company := range companies {
results = append(results, companyDTO{company.CompanyID, company.Name, company.Exchange, company.Country, company.MIC})
}
httputil.OK(c, results)
}