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>
This commit is contained in:
@@ -28,54 +28,52 @@ func NewContactStore(db *pgxpool.Pool) *PGContactStore {
|
||||
|
||||
func (s *PGContactStore) CreateContact(ctx context.Context, con *contact.Contact) error {
|
||||
query := `
|
||||
INSERT INTO contacts (phone_number, user_id)
|
||||
VALUES ($1, $2)
|
||||
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).Scan(
|
||||
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 FROM contacts WHERE id = $1`
|
||||
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.Id, &con.PhoneNumber, &con.UserId, &con.Firstname, &con.Lastname, &con.Nickname,
|
||||
)
|
||||
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("getting contact by ID: %w", err)
|
||||
} else {
|
||||
return &con, nil
|
||||
}
|
||||
|
||||
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`
|
||||
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.Id, &con.PhoneNumber, &con.UserId, &con.Firstname, &con.Lastname, &con.Nickname,
|
||||
)
|
||||
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("getting contact by phone and User ID: %w", err)
|
||||
} else {
|
||||
return &con, nil
|
||||
}
|
||||
|
||||
return &con, nil
|
||||
}
|
||||
|
||||
func (s *PGContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact, error) {
|
||||
query := `SELECT id, phone_number, user_id FROM contacts`
|
||||
query := `SELECT id, phone_number, user_id, first_name, last_name, nickname FROM contacts`
|
||||
|
||||
rows, err := s.db.Query(ctx, query)
|
||||
if err != nil {
|
||||
@@ -87,7 +85,7 @@ func (s *PGContactStore) GetAllContacts(ctx context.Context) ([]*contact.Contact
|
||||
for rows.Next() {
|
||||
var con contact.Contact
|
||||
if err := rows.Scan(
|
||||
&con.Id, &con.PhoneNumber, &con.UserId,
|
||||
&con.Id, &con.PhoneNumber, &con.UserId, &con.Firstname, &con.Lastname, &con.Nickname,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scanning contact row: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user