Potion/internal/app/handlers/recipe_handler.go
Hayden Hargreaves 7ad710f880 (FEAT): Search is returning recipes, next just need a UI and wire job.
Furthermore, not sure how we are going to handle the searching. Maybe a
full-text search index? For now, it has been ignored, but the filters
seem to be working properly.
2025-07-06 22:40:15 -07:00

47 lines
1.2 KiB
Go

package handlers
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
)
const CREATE_SUCCESS_HTML = `
<p id="response" class="text-sm text-green-600 px-4 py-1 bg-green-100 rounded-full w-fit">
Success! Your new masterpiece was created!
</p>
`
const CREATE_ERROR_HTML = `
<p id="response" class="text-sm text-red-500 px-4 py-1 bg-red-100 rounded-full w-fit">
Uh oh! Something went wrong when creating your recipe. Please try again. %s
</p>
`
func CreateRecipe(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
recipe, err := deps.RecipeService.CreateRecipe(ctx)
if err != nil {
ctx.String(http.StatusOK, CREATE_ERROR_HTML, err.Error())
return
}
// Send HTMX redirection
url := fmt.Sprintf(domain.WEB_RECIPE, recipe.Id)
ctx.Header("HX-Redirect", url)
ctx.Status(http.StatusCreated)
}
func SearchRecipes(ctx *gin.Context) {
deps := ctx.MustGet("deps").(*domain.InjectedDependencies)
recipes, err := deps.RecipeService.SearchRecipes(ctx)
if err != nil {
ctx.JSON(http.StatusOK, gin.H{"error": err.Error()})
}
ctx.JSON(http.StatusOK, gin.H{"recipes": recipes})
}