package handlers
import (
"fmt"
"net/http"
"strconv"
"time"
"go-crm/internal/db"
"go-crm/internal/templates"
"github.com/go-chi/chi/v5"
)
func (a *App) ListServices(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"))
clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64)
if limit == 0 {
limit = 20
}
services, err := db.ListServices(a.DB, accountID, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
clients, err := db.ListClients(a.DB, accountID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var clientOptions string
for _, c := range clients {
clientOptions += fmt.Sprintf(``, c.ClientID, htmlEscape(c.Name))
}
buf := templates.BufRender()
actions := ``
fmt.Fprint(buf, templates.PageHeader("Services", "Your service catalog and pricing", actions))
fmt.Fprintf(buf, `
New Service
`, clientOptions)
if len(services) == 0 {
fmt.Fprint(buf, templates.EmptyState(``, "No services yet. Add your first service to the catalog."))
} else {
fmt.Fprintf(buf, `
`)
fmt.Fprint(buf, templates.TableStart([]string{"Name", "Price", "Duration", "Actions"}))
for _, s := range services {
fmt.Fprintf(buf, `