You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

4 years ago
package api
import (
"net/http"
4 years ago
"strconv"
4 years ago
"github.com/gin-gonic/gin"
"github.com/imdotdev/im.dev/server/internal/search"
"github.com/imdotdev/im.dev/server/internal/user"
"github.com/imdotdev/im.dev/server/pkg/common"
"github.com/imdotdev/im.dev/server/pkg/e"
"github.com/imdotdev/im.dev/server/pkg/models"
)
func SearchPosts(c *gin.Context) {
4 years ago
filter := c.Query("filter")
4 years ago
query := c.Query("query")
if !models.ValidSearchFilter(filter) || query == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
4 years ago
page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
perPage, _ := strconv.ParseInt(c.Query("per_page"), 10, 64)
4 years ago
user := user.CurrentUser(c)
4 years ago
posts := search.Posts(user, filter, query, page, perPage)
4 years ago
c.JSON(http.StatusOK, common.RespSuccess(posts))
}
func SearchUsers(c *gin.Context) {
4 years ago
filter := c.Query("filter")
4 years ago
query := c.Query("query")
if !models.ValidSearchFilter(filter) || query == "" {
c.JSON(http.StatusBadRequest, common.RespError(e.ParamInvalid))
return
}
user := user.CurrentUser(c)
users := search.Users(user, filter, query)
c.JSON(http.StatusOK, common.RespSuccess(users))
}