feat(assets): track selected indices by FIGI

Add the nullable FIGI migration, owner-scoped asset identity operations, and the explicit index search and selection flow.

Refs #4

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-20 22:31:52 +01:00
parent 5cb45138df
commit 8f42c30ec6
20 changed files with 1082 additions and 147 deletions

View File

@ -1,8 +1,14 @@
package handlers
import (
"errors"
"github.com/gin-gonic/gin"
"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) {
@ -26,8 +32,9 @@ func (h *Handler) GetMyAssets(c *gin.Context) {
}
type addAssetRequest struct {
Symbol string `json:"symbol" binding:"required"`
Name string `json:"name"`
FIGI *string `json:"figi"`
Symbol string `json:"symbol" binding:"required"`
Name string `json:"name"`
}
func (h *Handler) AddMyAsset(c *gin.Context) {
@ -37,19 +44,41 @@ func (h *Handler) AddMyAsset(c *gin.Context) {
httputil.BadRequest(c, err)
return
}
asset, err := h.repo.AddUserAsset(userID, req.Symbol, req.Name)
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 {
httputil.InternalError(c, err)
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")
symbol := c.Param("symbol")
if err := h.repo.RemoveUserAsset(userID, symbol); err != nil {
httputil.InternalError(c, err)
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)