Files
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

112 lines
2.9 KiB
Go

package handlers
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/tradarr/backend/internal/httputil"
"github.com/tradarr/backend/internal/models"
"net/http"
"regexp"
"strings"
"unicode/utf8"
)
func (h *Handler) GetMe(c *gin.Context) {
userID := c.GetString("userID")
user, err := h.repo.GetUserByID(userID)
if err != nil || user == nil {
httputil.NotFound(c)
return
}
httputil.OK(c, user)
}
func (h *Handler) GetMyAssets(c *gin.Context) {
userID := c.GetString("userID")
assets, err := h.repo.GetUserAssets(userID)
if err != nil {
httputil.InternalError(c, err)
return
}
httputil.OK(c, assets)
}
type addAssetRequest struct {
FIGI *string `json:"figi"`
Symbol string `json:"symbol"`
Name string `json:"name"`
}
func (h *Handler) AddMyAsset(c *gin.Context) {
userID := c.GetString("userID")
var identity struct {
CompanyID *string `json:"company_id"`
}
if err := c.ShouldBindBodyWith(&identity, binding.JSON); err != nil {
httputil.BadRequest(c, err)
return
}
if identity.CompanyID != nil {
if h.companyCatalog == nil {
c.JSON(503, gin.H{"error": "company catalog unavailable"})
return
}
company, err := h.companyCatalog.ResolveCompany(strings.TrimSpace(*identity.CompanyID))
if err != nil {
c.JSON(400, gin.H{"error": "unknown company_id"})
return
}
asset, err := h.repo.UpsertCompanyAsset(userID, company)
if err != nil {
c.JSON(500, gin.H{"error": "asset operation failed"})
return
}
httputil.Created(c, asset)
return
}
var req addAssetRequest
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
httputil.BadRequest(c, err)
return
}
req.Symbol = strings.ToUpper(strings.TrimSpace(req.Symbol))
req.Name = strings.TrimSpace(req.Name)
if req.FIGI != nil {
value := strings.TrimSpace(*req.FIGI)
req.FIGI = &value
}
if req.Symbol == "" || !utf8.ValidString(req.Symbol) || utf8.RuneCountInString(req.Symbol) > 20 || (req.FIGI != nil && (*req.FIGI == "" || req.Name == "")) {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid asset fields"})
return
}
asset, err := h.repo.UpsertUserAsset(userID, req.Symbol, req.Name, req.FIGI)
if errors.Is(err, models.ErrAmbiguousAsset) {
c.JSON(http.StatusConflict, gin.H{"error": "ambiguous legacy asset"})
return
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "asset operation failed"})
return
}
httputil.Created(c, asset)
}
var assetUUID = regexp.MustCompile(`^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$`)
func (h *Handler) RemoveMyAsset(c *gin.Context) {
userID := c.GetString("userID")
identity := c.Param("id")
var err error
if assetUUID.MatchString(identity) {
err = h.repo.RemoveUserAssetByID(userID, identity)
} else {
err = h.repo.RemoveUserAsset(userID, identity)
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "asset operation failed"})
return
}
httputil.NoContent(c)
}