candle close alert

This commit is contained in:
yash 2026-04-27 20:34:48 +03:00
parent 30a7f1b68c
commit dd03cae0f3
10 changed files with 328 additions and 60 deletions

View file

@ -9,14 +9,19 @@ import (
)
const saveAlertQuery = `
insert into alert(user_id, instrument_id, price, condition)
values ($1, $2, $3, $4)
insert into alert(user_id, instrument_id, price, condition, timeframe)
values ($1, $2, $3, $4, $5)
returning id`
func (p *Postgresql) SaveAlert(ctx context.Context, alert *entities.Alert) (entities.AlertID, error) {
var id entities.AlertID
err := p.db.QueryRow(ctx, saveAlertQuery, alert.UserID, alert.Instrument.ID, alert.Price.String(), alert.Condition).Scan(&id)
var timeframe *string
if alert.Timeframe != "" {
timeframe = &alert.Timeframe
}
err := p.db.QueryRow(ctx, saveAlertQuery, alert.UserID, alert.Instrument.ID, alert.Price.String(), alert.Condition, timeframe).Scan(&id)
if err != nil {
return "", fmt.Errorf("failed to exec saveAlertQuery: %w", err)
}
@ -25,13 +30,13 @@ func (p *Postgresql) SaveAlert(ctx context.Context, alert *entities.Alert) (enti
}
const allActiveAlertsQuery = `
select a.id, a.user_id, a.price, a.condition, i.id, c_base.symbol, c_quote.symbol
select a.id, a.user_id, a.price, a.condition, a.timeframe, i.id, c_base.symbol, c_quote.symbol
from alert a
join instrument i on i.id = a.instrument_id
join currency c_base on c_base.id = i.base_currency_id
join currency c_quote on c_quote.id = i.quoted_currency_id
where a.active = true
order by a.id`
order by a.created_at desc`
func (p *Postgresql) AllActiveAlerts(ctx context.Context) ([]entities.Alert, error) {
rows, err := p.db.Query(ctx, allActiveAlertsQuery)
@ -44,9 +49,10 @@ func (p *Postgresql) AllActiveAlerts(ctx context.Context) ([]entities.Alert, err
for rows.Next() {
var alert entities.Alert
var priceStr string
var timeframe *string
if err := rows.Scan(
&alert.ID, &alert.UserID, &priceStr, &alert.Condition,
&alert.ID, &alert.UserID, &priceStr, &alert.Condition, &timeframe,
&alert.Instrument.ID, &alert.Instrument.BaseCurrency, &alert.Instrument.QuoteCurrency,
); err != nil {
return nil, fmt.Errorf("failed to scan alert row: %w", err)
@ -57,6 +63,10 @@ func (p *Postgresql) AllActiveAlerts(ctx context.Context) ([]entities.Alert, err
return nil, fmt.Errorf("failed to parse alert price: %w", err)
}
if timeframe != nil {
alert.Timeframe = *timeframe
}
alerts = append(alerts, alert)
}
@ -64,7 +74,7 @@ func (p *Postgresql) AllActiveAlerts(ctx context.Context) ([]entities.Alert, err
}
const alertByIDQuery = `
select a.id, a.user_id, a.price, a.condition, i.id, c_base.symbol, c_quote.symbol
select a.id, a.user_id, a.price, a.condition, a.timeframe, i.id, c_base.symbol, c_quote.symbol
from alert a
join instrument i on i.id = a.instrument_id
join currency c_base on c_base.id = i.base_currency_id
@ -74,9 +84,10 @@ where a.id = $1 and a.active = true`
func (p *Postgresql) AlertByID(ctx context.Context, id entities.AlertID) (*entities.Alert, error) {
var alert entities.Alert
var priceStr string
var timeframe *string
err := p.db.QueryRow(ctx, alertByIDQuery, id).Scan(
&alert.ID, &alert.UserID, &priceStr, &alert.Condition,
&alert.ID, &alert.UserID, &priceStr, &alert.Condition, &timeframe,
&alert.Instrument.ID, &alert.Instrument.BaseCurrency, &alert.Instrument.QuoteCurrency,
)
if err != nil {
@ -88,17 +99,21 @@ func (p *Postgresql) AlertByID(ctx context.Context, id entities.AlertID) (*entit
return nil, fmt.Errorf("failed to parse alert price: %w", err)
}
if timeframe != nil {
alert.Timeframe = *timeframe
}
return &alert, nil
}
const alertsByUserIDQuery = `
select a.id, a.user_id, a.price, a.condition, i.id, c_base.symbol, c_quote.symbol
select a.id, a.user_id, a.price, a.condition, a.timeframe, i.id, c_base.symbol, c_quote.symbol
from alert a
join instrument i on i.id = a.instrument_id
join currency c_base on c_base.id = i.base_currency_id
join currency c_quote on c_quote.id = i.quoted_currency_id
where a.user_id = $1 and a.active = true
order by a.id
order by a.created_at desc
offset $2 limit $3`
func (p *Postgresql) AlertsByUserID(ctx context.Context, userID entities.UserID, offset, limit int) ([]entities.Alert, error) {
@ -112,9 +127,10 @@ func (p *Postgresql) AlertsByUserID(ctx context.Context, userID entities.UserID,
for rows.Next() {
var alert entities.Alert
var priceStr string
var timeframe *string
if err := rows.Scan(
&alert.ID, &alert.UserID, &priceStr, &alert.Condition,
&alert.ID, &alert.UserID, &priceStr, &alert.Condition, &timeframe,
&alert.Instrument.ID, &alert.Instrument.BaseCurrency, &alert.Instrument.QuoteCurrency,
); err != nil {
return nil, fmt.Errorf("failed to scan alert row: %w", err)
@ -125,6 +141,10 @@ func (p *Postgresql) AlertsByUserID(ctx context.Context, userID entities.UserID,
return nil, fmt.Errorf("failed to parse alert price: %w", err)
}
if timeframe != nil {
alert.Timeframe = *timeframe
}
alerts = append(alerts, alert)
}