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:
+17
-18
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
|
||||
@@ -11,16 +12,6 @@ import (
|
||||
"git.kundeng.us/phoenix/textsender-api/internal/store"
|
||||
)
|
||||
|
||||
type RequestAddContact struct {
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
UserId uuid.UUID `json:"user_id"`
|
||||
}
|
||||
|
||||
type AddContactResponse struct {
|
||||
Message string `json:"message"`
|
||||
Data []contact.Contact `json:"data"`
|
||||
}
|
||||
|
||||
type ContactHandler struct {
|
||||
App *app.App
|
||||
ContactStore store.ContactStore
|
||||
@@ -30,6 +21,19 @@ func NewContactHandler(apiApp *app.App, str store.ContactStore) *ContactHandler
|
||||
return &ContactHandler{App: apiApp, ContactStore: str}
|
||||
}
|
||||
|
||||
type RequestAddContact struct {
|
||||
PhoneNumber string `json:"phone_number"`
|
||||
UserId uuid.UUID `json:"user_id"`
|
||||
Firstname *string `json:"first_name,omitempty"`
|
||||
Lastname *string `json:"last_name,omitempty"`
|
||||
Nickname *string `json:"nickname,omitempty"`
|
||||
}
|
||||
|
||||
type AddContactResponse struct {
|
||||
Message string `json:"message"`
|
||||
Data []contact.Contact `json:"data"`
|
||||
}
|
||||
|
||||
// AddContact godoc
|
||||
// @Summary Add contact
|
||||
// @Description Add a contact record to have a recipient to send a text to (requires JWT)
|
||||
@@ -43,18 +47,13 @@ func NewContactHandler(apiApp *app.App, str store.ContactStore) *ContactHandler
|
||||
// @Failure 500 {object} AddContactResponse
|
||||
// @Router /contact/new [post]
|
||||
func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req RequestAddContact
|
||||
if err := ExtractFromRequest(r, &req); err != nil {
|
||||
http.Error(w, "Invalid JSON: "+err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
newContact := contact.Contact{PhoneNumber: req.PhoneNumber, UserId: &req.UserId}
|
||||
newContact := contact.Contact{PhoneNumber: req.PhoneNumber, UserId: &req.UserId, Firstname: req.Firstname, Lastname: req.Lastname, Nickname: req.Nickname}
|
||||
|
||||
var statusCode int
|
||||
var resp AddContactResponse
|
||||
@@ -62,7 +61,7 @@ func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
if exists, err := c.ContactStore.ContactExists(ctx, newContact.PhoneNumber, *newContact.UserId); err != nil {
|
||||
fmt.Printf("Error: %v", err)
|
||||
log.Printf("Error: %v", err)
|
||||
statusCode = http.StatusInternalServerError
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
@@ -71,7 +70,7 @@ func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
resp.Message = "Cannot create contact"
|
||||
} else {
|
||||
if err := c.ContactStore.CreateContact(ctx, &newContact); err != nil {
|
||||
fmt.Printf("Error: %v", err)
|
||||
log.Printf("Error: %v", err)
|
||||
statusCode = http.StatusInternalServerError
|
||||
resp.Message = err.Error()
|
||||
} else {
|
||||
|
||||
@@ -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