Files
schedtxt_api/internal/store/contact_store.go
T
phoenixandphoenix 4e250d095c tsk-60: Save extra fields to DB for Contact (#66)
Closes #60

Reviewed-on: phoenix/textsender-api#66
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2026-01-06 02:40:01 +00:00

113 lines
3.4 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, first_name, last_name, nickname)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, phone_number, user_id
`
return s.db.QueryRow(ctx, query, con.PhoneNumber, con.UserId, con.Firstname, con.Lastname, con.Nickname).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, first_name, last_name, nickname FROM contacts WHERE id = $1`
var con contact.Contact
err := s.db.QueryRow(ctx, query, id).Scan(
&con.Id, &con.PhoneNumber, &con.UserId, &con.Firstname, &con.Lastname, &con.Nickname,
)
if err == pgx.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("getting contact by ID: %w", err)
} else {
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, first_name, last_name, nickname 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, &con.Firstname, &con.Lastname, &con.Nickname,
)
if err == pgx.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("getting contact by phone and User ID: %w", err)
} else {
return &con, nil
}
}
func (s *PGContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact, error) {
query := `SELECT id, phone_number, user_id, first_name, last_name, nickname 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, &con.Firstname, &con.Lastname, &con.Nickname,
); 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
}