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>
86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
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) {
|
|
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" binding:"required"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func (h *Handler) AddMyAsset(c *gin.Context) {
|
|
userID := c.GetString("userID")
|
|
var req addAssetRequest
|
|
if err := c.ShouldBindJSON(&req); 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)
|
|
}
|