Files
workspace/apps/go-crm/internal/handlers/scheduling.go
gabspereira 5b8107c169 Add GOTH stack CRM platform skeleton
- Go module with chi router, bcrypt
- SQLite schema for 9 tables
- Auth, Clients, Customers, Services, Scheduling, Payments, Q&A handlers
- HTMX template layouts
2026-04-21 15:56:40 +00:00

84 lines
4.2 KiB
Go

package handlers
import (
"database/sql"
"net/http"
"strconv"
"time"
"go-crm/internal/db"
"github.com/go-chi/chi/v5"
)
func ListSchedules(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)
customerID, _ := strconv.ParseInt(r.URL.Query().Get("customer_id"), 10, 64)
if limit == 0 {
limit = 20
}
schedules, err := db.ListSchedules(DB, clientID, customerID, limit, offset)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!DOCTYPE html><html><head><script src="https://unpkg.com/htmx.org@1.9.10"></script></head><body><h1>Scheduling</h1><button class="btn" onclick="document.getElementById('scheduleForm').style.display='block'">Add Schedule</button><div id="scheduleForm" style="display:none; margin-top:1rem;"><form hx-post="/scheduling" hx-target="#scheduleList"><input type="number" name="customer_id" placeholder="Customer ID" required><input type="number" name="client_id" placeholder="Client ID" required><input type="date" name="plan_date"><input type="date" name="confirmed_date"><input type="time" name="time"><select name="status"><option value="pending">Pending</option><option value="confirmed">Confirmed</option><option value="cancelled">Cancelled</option></select><textarea name="notes" placeholder="Notes"></textarea><button type="submit">Add</button></form></div><table><thead><tr><th>Customer</th><th>Date</th><th>Time</th><th>Status</th><th>Actions</th></tr></thead><tbody id="scheduleList">`))
for _, s := range schedules {
w.Write([]byte(`<tr><td>` + strconv.FormatInt(s.CustomerID, 10) + `</td><td>` + s.PlanDate + `</td><td>` + s.Time + `</td><td>` + s.Status + `</td><td><a href="/scheduling/` + strconv.FormatInt(s.ScheduleID, 10) + `">View</a></td></tr>`))
}
w.Write([]byte(`</tbody></table></body></html>`))
}
func CreateSchedule(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
schedule := db.Schedule{
CustomerID: customerID,
ClientID: clientID,
PlanDate: r.FormValue("plan_date"),
ConfirmedDate: r.FormValue("confirmed_date"),
Time: r.FormValue("time"),
Status: r.FormValue("status"),
Notes: r.FormValue("notes"),
CreatedAt: time.Now().Unix(),
}
if err := schedule.Create(DB); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("HX-Refresh", "true")
}
func ViewSchedule(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
var sch db.Schedule
err := DB.QueryRow("SELECT schedule_id, customer_id, client_id, plan_date, confirmed_date, time, status, notes, created_at FROM scheduling WHERE schedule_id = ?", id).Scan(&sch.ScheduleID, &sch.CustomerID, &sch.ClientID, &sch.PlanDate, &sch.ConfirmedDate, &sch.Time, &sch.Status, &sch.Notes, &sch.CreatedAt)
if err != nil {
http.Error(w, "Schedule not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<!DOCTYPE html><body><h1>Schedule</h1><p>Customer ID: ` + strconv.FormatInt(sch.CustomerID, 10) + `</p><p>Date: ` + sch.PlanDate + `</p><p>Time: ` + sch.Time + `</p><p>Status: ` + sch.Status + `</p><a href="/scheduling">Back</a></body></html>`))
}
func UpdateSchedule(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
r.ParseForm()
customerID, _ := strconv.ParseInt(r.FormValue("customer_id"), 10, 64)
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
DB.Exec("UPDATE scheduling SET customer_id=?, client_id=?, plan_date=?, confirmed_date=?, time=?, status=?, notes=? WHERE schedule_id=?", customerID, clientID, r.FormValue("plan_date"), r.FormValue("confirmed_date"), r.FormValue("time"), r.FormValue("status"), r.FormValue("notes"), id)
}
func DeleteSchedule(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
DB.Exec("DELETE FROM scheduling WHERE schedule_id = ?", id)
}