apollo-backend/internal/repository/postgres_device.go

118 lines
2.5 KiB
Go
Raw Normal View History

2021-07-26 16:34:26 +00:00
package repository
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/christianselig/apollo-backend/internal/domain"
)
type postgresDeviceRepository struct {
pool *pgxpool.Pool
}
func NewPostgresDevice(pool *pgxpool.Pool) domain.DeviceRepository {
return &postgresDeviceRepository{pool: pool}
}
func (p *postgresDeviceRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Device, error) {
rows, err := p.pool.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var devs []domain.Device
for rows.Next() {
var dev domain.Device
if err := rows.Scan(
&dev.ID,
&dev.APNSToken,
&dev.Sandbox,
&dev.LastPingedAt,
); err != nil {
return nil, err
}
devs = append(devs, dev)
}
return devs, nil
}
func (p *postgresDeviceRepository) GetByAPNSToken(ctx context.Context, token string) (domain.Device, error) {
query := `
SELECT id, apns_token, sandbox, last_pinged_at
FROM devices
2021-08-08 18:19:47 +00:00
WHERE apns_token = $1`
2021-07-26 16:34:26 +00:00
devs, err := p.fetch(ctx, query, token)
if err != nil {
return domain.Device{}, err
}
if len(devs) == 0 {
return domain.Device{}, domain.ErrNotFound
}
return devs[0], nil
}
func (p *postgresDeviceRepository) CreateOrUpdate(ctx context.Context, dev *domain.Device) error {
2021-08-08 18:19:47 +00:00
fmt.Println(dev)
2021-07-26 16:34:26 +00:00
query := `
INSERT INTO devices (apns_token, sandbox, last_pinged_at)
VALUES ($1, $2, $3)
2021-08-08 18:19:47 +00:00
ON CONFLICT(apns_token) DO
2021-07-26 16:34:26 +00:00
UPDATE SET last_pinged_at = $3
RETURNING id`
2021-08-08 18:19:47 +00:00
return p.pool.QueryRow(
2021-07-26 16:34:26 +00:00
ctx,
query,
dev.APNSToken,
dev.Sandbox,
dev.LastPingedAt,
2021-08-08 18:19:47 +00:00
).Scan(&dev.ID)
2021-07-26 16:34:26 +00:00
}
func (p *postgresDeviceRepository) Create(ctx context.Context, dev *domain.Device) error {
query := `
INSERT INTO devices
(apns_token, sandbox, last_pinged_at)
VALUES ($1, $2, $3)
RETURNING id`
2021-08-08 18:19:47 +00:00
return p.pool.QueryRow(
2021-07-26 16:34:26 +00:00
ctx,
query,
dev.APNSToken,
dev.Sandbox,
dev.LastPingedAt,
2021-08-08 18:19:47 +00:00
).Scan(&dev.ID)
2021-07-26 16:34:26 +00:00
}
func (p *postgresDeviceRepository) Update(ctx context.Context, dev *domain.Device) error {
query := `
UPDATE devices
SET last_pinged_at = $2
WHERE id = $1`
res, err := p.pool.Exec(ctx, query, dev.ID, dev.LastPingedAt)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}
func (p *postgresDeviceRepository) Delete(ctx context.Context, token string) error {
query := `DELETE FROM devices WHERE apns_token = $1`
res, err := p.pool.Exec(ctx, query, token)
if res.RowsAffected() != 1 {
return fmt.Errorf("weird behaviour, total rows affected: %d", res.RowsAffected())
}
return err
}