apollo-backend/internal/data/accounts.go

72 lines
1.4 KiB
Go
Raw Normal View History

2021-05-10 00:51:15 +00:00
package data
import (
2021-07-13 14:17:20 +00:00
"context"
2021-05-10 00:51:15 +00:00
"strings"
2021-07-13 14:17:20 +00:00
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
2021-05-10 00:51:15 +00:00
)
type Account struct {
ID int64
Username string
2021-07-12 18:36:08 +00:00
AccountID string
2021-05-10 00:51:15 +00:00
AccessToken string
RefreshToken string
ExpiresAt int64
LastMessageID string
2021-07-07 22:37:10 +00:00
LastCheckedAt float64
2021-05-10 00:51:15 +00:00
}
func (a *Account) NormalizedUsername() string {
return strings.ToLower(a.Username)
}
type AccountModel struct {
2021-07-13 14:17:20 +00:00
ctx context.Context
pool *pgxpool.Pool
2021-05-10 00:51:15 +00:00
}
func (am *AccountModel) Upsert(a *Account) error {
2021-07-13 14:17:20 +00:00
return am.pool.BeginFunc(am.ctx, func(tx pgx.Tx) error {
stmt := `
INSERT INTO accounts (username, account_id, access_token, refresh_token, expires_at, last_message_id, device_count, last_checked_at)
VALUES ($1, $2, $3, $4, $5, '', 0, 0)
ON CONFLICT(username)
DO
UPDATE SET
access_token = $3,
refresh_token = $4,
expires_at = $5,
last_message_id = $6,
last_checked_at = $7
RETURNING id`
return tx.QueryRow(
am.ctx,
stmt,
a.NormalizedUsername(),
a.AccountID,
a.AccessToken,
a.RefreshToken,
a.ExpiresAt,
a.LastMessageID,
a.LastCheckedAt,
).Scan(&a.ID)
})
2021-05-10 00:51:15 +00:00
}
func (am *AccountModel) Delete(id int64) error {
return nil
}
type MockAccountModel struct{}
func (mam *MockAccountModel) Upsert(a *Account) error {
return nil
}
func (mam *MockAccountModel) Delete(id int64) error {
return nil
}