apollo-backend/internal/data/accounts.go

58 lines
1.2 KiB
Go
Raw Normal View History

2021-05-10 00:51:15 +00:00
package data
import (
"database/sql"
"strings"
)
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 {
DB *sql.DB
}
func (am *AccountModel) Upsert(a *Account) error {
query := `
INSERT INTO accounts (username, account_id, access_token, refresh_token, expires_at, last_message_id, device_count, last_checked_at)
2021-07-12 19:09:40 +00:00
VALUES ($1, $2, $3, $4, $5, '', 0, 0)
2021-05-10 00:51:15 +00:00
ON CONFLICT(username)
DO
2021-07-12 19:09:40 +00:00
UPDATE SET
access_token = $3,
refresh_token = $4,
expires_at = $5,
last_message_id = $6,
last_checked_at = $7
2021-05-10 00:51:15 +00:00
RETURNING id`
args := []interface{}{a.NormalizedUsername(), a.AccountID, a.AccessToken, a.RefreshToken, a.ExpiresAt, a.LastMessageID, a.LastCheckedAt}
2021-05-10 00:51:15 +00:00
return am.DB.QueryRow(query, args...).Scan(&a.ID)
}
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
}