Missing some context storage and better passing of data to allow between the home page to the search page. Need a way to store the search results in state so they can be retrieved when the page is reloaded, etc.
45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
|
|
templates "github.com/haydenhargreaves/Potion/internal/templates/pages"
|
|
)
|
|
|
|
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()})
|
|
}
|
|
|
|
// Render content as the response
|
|
ctx.Status(200)
|
|
templates.ResultList(recipes).Render(ctx.Request.Context(), ctx.Writer)
|
|
}
|