user instruments

This commit is contained in:
yash 2026-04-28 11:42:07 +03:00
parent dd03cae0f3
commit abb2411af7
9 changed files with 494 additions and 48 deletions

View file

@ -3,12 +3,13 @@ package usecase
import (
"context"
"fmt"
"strings"
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
)
func (uc *Usecase) InstrumentList(ctx context.Context, offset, limit int) ([]entities.Instrument, error) {
instruments, err := uc.storage.InstrumentList(ctx, offset, limit)
func (uc *Usecase) InstrumentList(ctx context.Context, userID entities.UserID, offset, limit int) ([]entities.Instrument, error) {
instruments, err := uc.storage.InstrumentList(ctx, userID, offset, limit)
if err != nil {
uc.log.Error("failed to list instruments", "offset", offset, "limit", limit, "err", err)
return nil, fmt.Errorf("failed to list instruments: %w", err)
@ -26,3 +27,34 @@ func (uc *Usecase) CreateInstrument(ctx context.Context, instrument *entities.In
return id, nil
}
func (uc *Usecase) RemoveUserInstrument(ctx context.Context, userID entities.UserID, instrumentID entities.InstrumentID) error {
if err := uc.storage.RemoveUserInstrument(ctx, userID, instrumentID); err != nil {
uc.log.Error("failed to remove user instrument", "user_id", userID, "instrument_id", instrumentID, "err", err)
return fmt.Errorf("failed to remove user instrument: %w", err)
}
return nil
}
// AddUserInstrument ensures the instrument exists in the DB (creating it if needed)
// and links it to the given user. Returns the instrument with its ID filled in.
func (uc *Usecase) AddUserInstrument(ctx context.Context, userID entities.UserID, base, quote string) (*entities.Instrument, error) {
base = strings.ToUpper(strings.TrimSpace(base))
quote = strings.ToUpper(strings.TrimSpace(quote))
instr := &entities.Instrument{BaseCurrency: base, QuoteCurrency: quote}
id, err := uc.storage.CreateInstrument(ctx, instr)
if err != nil {
uc.log.Error("failed to upsert instrument", "base", base, "quote", quote, "err", err)
return nil, fmt.Errorf("failed to upsert instrument: %w", err)
}
instr.ID = id
if err := uc.storage.AddUserInstrument(ctx, userID, id); err != nil {
uc.log.Error("failed to add user instrument", "user_id", userID, "instrument_id", id, "err", err)
return nil, fmt.Errorf("failed to add user instrument: %w", err)
}
return instr, nil
}