Skip to content

Commit bdb6d34

Browse files
committed
demo: use path params instead of query params
1 parent 8287b48 commit bdb6d34

20 files changed

+46
-29
lines changed

examples/full-app-gourmet/controller/recipe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (rs recipeRessource) newRecipe(c *fuego.ContextWithBody[store.CreateRecipeP
6060
}
6161

6262
func (rs recipeRessource) getRecipeWithIngredients(c fuego.ContextNoBody) ([]store.GetIngredientsOfRecipeRow, error) {
63-
recipe, err := rs.IngredientRepository.GetIngredientsOfRecipe(c.Context(), c.QueryParam("id"))
63+
recipe, err := rs.IngredientRepository.GetIngredientsOfRecipe(c.Context(), c.PathParam("id"))
6464
if err != nil {
6565
return nil, err
6666
}

examples/full-app-gourmet/templa/admin/ingredient.list.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ templ IngredientList(ingredients []store.Ingredient, parameters components.Searc
3838
<tr>
3939
<td class="border-b border-zinc-100 dark:border-zinc-700 p-4 pr-8 text-zinc-700 dark:text-zinc-300">
4040
<a
41-
href={ templ.URL("/admin/ingredients/one?id=" + ingredient.ID) }
41+
href={ templ.URL("/admin/ingredients/" + ingredient.ID) }
4242
hx-boost="true"
4343
hx-target="#page"
4444
hx-select="#page"

examples/full-app-gourmet/templa/admin/ingredient.page.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ templ IngredientPage(ingredient store.Ingredient) {
99
<h1>Edit Ingredient</h1>
1010
@IngredientForm(IngredientFormProps{
1111
Ingredient: ingredient,
12-
FormAction:"/admin/ingredients/one?id=" + ingredient.ID,
12+
FormAction:"/admin/ingredients/" + ingredient.ID,
1313
IsCreating: false,
1414
HXTrigger: "change delay:500ms",
1515
})

examples/full-app-gourmet/templa/admin/recipe.list.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ templ RecipeList(recipes []store.Recipe, parameters components.SearchParams) {
4040
<tr>
4141
<td class="border-b border-zinc-100 dark:border-zinc-700 p-4 pr-8 text-zinc-700 dark:text-zinc-300">
4242
<a
43-
href={ templ.URL("/admin/recipes/one?id=" + recipe.ID) }
43+
href={ templ.URL("/admin/recipes/" + recipe.ID) }
4444
hx-boost="true"
4545
hx-target="#page"
4646
hx-select="#page"

examples/full-app-gourmet/templa/admin/recipe.page.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ templ RecipePage(props RecipePageProps) {
1717
Recipe: props.Recipe,
1818
Dosings: props.Dosings,
1919
AllIngredients: props.AllIngredients,
20-
FormAction:"/admin/recipes/one?id=" + props.Recipe.ID,
20+
FormAction:"/admin/recipes/" + props.Recipe.ID,
2121
IsCreating: false,
2222
HXTrigger: "change delay:500ms",
2323
})

examples/full-app-gourmet/templa/components/slider.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ templ SliderRecipes(recipes []store.Recipe) {
1414
@Card(CardProps{
1515
Title: recipe.Name,
1616
WhenToEat: recipe.WhenToEat,
17-
Link: "/recipes/one?id=" + recipe.ID,
17+
Link: "/recipes/" + recipe.ID,
1818
ImageURL: recipe.ImageUrl,
1919
Body: recipe.Description,
2020
})

examples/full-app-gourmet/templa/ingredient.list.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ templ IngredientList(props IngredientListProps) {
1515
for _, ingredient := range props.Ingredients {
1616
@components.Card(components.CardProps{
1717
Title: ingredient.Name,
18-
Link: "/ingredients/one?id=" + ingredient.ID,
18+
Link: "/ingredients/" + ingredient.ID,
1919
ImageURL: "",
2020
Body: ingredient.Description,
2121
})

examples/full-app-gourmet/templa/recipe.list.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ templ RecipeList(props RecipeListProps) {
1717
@components.Card(components.CardProps{
1818
Title: recipe.Name,
1919
WhenToEat: recipe.WhenToEat,
20-
Link: "/recipes/one?id=" + recipe.ID,
20+
Link: "/recipes/" + recipe.ID,
2121
ImageURL: "",
2222
Body: recipe.Description,
2323
})

examples/full-app-gourmet/templa/recipe.page.templ

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ templ RecipePage(props RecipePageProps, generalProps GeneralProps) {
6161
by { props.Recipe.CreatedBy }
6262
if generalProps.IsAdmin {
6363
-
64-
<a href={ templ.URL("/admin/recipes/one?id=" + props.Recipe.ID) }>
64+
<a href={ templ.URL("/admin/recipes/" + props.Recipe.ID) }>
6565
edit
6666
</a>
6767
}
@@ -171,7 +171,7 @@ templ RelatedRecipes(recipes []store.Recipe) {
171171
WhenToEat: recipe.WhenToEat,
172172
ImageURL: recipe.ImageUrl,
173173
Body: recipe.Description,
174-
Link: "/recipes/one?id="+recipe.ID,
174+
Link: "/recipes/"+recipe.ID,
175175
})
176176
}
177177
</div>

examples/full-app-gourmet/templa/search.templ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ templ Search(props SearchProps) {
5555
@components.Card(components.CardProps{
5656
Title: recipe.Name,
5757
WhenToEat: recipe.WhenToEat,
58-
Link: "/recipes/one?id=" + recipe.ID,
58+
Link: "/recipes/" + recipe.ID,
5959
ImageURL: recipe.ImageUrl,
6060
Body: recipe.Description,
6161
})

examples/full-app-gourmet/templates/pages/admin/single-recipe.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h1>Admin - Recipe</h1>
1212
>
1313
<h2 class="mb-4 text-3xl font-bold">{{ .Recipe.Name }}</h2>
1414
<div class="flex justify-between gap-2 md:gap-4">
15-
<a class="btn btn-secondary" href="/recipes/one?id={{ .Recipe.ID }}">
15+
<a class="btn btn-secondary" href="/recipes/{{ .Recipe.ID }}">
1616
See
1717
</a>
1818
<button class="btn btn-primary" form="myform">Save</button>

examples/full-app-gourmet/templates/pages/recipe.page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1 class="text-4xl font-bold">{{ .Recipe.Name }}</h1>
1111
{{ if .Admin }}
1212
<div class="flex items-center">
1313
<a
14-
href="/admin/recipes/one?id={{ .Recipe.ID }}"
14+
href="/admin/recipes/{{ .Recipe.ID }}"
1515
class="btn btn-secondary"
1616
>
1717
Edit

examples/full-app-gourmet/templates/partials/ingredients/ingredient-card.partial.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
>
55
<a
66
class="flex h-full w-full flex-col"
7-
href="/ingredients/one?id={{ .ID }}"
7+
href="/ingredients/{{ .ID }}"
88
hx-boost="true"
99
hx-target="#page"
1010
hx-select="#page"

examples/full-app-gourmet/templates/partials/recipes/recipe-card.partial.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
>
55
<a
66
class="flex h-full w-full flex-col p-4"
7-
href="/recipes/one?id={{ .ID }}"
7+
href="/recipes/{{ .ID }}"
88
hx-boost="true"
99
hx-target="#page"
1010
hx-select="#page"

examples/full-app-gourmet/templates/partials/recipes/recipe-line.partial.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="flex justify-between">
66
<div class="flex">
77
<a
8-
href="/admin/recipes/one?id={{ .ID }}"
8+
href="/admin/recipes/{{ .ID }}"
99
hx-boost="true"
1010
hx-target="#page"
1111
hx-select="#page"

examples/full-app-gourmet/views/admin.ingredient.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import (
1212
)
1313

1414
func (rs Ressource) adminOneIngredient(c *fuego.ContextWithBody[store.UpdateIngredientParams]) (fuego.CtxRenderer, error) {
15-
id := c.QueryParam("id") // TODO use PathParam
15+
id := c.PathParam("id")
1616

1717
if c.Request().Method == "PUT" {
1818
updateIngredientArgs, err := c.Body()
1919
if err != nil {
2020
return nil, err
2121
}
2222

23-
updateIngredientArgs.ID = c.QueryParam("id") // TODO use PathParam
23+
updateIngredientArgs.ID = c.PathParam("id")
2424

2525
_, err = rs.IngredientsQueries.UpdateIngredient(c.Context(), updateIngredientArgs)
2626
if err != nil {

examples/full-app-gourmet/views/admin.recipe.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func (rs Ressource) deleteRecipe(c fuego.ContextNoBody) (any, error) {
15-
id := c.QueryParam("id") // TODO use PathParam
15+
id := c.PathParam("id")
1616
err := rs.RecipesQueries.DeleteRecipe(c.Context(), id)
1717
if err != nil {
1818
return nil, err
@@ -38,7 +38,7 @@ func (rs Ressource) adminRecipes(c fuego.ContextNoBody) (fuego.Templ, error) {
3838
}
3939

4040
func (rs Ressource) adminOneRecipe(c *fuego.ContextWithBody[store.UpdateRecipeParams]) (fuego.Templ, error) {
41-
id := c.QueryParam("id") // TODO use PathParam
41+
id := c.Request().PathValue("id")
4242

4343
if c.Request().Method == "PUT" {
4444
updateRecipeBody, err := c.Body()
@@ -88,14 +88,14 @@ func (rs Ressource) editRecipe(c *fuego.ContextWithBody[store.UpdateRecipeParams
8888
return "", err
8989
}
9090

91-
updateRecipeArgs.ID = c.QueryParam("id") // TODO use PathParam
91+
updateRecipeArgs.ID = c.PathParam("id")
9292

9393
recipe, err := rs.RecipesQueries.UpdateRecipe(c.Context(), updateRecipeArgs)
9494
if err != nil {
9595
return "", err
9696
}
9797

98-
return c.Redirect(301, "/admin/recipes/one?id="+recipe.ID)
98+
return c.Redirect(301, "/admin/recipes/"+recipe.ID)
9999
}
100100

101101
func (rs Ressource) adminAddRecipes(c *fuego.ContextWithBody[store.CreateRecipeParams]) (any, error) {
@@ -112,6 +112,23 @@ func (rs Ressource) adminAddRecipes(c *fuego.ContextWithBody[store.CreateRecipeP
112112
return c.Redirect(301, "/admin/recipes")
113113
}
114114

115+
func (rs Ressource) adminCreateRecipePage(c *fuego.ContextNoBody) (fuego.Templ, error) {
116+
allIngredients, err := rs.IngredientsQueries.GetIngredients(c.Context())
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
slices.SortFunc(allIngredients, func(a, b store.Ingredient) int {
122+
return strings.Compare(a.Name, b.Name)
123+
})
124+
125+
return admin.RecipePage(admin.RecipePageProps{
126+
Recipe: store.Recipe{},
127+
128+
AllIngredients: allIngredients,
129+
}), nil
130+
}
131+
115132
func (rs Ressource) adminAddDosing(c *fuego.ContextWithBody[store.CreateDosingParams]) (any, error) {
116133
body, err := c.Body()
117134
if err != nil {
@@ -123,5 +140,5 @@ func (rs Ressource) adminAddDosing(c *fuego.ContextWithBody[store.CreateDosingPa
123140
return "", err
124141
}
125142

126-
return c.Redirect(301, "/admin/recipes/one?id="+body.RecipeID)
143+
return c.Redirect(301, "/admin/recipes/"+body.RecipeID)
127144
}

examples/full-app-gourmet/views/recipe.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (rs Ressource) relatedRecipes(c fuego.ContextNoBody) (fuego.Templ, error) {
120120
}
121121

122122
func (rs Ressource) showSingleRecipes2(c fuego.ContextNoBody) (fuego.Templ, error) {
123-
id := c.QueryParam("id")
123+
id := c.PathParam("id")
124124

125125
recipe, err := rs.RecipesQueries.GetRecipe(c.Context(), id)
126126
if err != nil {
@@ -244,7 +244,7 @@ func (rs Ressource) addRecipe(c *fuego.ContextWithBody[store.CreateRecipeParams]
244244
}
245245

246246
func (rs Ressource) RecipePage(c fuego.ContextNoBody) (fuego.HTML, error) {
247-
id := c.QueryParam("id")
247+
id := c.PathParam("id")
248248

249249
recipe, err := rs.RecipesQueries.GetRecipe(c.Context(), id)
250250
if err != nil {

examples/full-app-gourmet/views/recipe_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net/http/httptest"
66
"os"
7-
87
"testing"
98
"time"
109

examples/full-app-gourmet/views/views.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (rs Ressource) Routes(s *fuego.Server) {
1414
fuego.Get(s, "/", rs.showIndex, cache.New())
1515
fuego.Get(s, "/recipes", rs.showRecipes)
1616
fuego.Get(s, "/planner", rs.planner)
17-
fuego.Get(s, "/recipes/one", rs.showSingleRecipes2)
17+
fuego.Get(s, "/recipes/{id}", rs.showSingleRecipes2)
1818
fuego.Get(s, "/recipes/related", rs.relatedRecipes)
1919
fuego.Post(s, "/recipes-new", rs.addRecipe)
2020
fuego.Get(s, "/ingredients", rs.showIngredients)
@@ -31,13 +31,14 @@ func (rs Ressource) Routes(s *fuego.Server) {
3131
fuego.UseStd(adminRoutes, basicauth.New(basicauth.Config{Username: os.Getenv("ADMIN_USER"), Password: os.Getenv("ADMIN_PASSWORD")}))
3232
fuego.Get(adminRoutes, "", rs.pageAdmin)
3333
fuego.Get(adminRoutes, "/recipes", rs.adminRecipes)
34-
fuego.All(adminRoutes, "/recipes/one", rs.adminOneRecipe)
34+
fuego.All(adminRoutes, "/recipes/{id}", rs.adminOneRecipe)
35+
fuego.Get(adminRoutes, "/recipes/create", rs.adminCreateRecipePage)
3536
fuego.Put(adminRoutes, "/recipes/edit", rs.editRecipe)
3637
fuego.Post(adminRoutes, "/recipes-new", rs.adminAddRecipes)
3738
fuego.Post(adminRoutes, "/dosings-new", rs.adminAddDosing)
3839
fuego.Get(adminRoutes, "/ingredients", rs.adminIngredients)
3940
fuego.Get(adminRoutes, "/ingredients/create", rs.adminIngredientCreationPage)
40-
fuego.All(adminRoutes, "/ingredients/one", rs.adminOneIngredient)
41+
fuego.All(adminRoutes, "/ingredients/{id}", rs.adminOneIngredient)
4142

4243
fuego.Post(adminRoutes, "/ingredients/new", rs.adminCreateIngredient)
4344
fuego.Get(adminRoutes, "/users", rs.adminRecipes)

0 commit comments

Comments
 (0)