Closes #40 Reviewed-on: phoenix/textsender-api#46 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type ContactStore interface {
|
|
CreateContact(ctx context.Context, con *contact.Contact) error
|
|
GetContactByID(ctx context.Context, id uuid.UUID) (*contact.Contact, error)
|
|
GetContactByPhone(ctx context.Context, phone string, userId uuid.UUID) (*contact.Contact, error)
|
|
GetAllContacts(ctx context.Context) ([]*contact.Contact, error)
|
|
ContactExists(ctx context.Context, phone string, userId uuid.UUID) (bool, error)
|
|
}
|
|
|
|
type PGContactStore struct {
|
|
db *pgxpool.Pool
|
|
}
|
|
|
|
func NewContactStore(db *pgxpool.Pool) *PGContactStore {
|
|
return &PGContactStore{db: db}
|
|
}
|
|
|
|
func (s *PGContactStore) CreateContact(ctx context.Context, con *contact.Contact) error {
|
|
query := `
|
|
INSERT INTO contacts (phone_number, user_id)
|
|
VALUES ($1, $2)
|
|
RETURNING id, phone_number, user_id
|
|
`
|
|
|
|
return s.db.QueryRow(ctx, query, con.PhoneNumber, con.UserId).Scan(
|
|
&con.Id, &con.PhoneNumber, &con.UserId,
|
|
)
|
|
}
|
|
|
|
func (s *PGContactStore) GetContactByID(ctx context.Context, id uuid.UUID) (*contact.Contact, error) {
|
|
query := `SELECT id, phone_number, user_id FROM contacts WHERE id = $1`
|
|
|
|
var con contact.Contact
|
|
err := s.db.QueryRow(ctx, query, id).Scan(
|
|
&con.Id, &con.PhoneNumber, &con.UserId,
|
|
)
|
|
|
|
if err == pgx.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting contact by ID: %w", err)
|
|
}
|
|
|
|
return &con, nil
|
|
}
|
|
|
|
func (s *PGContactStore) GetContactByPhone(ctx context.Context, phone string, userId uuid.UUID) (*contact.Contact, error) {
|
|
query := `SELECT id, phone_number, user_id FROM contacts WHERE phone_number = $1 AND user_id = $2`
|
|
|
|
var con contact.Contact
|
|
err := s.db.QueryRow(ctx, query, phone, userId).Scan(
|
|
&con.Id, &con.PhoneNumber, &con.UserId,
|
|
)
|
|
|
|
if err == pgx.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("getting contact by phone and User ID: %w", err)
|
|
}
|
|
|
|
return &con, nil
|
|
}
|
|
|
|
func (s *PGContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact, error) {
|
|
query := `SELECT id, phone_number, user_id FROM contacts`
|
|
|
|
rows, err := s.db.Query(ctx, query)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("querying all contacts: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var cons []*contact.Contact
|
|
for rows.Next() {
|
|
var con contact.Contact
|
|
if err := rows.Scan(
|
|
&con.Id, &con.PhoneNumber, &con.UserId,
|
|
); err != nil {
|
|
return nil, fmt.Errorf("scanning contact row: %w", err)
|
|
}
|
|
cons = append(cons, &con)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterating contact rows: %w", err)
|
|
}
|
|
|
|
return cons, nil
|
|
}
|
|
|
|
func (s *PGContactStore) ContactExists(ctx context.Context, phone string, userId uuid.UUID) (bool, error) {
|
|
query := `SELECT EXISTS(SELECT 1 FROM contacts WHERE phone_number = $1 AND user_id = $2)`
|
|
|
|
var exists bool
|
|
err := s.db.QueryRow(ctx, query, phone, userId).Scan(&exists)
|
|
if err != nil {
|
|
return false, fmt.Errorf("checking if contact exists: %w", err)
|
|
}
|
|
|
|
return exists, nil
|
|
}
|