2025-12-03 19:29:37 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2026-02-20 15:54:17 +00:00
|
|
|
"strconv"
|
2025-12-03 19:29:37 +00:00
|
|
|
|
2026-03-06 18:13:16 +00:00
|
|
|
apperrors "veza-backend-api/internal/errors"
|
2026-04-14 10:22:14 +00:00
|
|
|
"veza-backend-api/internal/services"
|
2025-12-03 19:29:37 +00:00
|
|
|
|
|
|
|
|
"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)
|
2026-02-20 15:54:17 +00:00
|
|
|
Suggestions(query string, limit int) (*services.SearchResult, error)
|
2025-12-28 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
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 {
|
feat: backend, stream server & infra improvements
Backend (Go):
- Config: CORS, RabbitMQ, rate limit, main config updates
- Routes: core, distribution, tracks routing changes
- Middleware: rate limiter, endpoint limiter, response cache hardening
- Handlers: distribution, search handler fixes
- Workers: job worker improvements
- Upload validator and logging config additions
- New migrations: products, orders, performance indexes
- Seed tooling and data
Stream Server (Rust):
- Audio processing, config, routes, simple stream server updates
- Dockerfile improvements
Infrastructure:
- docker-compose.yml updates
- nginx-rtmp config changes
- Makefile improvements (config, dev, high, infra)
- Root package.json and lock file updates
- .env.example updates
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:36:06 +00:00
|
|
|
SearchHandlersInstance = &SearchHandlers{
|
2025-12-28 21:18:33 +00:00
|
|
|
searchService: searchService,
|
|
|
|
|
}
|
feat: backend, stream server & infra improvements
Backend (Go):
- Config: CORS, RabbitMQ, rate limit, main config updates
- Routes: core, distribution, tracks routing changes
- Middleware: rate limiter, endpoint limiter, response cache hardening
- Handlers: distribution, search handler fixes
- Workers: job worker improvements
- Upload validator and logging config additions
- New migrations: products, orders, performance indexes
- Seed tooling and data
Stream Server (Rust):
- Audio processing, config, routes, simple stream server updates
- Dockerfile improvements
Infrastructure:
- docker-compose.yml updates
- nginx-rtmp config changes
- Makefile improvements (config, dev, high, infra)
- Root package.json and lock file updates
- .env.example updates
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 10:36:06 +00:00
|
|
|
return SearchHandlersInstance
|
2025-12-28 21:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
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 == "" {
|
2026-03-06 18:13:16 +00:00
|
|
|
RespondWithAppError(c, apperrors.NewValidationError("Search query is required"))
|
2025-12-03 19:29:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
types := c.QueryArray("type")
|
|
|
|
|
|
|
|
|
|
results, err := sh.searchService.Search(query, types)
|
|
|
|
|
if err != nil {
|
2026-03-06 18:13:16 +00:00
|
|
|
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Search failed", err))
|
2025-12-03 19:29:37 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 16:21:59 +00:00
|
|
|
RespondSuccess(c, http.StatusOK, results)
|
|
|
|
|
}
|
2026-02-20 15:54:17 +00:00
|
|
|
|
|
|
|
|
// Suggestions returns autocomplete suggestions for the search input
|
|
|
|
|
func (sh *SearchHandlers) Suggestions(c *gin.Context) {
|
|
|
|
|
query := c.Query("q")
|
|
|
|
|
if query == "" {
|
2026-03-06 18:13:16 +00:00
|
|
|
RespondWithAppError(c, apperrors.NewValidationError("Query parameter 'q' is required"))
|
2026-02-20 15:54:17 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
limit := 5
|
|
|
|
|
if l := c.Query("limit"); l != "" {
|
|
|
|
|
if n, err := parseInt(l); err == nil && n > 0 && n <= 20 {
|
|
|
|
|
limit = n
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
results, err := sh.searchService.Suggestions(query, limit)
|
|
|
|
|
if err != nil {
|
2026-03-06 18:13:16 +00:00
|
|
|
RespondWithAppError(c, apperrors.NewInternalErrorWrap("Suggestions failed", err))
|
2026-02-20 15:54:17 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
RespondSuccess(c, http.StatusOK, results)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseInt(s string) (int, error) {
|
|
|
|
|
return strconv.Atoi(s)
|
|
|
|
|
}
|