Files
schedtxt_api/internal/store/contact_store.go
T
phoenixandphoenix a51979e724 tsk-58: Update names of Contact endpoint (#67)
Closes #58

Reviewed-on: phoenix/textsender-api#67
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2026-01-07 20:24:30 +00:00

183 lines
6.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)
UpdateNames(ctx context.Context, con *contact.Contact, firstname *string, lastname *string, nickname *string) (int64, 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
}
func (s *PGContactStore) UpdateNames(ctx context.Context, con *contact.Contact, firstname *string, lastname *string, nickname *string) (int64, error) {
if firstname == nil && lastname == nil && nickname == nil {
return 0, fmt.Errorf("Names are nil")
} else {
if firstname != nil && lastname != nil && nickname != nil {
query := "UPDATE contacts SET first_name = $1, last_name = $2, nickname = $3 WHERE id = $4"
if affected, err := s.db.Exec(ctx, query, firstname, lastname, nickname, con.Id); err != nil {
return 0, err
} else {
con.Firstname = firstname
con.Lastname = lastname
con.Nickname = nickname
return affected.RowsAffected(), nil
}
} else if firstname != nil && lastname != nil {
query := "UPDATE contacts SET first_name = $1, last_name = $2 WHERE id = $3"
if affected, err := s.db.Exec(ctx, query, firstname, lastname, con.Id); err != nil {
return 0, err
} else {
con.Firstname = firstname
con.Lastname = lastname
return affected.RowsAffected(), nil
}
} else if lastname != nil && nickname != nil {
query := "UPDATE contacts SET last_name = $1, nickname = $2 WHERE id = $3"
if affected, err := s.db.Exec(ctx, query, lastname, nickname, con.Id); err != nil {
return 0, err
} else {
con.Lastname = lastname
con.Nickname = nickname
return affected.RowsAffected(), nil
}
} else if nickname != nil && firstname != nil {
query := "UPDATE contacts SET nickname = $1, firstname = $2 WHERE id = $3"
if affected, err := s.db.Exec(ctx, query, nickname, firstname, con.Id); err != nil {
return 0, err
} else {
con.Firstname = firstname
con.Nickname = nickname
return affected.RowsAffected(), nil
}
} else if firstname != nil {
query := "UPDATE contacts SET firstname = $1 WHERE id = $2"
if affected, err := s.db.Exec(ctx, query, firstname, con.Id); err != nil {
return 0, err
} else {
con.Firstname = firstname
return affected.RowsAffected(), nil
}
} else if lastname != nil {
query := "UPDATE contacts SET lastname = $1 WHERE id = $2"
if affected, err := s.db.Exec(ctx, query, lastname, con.Id); err != nil {
return 0, err
} else {
con.Lastname = lastname
return affected.RowsAffected(), nil
}
} else {
query := "UPDATE contacts SET nickname = $1 WHERE id = $2"
if affected, err := s.db.Exec(ctx, query, nickname, con.Id); err != nil {
return 0, err
} else {
con.Nickname = nickname
return affected.RowsAffected(), nil
}
}
}
}