Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
96 changes: 95 additions & 1 deletion backend/routes/coach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion backend/services/coach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down