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:
phoenix
2026-01-06 02:40:01 +00:00
committed by phoenix
parent cac1a9d97d
commit 4e250d095c
4 changed files with 42 additions and 42 deletions
+17 -18
View File
@@ -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 {