Files
whatsapp-bot/handlers/dashboard.go
SekiDesu0 9a341fed76 modified: .env
modified:   bot.db
	modified:   db/db.go
	modified:   go.mod
	modified:   go.sum
	modified:   handlers/dashboard.go
	modified:   main.go
	modified:   services/openrouter.go
	modified:   templates/dashboard.html
2026-03-01 05:39:31 -03:00

82 lines
1.8 KiB
Go

package handlers
import (
"net/http"
"whatsapp-bot/db"
"whatsapp-bot/services"
"github.com/gin-gonic/gin"
)
func ShowDashboard(c *gin.Context) {
rows, err := db.Conn.Query("SELECT customer_phone, appointment_date, status FROM appointments")
if err != nil {
c.String(http.StatusInternalServerError, "DB Error")
return
}
defer rows.Close()
type Appt struct{ CustomerPhone, Date, Status string }
var appts []Appt
for rows.Next() {
var a Appt
rows.Scan(&a.CustomerPhone, &a.Date, &a.Status)
appts = append(appts, a)
}
c.HTML(http.StatusOK, "dashboard.html", gin.H{
"Appointments": appts,
})
}
// Test OpenRouter via the Dashboard
func TestAIHandler(c *gin.Context) {
var body struct {
Prompt string `json:"prompt"`
}
if err := c.BindJSON(&body); err != nil {
c.JSON(400, gin.H{"response": "Invalid request, dummy."})
return
}
// Calling the service we wrote earlier
response, err := services.GetAIResponse(body.Prompt)
if err != nil {
c.JSON(500, gin.H{"response": "AI Error: " + err.Error()})
return
}
c.JSON(200, gin.H{"response": response})
}
// Add this to handlers/dashboard.go
func CreateAppointmentHandler(c *gin.Context) {
var body struct {
Phone string `json:"phone"`
Date string `json:"date"`
}
if err := c.BindJSON(&body); err != nil {
c.Status(400)
return
}
// Use the helper function instead of raw SQL here
if err := db.SaveAppointment(body.Phone, body.Date); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.Status(200)
}
// Manage/Delete Appointment
func DeleteAppointmentHandler(c *gin.Context) {
id := c.Param("id")
_, err := db.Conn.Exec("DELETE FROM appointments WHERE id = ?", id)
if err != nil {
c.Status(http.StatusInternalServerError)
return
}
c.Status(http.StatusOK)
}