125 lines
3.5 KiB
Go
125 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Lead represents the basic lead information with JSON tags that include the ID.
|
|
type Lead struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
Name string `json:"name" binding:"required"`
|
|
Email string `json:"email" binding:"required,email"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// Assessment stores the questionnaire responses and generated report.
|
|
type Assessment struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
LeadID uint `json:"lead_id"`
|
|
Answers string `json:"answers"` // JSON-encoded answers from the questionnaire.
|
|
Report string `json:"report"` // Generated assessment report.
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func main() {
|
|
// Connect to SQLite.
|
|
db, err := gorm.Open(sqlite.Open("leads.db"), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatal("failed to connect to database:", err)
|
|
}
|
|
|
|
// Auto-migrate our schema.
|
|
if err := db.AutoMigrate(&Lead{}, &Assessment{}); err != nil {
|
|
log.Fatal("failed to migrate database:", err)
|
|
}
|
|
|
|
router := gin.Default()
|
|
|
|
// Provide CORS handling.
|
|
router.Use(cors.New(cors.Config{
|
|
AllowOrigins: []string{"http://localhost:5173", "http://localhost:8081"}, // adjust as needed
|
|
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
|
|
AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
|
|
ExposeHeaders: []string{"Content-Length"},
|
|
AllowCredentials: true,
|
|
MaxAge: 12 * time.Hour,
|
|
}))
|
|
|
|
// Lead creation endpoint.
|
|
router.POST("/api/leads", func(c *gin.Context) {
|
|
var lead Lead
|
|
if err := c.ShouldBindJSON(&lead); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
result := db.Create(&lead)
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "could not save lead"})
|
|
return
|
|
}
|
|
// Return the lead with its generated ID.
|
|
c.JSON(http.StatusOK, lead)
|
|
})
|
|
|
|
// Assessment endpoint.
|
|
router.POST("/api/assessments", func(c *gin.Context) {
|
|
var input struct {
|
|
LeadID uint `json:"lead_id" binding:"required"`
|
|
Answers map[string]any `json:"answers" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Custom logic to generate the assessment report.
|
|
report := generateReport(input.Answers)
|
|
|
|
assessment := Assessment{
|
|
LeadID: input.LeadID,
|
|
Answers: stringifyAnswers(input.Answers),
|
|
Report: report,
|
|
}
|
|
result := db.Create(&assessment)
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "could not save assessment"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, assessment)
|
|
})
|
|
|
|
router.Run(":8081")
|
|
}
|
|
|
|
// generateReport analyzes answers and returns a custom report.
|
|
func generateReport(answers map[string]any) string {
|
|
// This is a placeholder implementation.
|
|
strengths := []string{"Good posture", "High energy"}
|
|
areasToImprove := []string{"Lower back flexibility", "Core strength"}
|
|
return "Strengths: " + join(strengths) + "\nAreas for improvement: " + join(areasToImprove)
|
|
}
|
|
|
|
func join(items []string) string {
|
|
out := ""
|
|
for i, item := range items {
|
|
if i > 0 {
|
|
out += ", "
|
|
}
|
|
out += item
|
|
}
|
|
return out
|
|
}
|
|
|
|
// stringifyAnswers converts the answers map to a JSON string.
|
|
func stringifyAnswers(answers map[string]any) string {
|
|
return fmt.Sprintf("%v", answers)
|
|
}
|