Add full monorepo: virtual-banker, backend, frontend, docs, scripts, deployment
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
74
backend/api/watchlists/watchlists.go
Normal file
74
backend/api/watchlists/watchlists.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package watchlists
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// WatchlistService handles watchlist operations
|
||||
type WatchlistService struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
// NewWatchlistService creates a new watchlist service
|
||||
func NewWatchlistService(db *pgxpool.Pool) *WatchlistService {
|
||||
return &WatchlistService{db: db}
|
||||
}
|
||||
|
||||
// AddToWatchlist adds an address to a user's watchlist
|
||||
func (w *WatchlistService) AddToWatchlist(ctx context.Context, userID string, chainID int, address, label string) error {
|
||||
query := `
|
||||
INSERT INTO watchlists (user_id, chain_id, address, label)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (user_id, chain_id, address) DO UPDATE SET
|
||||
label = $4
|
||||
`
|
||||
|
||||
_, err := w.db.Exec(ctx, query, userID, chainID, address, label)
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveFromWatchlist removes an address from watchlist
|
||||
func (w *WatchlistService) RemoveFromWatchlist(ctx context.Context, userID string, chainID int, address string) error {
|
||||
query := `DELETE FROM watchlists WHERE user_id = $1 AND chain_id = $2 AND address = $3`
|
||||
_, err := w.db.Exec(ctx, query, userID, chainID, address)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetWatchlist gets a user's watchlist
|
||||
func (w *WatchlistService) GetWatchlist(ctx context.Context, userID string, chainID int) ([]WatchlistItem, error) {
|
||||
query := `
|
||||
SELECT chain_id, address, label, created_at
|
||||
FROM watchlists
|
||||
WHERE user_id = $1 AND chain_id = $2
|
||||
ORDER BY created_at DESC
|
||||
`
|
||||
|
||||
rows, err := w.db.Query(ctx, query, userID, chainID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query watchlist: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []WatchlistItem
|
||||
for rows.Next() {
|
||||
var item WatchlistItem
|
||||
if err := rows.Scan(&item.ChainID, &item.Address, &item.Label, &item.CreatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// WatchlistItem represents a watchlist item
|
||||
type WatchlistItem struct {
|
||||
ChainID int
|
||||
Address string
|
||||
Label string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user