From 282ff6ef3ec1fe43c65b4efce25bc7e25eaf57ea Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 14 Aug 2021 13:56:03 -0400 Subject: [PATCH] remove the data package --- internal/api/accounts.go | 3 +- internal/api/devices.go | 3 +- internal/data/accounts.go | 67 ------------------------------- internal/data/device_accounts.go | 45 --------------------- internal/data/devices.go | 69 -------------------------------- internal/data/models.go | 44 -------------------- 6 files changed, 4 insertions(+), 227 deletions(-) delete mode 100644 internal/data/accounts.go delete mode 100644 internal/data/device_accounts.go delete mode 100644 internal/data/devices.go delete mode 100644 internal/data/models.go diff --git a/internal/api/accounts.go b/internal/api/accounts.go index 5c762c0..99a2a99 100644 --- a/internal/api/accounts.go +++ b/internal/api/accounts.go @@ -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) { diff --git a/internal/api/devices.go b/internal/api/devices.go index 1ac6cc0..2b90578 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -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?" diff --git a/internal/data/accounts.go b/internal/data/accounts.go deleted file mode 100644 index 68bdf00..0000000 --- a/internal/data/accounts.go +++ /dev/null @@ -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 -} diff --git a/internal/data/device_accounts.go b/internal/data/device_accounts.go deleted file mode 100644 index 8715c4f..0000000 --- a/internal/data/device_accounts.go +++ /dev/null @@ -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 -} diff --git a/internal/data/devices.go b/internal/data/devices.go deleted file mode 100644 index 6ab6151..0000000 --- a/internal/data/devices.go +++ /dev/null @@ -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 -} diff --git a/internal/data/models.go b/internal/data/models.go deleted file mode 100644 index c615802..0000000 --- a/internal/data/models.go +++ /dev/null @@ -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{}, - } -}