Skip to content

Commit 6e91e99

Browse files
authored
Merge pull request #2 from robherley/robherley/adjust-notify-time
add option to adjust notify time before auction ends
2 parents 96dd2bd + 08be807 commit 6e91e99

File tree

8 files changed

+62
-26
lines changed

8 files changed

+62
-26
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
ALTER TABLE subscriptions
4+
ADD COLUMN notify_minutes INTEGER NOT NULL DEFAULT 10;
5+
-- +goose StatementEnd
6+
7+
-- +goose Down
8+
-- +goose StatementBegin
9+
ALTER TABLE subscriptions
10+
DROP COLUMN notify_minutes;
11+
-- +goose StatementEnd

database/queries/items.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ SELECT EXISTS (
1111
) AS is_tracked;
1212

1313
-- name: FindItemsEndingSoon :many
14-
SELECT * FROM items
15-
WHERE ends_at < datetime('now', '+10 minutes') AND sent_final = FALSE
14+
SELECT i.* FROM items i
15+
JOIN subscriptions s ON i.subscription_id = s.id
16+
WHERE i.ends_at < datetime('now', '+' || s.notify_minutes || ' minutes') AND i.sent_final = FALSE
1617
LIMIT 100;
1718

1819
-- name: SetItemSentFinal :exec

database/queries/subscriptions.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: CreateSubscription :one
2-
INSERT INTO subscriptions (id, user_id, term, last_notified_at, min_price, max_price)
3-
VALUES (?, ?, ?, 0, ?, ?)
2+
INSERT INTO subscriptions (id, user_id, term, last_notified_at, min_price, max_price, notify_minutes)
3+
VALUES (?, ?, ?, 0, ?, ?, ?)
44
RETURNING *;
55

66
-- name: FindSubscription :one

internal/bot/cmd/subscribe.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (cmd *Subscribe) Description() string {
3333
func (cmd *Subscribe) Options() []*discordgo.ApplicationCommandOption {
3434
termMinLength := 1
3535
termMaxLength := 100
36+
notifyMinValue := float64(1)
3637
return []*discordgo.ApplicationCommandOption{
3738
{
3839
Type: discordgo.ApplicationCommandOptionString,
@@ -54,6 +55,13 @@ func (cmd *Subscribe) Options() []*discordgo.ApplicationCommandOption {
5455
Description: "Maximum price to alert on",
5556
Required: false,
5657
},
58+
{
59+
Type: discordgo.ApplicationCommandOptionInteger,
60+
Name: "notify",
61+
Description: "How many minutes before the auction ends to send a notification",
62+
Required: false,
63+
MinValue: &notifyMinValue,
64+
},
5765
// TODO: category
5866
}
5967
}
@@ -69,9 +77,10 @@ func (cmd *Subscribe) Handle(ctx context.Context, s *discordgo.Session, i *disco
6977
data := i.ApplicationCommandData()
7078

7179
var (
72-
term string
73-
minPrice *int64
74-
maxPrice *int64
80+
term string
81+
minPrice *int64
82+
maxPrice *int64
83+
notifyMinutes int64 = 10
7584
)
7685

7786
for _, option := range data.Options {
@@ -84,6 +93,8 @@ func (cmd *Subscribe) Handle(ctx context.Context, s *discordgo.Session, i *disco
8493
case "max":
8594
max := option.IntValue()
8695
maxPrice = &max
96+
case "notify":
97+
notifyMinutes = option.IntValue()
8798
}
8899
}
89100

@@ -113,11 +124,12 @@ func (cmd *Subscribe) Handle(ctx context.Context, s *discordgo.Session, i *disco
113124
}
114125

115126
sub, err := cmd.db.CreateSubscription(ctx, sqlgen.CreateSubscriptionParams{
116-
ID: db.NewID(),
117-
UserID: userID,
118-
Term: term,
119-
MinPrice: minPrice,
120-
MaxPrice: maxPrice,
127+
ID: db.NewID(),
128+
UserID: userID,
129+
Term: term,
130+
MinPrice: minPrice,
131+
MaxPrice: maxPrice,
132+
NotifyMinutes: notifyMinutes,
121133
})
122134

123135
if err != nil {

internal/bot/cmd/subscriptions.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (cmd *Subscriptions) Handle(ctx context.Context, s *discordgo.Session, i *d
5252
builder.WriteString(sub.Term)
5353

5454
if sub.MinPrice != nil || sub.MaxPrice != nil {
55-
builder.WriteString("$")
55+
builder.WriteString(" $")
5656
if sub.MinPrice != nil {
5757
builder.WriteString(strconv.FormatInt(*sub.MinPrice, 10))
5858
} else {
@@ -67,6 +67,10 @@ func (cmd *Subscriptions) Handle(ctx context.Context, s *discordgo.Session, i *d
6767
builder.WriteString(" ")
6868
}
6969

70+
builder.WriteString(" ⏲️ ")
71+
builder.WriteString(strconv.FormatInt(sub.NotifyMinutes, 10))
72+
builder.WriteString("m")
73+
7074
builder.WriteString("\n")
7175
}
7276
}

internal/db/sqlgen/items.sql.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/sqlgen/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/db/sqlgen/subscriptions.sql.go

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)