Skip to content
Merged
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
6 changes: 3 additions & 3 deletions handlers/auth_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,7 +82,6 @@ func LoginHandler(c *gin.Context) {
}

c.IndentedJSON(http.StatusOK, tokenString)
return
}

// ProtectedHandler
Expand Down
5 changes: 3 additions & 2 deletions handlers/comment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
72 changes: 72 additions & 0 deletions handlers/question_handler/search_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions handlers/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions routers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
Loading