mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-13 07:27:43 +00:00
remove the data package
This commit is contained in:
parent
c17a39b9eb
commit
282ff6ef3e
6 changed files with 4 additions and 227 deletions
|
@ -7,9 +7,10 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/christianselig/apollo-backend/internal/domain"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/christianselig/apollo-backend/internal/domain"
|
||||
)
|
||||
|
||||
func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
@ -8,12 +8,13 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/christianselig/apollo-backend/internal/domain"
|
||||
"github.com/dustin/go-humanize/english"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/sideshow/apns2"
|
||||
"github.com/sideshow/apns2/payload"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/christianselig/apollo-backend/internal/domain"
|
||||
)
|
||||
|
||||
const notificationTitle = "📣 Hello, is this thing on?"
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
ID int64
|
||||
Username string
|
||||
AccountID string
|
||||
AccessToken string
|
||||
RefreshToken string
|
||||
ExpiresAt int64
|
||||
LastMessageID string
|
||||
LastCheckedAt float64
|
||||
}
|
||||
|
||||
func (a *Account) NormalizedUsername() string {
|
||||
return strings.ToLower(a.Username)
|
||||
}
|
||||
|
||||
type AccountModel struct {
|
||||
ctx context.Context
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func (am *AccountModel) Upsert(a *Account) error {
|
||||
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
|
||||
RETURNING id`
|
||||
return tx.QueryRow(
|
||||
am.ctx,
|
||||
stmt,
|
||||
a.NormalizedUsername(),
|
||||
a.AccountID,
|
||||
a.AccessToken,
|
||||
a.RefreshToken,
|
||||
a.ExpiresAt,
|
||||
).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
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type DeviceAccount struct {
|
||||
ID int64
|
||||
AccountID int64
|
||||
DeviceID int64
|
||||
}
|
||||
|
||||
type DeviceAccountModel struct {
|
||||
ctx context.Context
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func (dam *DeviceAccountModel) Associate(accountID int64, deviceID int64) error {
|
||||
stmt := `
|
||||
INSERT INTO devices_accounts (account_id, device_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (account_id, device_id) DO NOTHING
|
||||
RETURNING id`
|
||||
if _, err := dam.pool.Exec(dam.ctx, stmt, accountID, deviceID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update account counter
|
||||
stmt = `
|
||||
UPDATE accounts
|
||||
SET device_count = (
|
||||
SELECT COUNT(*) FROM devices_accounts WHERE account_id = $1
|
||||
)
|
||||
WHERE id = $1`
|
||||
_, err := dam.pool.Exec(dam.ctx, stmt, accountID)
|
||||
return err
|
||||
}
|
||||
|
||||
type MockDeviceAccountModel struct{}
|
||||
|
||||
func (mdam *MockDeviceAccountModel) Associate(accountID int64, deviceID int64) error {
|
||||
return nil
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
ID int64
|
||||
APNSToken string
|
||||
Sandbox bool
|
||||
LastPingedAt int64
|
||||
}
|
||||
|
||||
type DeviceModel struct {
|
||||
ctx context.Context
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func (dm *DeviceModel) Upsert(d *Device) error {
|
||||
d.LastPingedAt = time.Now().Unix()
|
||||
|
||||
return dm.pool.BeginFunc(dm.ctx, func(tx pgx.Tx) error {
|
||||
stmt := `
|
||||
INSERT INTO devices (apns_token, sandbox, last_pinged_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT(apns_token)
|
||||
DO
|
||||
UPDATE SET last_pinged_at = $3
|
||||
RETURNING id`
|
||||
return tx.QueryRow(
|
||||
dm.ctx,
|
||||
stmt,
|
||||
d.APNSToken,
|
||||
d.Sandbox,
|
||||
d.LastPingedAt,
|
||||
).Scan(&d.ID)
|
||||
})
|
||||
}
|
||||
|
||||
func (dm *DeviceModel) GetByAPNSToken(token string) (*Device, error) {
|
||||
device := &Device{}
|
||||
stmt := `
|
||||
SELECT id, apns_token, sandbox, last_pinged_at
|
||||
FROM devices
|
||||
WHERE apns_token = $1`
|
||||
|
||||
if err := dm.pool.QueryRow(dm.ctx, stmt, token).Scan(
|
||||
&device.ID,
|
||||
&device.APNSToken,
|
||||
&device.Sandbox,
|
||||
&device.LastPingedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return device, nil
|
||||
}
|
||||
|
||||
type MockDeviceModel struct{}
|
||||
|
||||
func (mdm *MockDeviceModel) Upsert(d *Device) error {
|
||||
return nil
|
||||
}
|
||||
func (mdm *MockDeviceModel) GetByAPNSToken(token string) (*Device, error) {
|
||||
return nil, nil
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
)
|
||||
|
||||
type Models struct {
|
||||
Accounts interface {
|
||||
Upsert(a *Account) error
|
||||
Delete(id int64) error
|
||||
}
|
||||
|
||||
Devices interface {
|
||||
Upsert(*Device) error
|
||||
GetByAPNSToken(string) (*Device, error)
|
||||
}
|
||||
|
||||
DevicesAccounts interface {
|
||||
Associate(int64, int64) error
|
||||
}
|
||||
}
|
||||
|
||||
func NewModels(ctx context.Context, pool *pgxpool.Pool) *Models {
|
||||
return &Models{
|
||||
Accounts: &AccountModel{ctx, pool},
|
||||
Devices: &DeviceModel{ctx, pool},
|
||||
DevicesAccounts: &DeviceAccountModel{ctx, pool},
|
||||
}
|
||||
}
|
||||
|
||||
func NewMockModels() *Models {
|
||||
return &Models{
|
||||
Accounts: &MockAccountModel{},
|
||||
Devices: &MockDeviceModel{},
|
||||
DevicesAccounts: &MockDeviceAccountModel{},
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue