updated stuff
This commit is contained in:
50
internal/db/discord_tokens.go
Normal file
50
internal/db/discord_tokens.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.haelnorr.com/h/oslstats/internal/discord"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type DiscordToken struct {
|
||||
bun.BaseModel `bun:"table:discord_tokens,alias:dt"`
|
||||
|
||||
DiscordID string `bun:"discord_id,pk,notnull"`
|
||||
AccessToken string `bun:"access_token,notnull"`
|
||||
RefreshToken string `bun:"refresh_token,notnull"`
|
||||
ExpiresAt int64 `bun:"expires_at,notnull"`
|
||||
}
|
||||
|
||||
func UpdateDiscordToken(ctx context.Context, db *bun.DB, user *discordgo.User, token *discord.Token) error {
|
||||
if db == nil {
|
||||
return errors.New("db cannot be nil")
|
||||
}
|
||||
if user == nil {
|
||||
return errors.New("user cannot be nil")
|
||||
}
|
||||
if token == nil {
|
||||
return errors.New("token cannot be nil")
|
||||
}
|
||||
expiresAt := time.Now().Add(time.Duration(token.ExpiresIn) * time.Second).Unix()
|
||||
|
||||
discordToken := &DiscordToken{
|
||||
DiscordID: user.ID,
|
||||
AccessToken: token.AccessToken,
|
||||
RefreshToken: token.RefreshToken,
|
||||
ExpiresAt: expiresAt,
|
||||
}
|
||||
|
||||
_, err := db.NewInsert().
|
||||
Model(discordToken).
|
||||
On("CONFLICT (discord_id) DO UPDATE").
|
||||
Set("access_token = EXCLUDED.access_token").
|
||||
Set("refresh_token = EXCLUDED.refresh_token").
|
||||
Set("expires_at = EXCLUDED.expires_at").
|
||||
Exec(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user