package handlers import ( "net/http" "strconv" "time" "go-crm/internal/db" "github.com/go-chi/chi/v5" ) func ListCustomers(w http.ResponseWriter, r *http.Request) { accountID, ok := requireAuth(w, r) if !ok { return } limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64) if limit == 0 { limit = 20 } customers, err := db.ListCustomers(DB, accountID, clientID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } clients, err := db.ListClients(DB, accountID, limit, offset) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var clientOptions string for _, c := range clients { clientOptions += `` } w.Header().Set("Content-Type", "text/html") w.Write([]byte(`

Customers

`)) for _, c := range customers { w.Write([]byte(``)) } w.Write([]byte(`
NamePhoneBirth DateInstagramActions
` + c.Name + `` + c.Phone + `` + c.BirthDate + `` + c.Instagram + `View
`)) } func CreateCustomer(w http.ResponseWriter, r *http.Request) { accountID, ok := requireAuth(w, r) if !ok { return } r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) var checkID int64 err := DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID) if err != nil { http.Error(w, "Invalid client", http.StatusBadRequest) return } customer := db.Customer{ ClientID: clientID, Name: r.FormValue("name"), Phone: r.FormValue("phone"), BirthDate: r.FormValue("birth_date"), Instagram: r.FormValue("instagram"), CreatedAt: time.Now().Unix(), } if err := customer.Create(DB); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func ViewCustomer(w http.ResponseWriter, r *http.Request) { _, ok := requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) var c db.Customer err := DB.QueryRow("SELECT customer_id, client_id, name, phone, birth_date, instagram, created_at FROM customers WHERE customer_id = ?", id).Scan(&c.CustomerID, &c.ClientID, &c.Name, &c.Phone, &c.BirthDate, &c.Instagram, &c.CreatedAt) if err != nil { http.Error(w, "Customer not found", http.StatusNotFound) return } w.Header().Set("Content-Type", "text/html") w.Write([]byte(`

` + c.Name + `

Phone: ` + c.Phone + `

Birth Date: ` + c.BirthDate + `

Instagram: ` + c.Instagram + `

Back`)) } func UpdateCustomer(w http.ResponseWriter, r *http.Request) { _, ok := requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) r.ParseForm() clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64) _, err := DB.Exec("UPDATE customers SET client_id=?, name=?, phone=?, birth_date=?, instagram=? WHERE customer_id=?", clientID, r.FormValue("name"), r.FormValue("phone"), r.FormValue("birth_date"), r.FormValue("instagram"), id) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } w.Header().Set("HX-Refresh", "true") } func DeleteCustomer(w http.ResponseWriter, r *http.Request) { _, ok := requireAuth(w, r) if !ok { return } id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64) DB.Exec("DELETE FROM customers WHERE customer_id = ?", id) }