2025-12-03 19:29:37 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"veza-backend-api/internal/services"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var SearchHandlersInstance *SearchHandlers
|
|
|
|
|
|
2025-12-28 21:18:33 +00:00
|
|
|
// SearchServiceInterface defines the interface for search operations
|
|
|
|
|
// This allows for easier testing with mocks
|
|
|
|
|
type SearchServiceInterface interface {
|
|
|
|
|
Search(query string, types []string) (*services.SearchResult, error)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 19:29:37 +00:00
|
|
|
type SearchHandlers struct {
|
2025-12-28 21:18:33 +00:00
|
|
|
searchService SearchServiceInterface
|
2025-12-03 19:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSearchHandlers(searchService *services.SearchService) {
|
|
|
|
|
SearchHandlersInstance = &SearchHandlers{
|
|
|
|
|
searchService: searchService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 21:18:33 +00:00
|
|
|
// NewSearchHandlersWithInterface creates new search handlers with an interface (for testing)
|
|
|
|
|
func NewSearchHandlersWithInterface(searchService SearchServiceInterface) *SearchHandlers {
|
|
|
|
|
return &SearchHandlers{
|
|
|
|
|
searchService: searchService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 19:29:37 +00:00
|
|
|
// Search performs a full-text search across tracks, users, and playlists
|
|
|
|
|
func (sh *SearchHandlers) Search(c *gin.Context) {
|
|
|
|
|
query := c.Query("q")
|
|
|
|
|
if query == "" {
|
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Search query is required"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
types := c.QueryArray("type")
|
|
|
|
|
|
|
|
|
|
results, err := sh.searchService.Search(query, types)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 16:21:59 +00:00
|
|
|
RespondSuccess(c, http.StatusOK, results)
|
|
|
|
|
}
|