package handlers
import (
"net/http"
"strconv"
"time"
"go-crm/internal/db"
)
func ListQuestions(w http.ResponseWriter, r *http.Request) {
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
}
questions, err := db.ListQuestions(DB, clientID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`
Questions
| Customer | Question | Status | Actions |
`))
for _, q := range questions {
w.Write([]byte(`| ` + strconv.FormatInt(q.CustomerID, 10) + ` | ` + q.Question + ` | ` + q.Status + ` | View |
`))
}
w.Write([]byte(`
`))
}
func CreateQuestion(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
question := db.Question{
ClientID: clientID,
CustomerID: customerID,
Question: r.FormValue("question"),
Timestamp: time.Now().Unix(),
Status: r.FormValue("status"),
CreatedAt: time.Now().Unix(),
}
if err := question.Create(DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("HX-Refresh", "true")
}
func ViewQuestion(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`Question
Back