60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
return instruments, nil
|
|
}
|
|
|
|
func (uc *Usecase) CreateInstrument(ctx context.Context, instrument *entities.Instrument) (entities.InstrumentID, error) {
|
|
id, err := uc.storage.CreateInstrument(ctx, instrument)
|
|
if err != nil {
|
|
uc.log.Error("failed to create instrument", "instrument", instrument, "err", err)
|
|
return "", fmt.Errorf("failed to create instrument: %w", err)
|
|
}
|
|
|
|
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
|
|
}
|