diff --git a/handlers/auth_handler.go b/handlers/auth_handler.go index 1256b00..274ed5f 100644 --- a/handlers/auth_handler.go +++ b/handlers/auth_handler.go @@ -5,10 +5,11 @@ import ( "Learning/error" "Learning/models" "Learning/token" - "github.com/gin-gonic/gin" - "golang.org/x/crypto/bcrypt" "net/http" "strings" + + "github.com/gin-gonic/gin" + "golang.org/x/crypto/bcrypt" ) // RegisterHandler @@ -81,7 +82,6 @@ func LoginHandler(c *gin.Context) { } c.IndentedJSON(http.StatusOK, tokenString) - return } // ProtectedHandler diff --git a/handlers/comment_handler.go b/handlers/comment_handler.go index 3ccd0e6..f64991e 100644 --- a/handlers/comment_handler.go +++ b/handlers/comment_handler.go @@ -4,9 +4,10 @@ import ( "Learning/database" "Learning/error" "Learning/models" - "github.com/gin-gonic/gin" "net/http" "regexp" + + "github.com/gin-gonic/gin" ) // DeleteComment @@ -75,7 +76,7 @@ func AddComment(c *gin.Context) { c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "error checking description"}) return } - if matchString == true { + if matchString { c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Description containing bad characters"}) return } diff --git a/handlers/question_handler/search_handler.go b/handlers/question_handler/search_handler.go new file mode 100644 index 0000000..ae3f740 --- /dev/null +++ b/handlers/question_handler/search_handler.go @@ -0,0 +1,72 @@ +package question_handler + +import ( + "Learning/database" + "Learning/models" + "net/http" + "strconv" + "strings" + + "github.com/gin-gonic/gin" +) + +// SearchQuestions +// @Tags question +// @Accept json +// @Produce json +// @Param Authorization header string true "Bearer Token" +// @Param query query string true "Search query" +// @Param page query int false "Page number" default(1) +// @Param limit query int false "Items per page" default(10) +// @Success 200 {array} models.Question +// @Router /api/questions/search [get] +func SearchQuestions(c *gin.Context) { + query := c.Query("query") + if query == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "Search query is required"}) + return + } + + pageStr := c.DefaultQuery("page", "1") + limitStr := c.DefaultQuery("limit", "10") + + page, err := strconv.Atoi(pageStr) + if err != nil { + page = 1 + } + + limit, err := strconv.Atoi(limitStr) + if err != nil { + limit = 10 + } + + words := strings.Fields(query) + var searchTerms []string + for _, word := range words { + if len(word) > 2 { // Ignore very short words + searchTerms = append(searchTerms, "%"+word+"%") + } + } + + var questions []models.Question + queryBuilder := database.DB.Model(&models.Question{}) + + for _, term := range searchTerms { + queryBuilder = queryBuilder.Where("description LIKE ?", term) + } + + offset := (page - 1) * limit + err = queryBuilder. + Preload("Tags"). + Order("votes DESC"). + Offset(offset). + Limit(limit). + Find(&questions).Error + + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to search questions"}) + return + } + + c.JSON(http.StatusOK, questions) +} diff --git a/handlers/user_handler.go b/handlers/user_handler.go index d72caa9..48a623f 100644 --- a/handlers/user_handler.go +++ b/handlers/user_handler.go @@ -19,13 +19,13 @@ import ( func AddUser(c *gin.Context) { var user models.User if err := c.ShouldBindJSON(&user); err != nil { - c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "Invalid JSON format", "error": err.Error()}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": error.InvalidJson}) return } result := database.DB.Create(&user) if result.Error != nil { - c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Error creating user", "error": result.Error.Error()}) + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Error creating user"}) return } @@ -42,13 +42,13 @@ func AddUser(c *gin.Context) { func DeleteUser(c *gin.Context) { var user models.User if err := c.ShouldBindJSON(&user); err != nil { - c.IndentedJSON(http.StatusBadRequest, gin.H{"message": error.InvalidJson, "error": err.Error()}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"message": error.InvalidJson}) return } result := database.DB.Delete(&user) if result.Error != nil { - c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Error deleting user", "error": result.Error.Error()}) + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Error deleting user"}) return } c.IndentedJSON(http.StatusAccepted, user) diff --git a/routers/routes.go b/routers/routes.go index 7e31c78..67738e2 100644 --- a/routers/routes.go +++ b/routers/routes.go @@ -5,6 +5,7 @@ import ( "Learning/handlers/answer_handler" "Learning/handlers/question_handler" "Learning/middleware" + "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" @@ -27,15 +28,16 @@ func SetupRouter() *gin.Engine { protected.GET("/questions/all", question_handler.FetchQuestions) protected.POST("/questions/add", question_handler.PostQuestion) protected.GET("/questions/my", question_handler.FetchMyQuestions) - protected.GET("/questions/voteUp/:id}", question_handler.VoteUpQuestion) - protected.GET("/questions/voteDown/:id}", question_handler.VoteDownQuestion) + protected.GET("/questions/voteUp/:id", question_handler.VoteUpQuestion) + protected.GET("/questions/voteDown/:id", question_handler.VoteDownQuestion) + protected.GET("/questions/search", question_handler.SearchQuestions) // Answer routes protected.POST("/answer_handler/add", answer_handler.AddAnswer) protected.GET("/answer_handler/correctAnswer/:id", answer_handler.CorrectAnswer) - protected.GET("/answer_handler/voteUp/:id}", answer_handler.VoteUpAnswer) - protected.GET("/answer_handler/voteDown/:id}", answer_handler.VoteDownAnswer) - protected.GET("/answer_handler/delete", answer_handler.DeleteAnswer) + protected.GET("/answer_handler/voteUp/:id", answer_handler.VoteUpAnswer) + protected.GET("/answer_handler/voteDown/:id", answer_handler.VoteDownAnswer) + protected.DELETE("/answer_handler/delete", answer_handler.DeleteAnswer) //comment routes protected.POST("/comment/add", handlers.AddComment)