diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go index 4272c3a3..b858c53c 100644 --- a/backend/cmd/server/main.go +++ b/backend/cmd/server/main.go @@ -119,8 +119,7 @@ func setupRouter(cfg *config.Config) *gin.Engine { routes.SetupTranscriptRoutes(auth) - auth.GET("/coach/strengthen-argument/weak-statement", routes.GetWeakStatement) - auth.POST("/coach/strengthen-argument/evaluate", routes.EvaluateStrengthenedArgument) + routes.SetupCoachRoutes(auth) auth.GET("/rooms", routes.GetRoomsHandler) auth.POST("/rooms", routes.CreateRoomHandler) diff --git a/backend/routes/coach.go b/backend/routes/coach.go index 94fa43b8..f120c59b 100644 --- a/backend/routes/coach.go +++ b/backend/routes/coach.go @@ -3,10 +3,22 @@ package routes import ( "arguehub/services" "net/http" + "strings" "github.com/gin-gonic/gin" ) +// SetupCoachRoutes registers all coach-related routes on the given router group +func SetupCoachRoutes(router *gin.RouterGroup) { + coach := router.Group("/coach") + { + coach.GET("/strengthen-argument/weak-statement", GetWeakStatement) + coach.POST("/strengthen-argument/evaluate", EvaluateStrengthenedArgument) + coach.GET("/pros-cons/topic", GetProsConsTopic) + coach.POST("/pros-cons/submit", SubmitProsCons) + } +} + // GetWeakStatement generates a weak statement based on the user-provided topic and stance func GetWeakStatement(c *gin.Context) { topic := c.Query("topic") @@ -49,7 +61,7 @@ func EvaluateStrengthenedArgument(c *gin.Context) { pointsEarned := evaluation.Score * 10 // Update user's points - userID := c.GetString("user_id") + userID, _ := c.Get("userID") if err := services.UpdateUserPoints(userID, pointsEarned); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user points"}) return @@ -61,3 +73,85 @@ func EvaluateStrengthenedArgument(c *gin.Context) { "pointsEarned": pointsEarned, }) } + +// GetProsConsTopic generates a debate topic for the pros & cons challenge +func GetProsConsTopic(c *gin.Context) { + ratingValue, exists := c.Get("rating") + rating, isFloat := ratingValue.(float64) + + skillLevel := "beginner" + + if exists && isFloat { + if rating < 1400 { + skillLevel = "beginner" + } else if rating <= 1800 { + skillLevel = "intermediate" + } else { + skillLevel = "advanced" + } + } + + topic, err := services.GenerateDebateTopic(skillLevel) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate topic: " + err.Error()}) + return + } + + c.JSON(http.StatusOK, gin.H{"topic": topic}) +} + +type SubmitProsConsRequest struct { + Topic string `json:"topic" binding:"required"` + Pros []string `json:"pros" binding:"required"` + Cons []string `json:"cons" binding:"required"` +} + +// SubmitProsCons evaluates the pros and cons submitted by the user +func SubmitProsCons(c *gin.Context) { + var req SubmitProsConsRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request payload: " + err.Error()}) + return + } + + var validPros []string + for _, p := range req.Pros { + trimmed := strings.TrimSpace(p) + if trimmed != "" { + validPros = append(validPros, trimmed) + } + } + + var validCons []string + for _, con := range req.Cons { + trimmed := strings.TrimSpace(con) + if trimmed != "" { + validCons = append(validCons, trimmed) + } + } + + if len(validPros) == 0 || len(validCons) == 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "Please provide at least one pro and one con"}) + return + } + + if len(validPros) > 5 || len(validCons) > 5 { + c.JSON(http.StatusBadRequest, gin.H{"error": "Maximum of 5 pros and 5 cons allowed"}) + return + } + + evaluation, err := services.EvaluateProsCons(req.Topic, validPros, validCons) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to evaluate arguments: " + err.Error()}) + return + } + + // Update user points if authenticated + userID, _ := c.Get("userID") + if err := services.UpdateUserPoints(userID, evaluation.Score); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user points"}) + return + } + + c.JSON(http.StatusOK, evaluation) +} diff --git a/backend/services/coach.go b/backend/services/coach.go index 37d4bf3f..7756118d 100644 --- a/backend/services/coach.go +++ b/backend/services/coach.go @@ -114,7 +114,7 @@ Provide ONLY the JSON output without additional text or markdown formatting.`, } // UpdateUserPoints increments the user's total points in the database -func UpdateUserPoints(userID string, points int) error { +func UpdateUserPoints(userID interface{} , points int) error {// added inteface type so that it can handle both primitive.ObjectID and string _, err := db.MongoDatabase.Collection("users").UpdateOne( context.Background(), bson.M{"_id": userID},