package handlers import ( "net/http" "strconv" "time" "go-crm/internal/db" "github.com/go-chi/chi/v5" ) func (a *App) ListClients(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) if limit == 0 { limit = 20 } clients, err := db.ListClients(a.DB, accountID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(`
| Name | Phone | Actions | |
|---|---|---|---|
| ` + c.Name + ` | ` + c.Phone + ` | ` + whatsappCell + ` | View |
WhatsApp: ` + client.WhatsAppNumber + ` (` + status + `)
` } else { whatsappSection = `WhatsApp: Not configured Connect
` } w.Write([]byte(`Client ID: ` + strconv.FormatInt(client.ClientID, 10) + `
Phone: ` + client.Phone + `
Email: ` + client.Email + `
Address: ` + client.Address + `
Notes: ` + client.Notes + `
` + whatsappSection + ` Back `)) } func (a *App) UpdateClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() client := db.Client{ ClientID: id, AccountID: accountID, Name: r.FormValue("name"), Phone: r.FormValue("phone"), Email: r.FormValue("email"), Address: r.FormValue("address"), Notes: r.FormValue("notes"), } if err := client.Update(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func (a *App) DeleteClient(w http.ResponseWriter, r *http.Request) { accountID, ok := a.requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) client := &db.Client{ClientID: id, AccountID: accountID} if err := client.Delete(a.DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte("OK")) } // --- package-level shims kept for existing tests --- func ListClients(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ListClients(w, r) } func CreateClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).CreateClient(w, r) } func ViewClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).ViewClient(w, r) } func UpdateClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).UpdateClient(w, r) } func DeleteClient(w http.ResponseWriter, r *http.Request) { (&App{DB: DB, WAConnector: WAConnector}).DeleteClient(w, r) }