package main

import (
	"fmt"

	"github.com/gofiber/fiber/v2"
)

// SearchAllByField searches all of the DB by specified field
func (s *Server) SearchAllHandler(c *fiber.Ctx) error {
	searchField := c.Query("field", "Name")
	searchTerm := c.Query("term")

	s.Log.Debug().Msgf("Search by term: %s in all items at field: %s", searchTerm, searchField)
	results, err := s.SearchAllByField(searchField, searchTerm)
	if err != nil {
		s.Log.Error().Msgf("SearchAllByField failed to return results: %s ", err)
		return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("Database failure! error: %s", err))
	}

	return c.Status(fiber.StatusOK).JSON(results)
}
