diff options
| author | xengineering <me@xengineering.eu> | 2024-04-23 17:42:57 +0200 | 
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2024-05-09 12:19:30 +0200 | 
| commit | 2704cb76554b02f546bf3f9d2d11be98f2854b7b (patch) | |
| tree | cd167fcc3a0c27ee2943c2d7abd3acc8741406f6 | |
| parent | c5e7551dc2da9798ff51d91aa238098f5ac4605f (diff) | |
| download | ceres-2704cb76554b02f546bf3f9d2d11be98f2854b7b.tar ceres-2704cb76554b02f546bf3f9d2d11be98f2854b7b.tar.zst ceres-2704cb76554b02f546bf3f9d2d11be98f2854b7b.zip | |
Remove default recipe name
To avoid not clickable recipes on the /recipes page a default name used
to be inserted on recipe creation.
This was not a proper fix for the problem and also was annoying that the
user first had to remove the default recipe name.
This commit removes this default name.
| -rw-r--r-- | controller/recipe.go | 17 | ||||
| -rw-r--r-- | server.go | 1 | ||||
| -rw-r--r-- | view/html/recipe-edit.html | 2 | ||||
| -rw-r--r-- | view/html/recipes.html | 2 | ||||
| -rw-r--r-- | view/recipe.go | 10 | 
5 files changed, 27 insertions, 5 deletions
| diff --git a/controller/recipe.go b/controller/recipe.go index 9529b2a..b0a81d9 100644 --- a/controller/recipe.go +++ b/controller/recipe.go @@ -13,19 +13,30 @@ import (  )  func RecipeCreate(w http.ResponseWriter, r *http.Request) { +	buf, err := io.ReadAll(r.Body) +	if err != nil { +		http.Error(w, err.Error(), http.StatusInternalServerError) +		return +	} +  	recipe := model.Recipe{} -	recipe.Title = "recipe without title" +	err = json.Unmarshal(buf, &recipe) +	if err != nil { +		http.Error(w, err.Error(), http.StatusBadRequest) +		return +	} +  	recipe.LastChanged = fmt.Sprint(time.Now().Unix())  	recipe.Created = recipe.LastChanged  	var obj model.Object = &recipe -	err := model.Transaction(obj.Create) +	err = model.Transaction(obj.Create)  	if err != nil {  		http.Error(w, err.Error(), http.StatusInternalServerError)  		return  	} -	http.Redirect(w, r, "/recipe/"+recipe.Id+"?view=recipe-edit", http.StatusSeeOther) +	http.Redirect(w, r, "/recipe/"+recipe.Id, http.StatusSeeOther)  }  func RecipeUpdate(w http.ResponseWriter, r *http.Request) { @@ -28,6 +28,7 @@ func NewServer(addr string) Server {  	r.HandleFunc("/version", view.VersionRead(version)).Methods(`GET`)  	r.HandleFunc("/recipes", view.RecipesRead).Methods(`GET`) +	r.HandleFunc("/recipe/create", view.RecipeCreate).Methods(`GET`)  	r.HandleFunc("/recipe", controller.RecipeCreate).Methods(`POST`)  	r.HandleFunc("/recipe/{id:[0-9]+}", view.RecipeRead).Methods(`GET`) diff --git a/view/html/recipe-edit.html b/view/html/recipe-edit.html index 3925293..8d940f8 100644 --- a/view/html/recipe-edit.html +++ b/view/html/recipe-edit.html @@ -8,7 +8,7 @@  			<h1>Recipe editor</h1>  		</header>  		<main> -			<form action="/recipe/{{.Id}}"> +			<form action="/recipe{{if ne .Id ""}}/{{.Id}}{{end}}">  				<input type="hidden" name="id" value="{{.Id}}">  				<p> diff --git a/view/html/recipes.html b/view/html/recipes.html index 89620ed..d9e720b 100644 --- a/view/html/recipes.html +++ b/view/html/recipes.html @@ -8,7 +8,7 @@  			<h1>Recipes</h1>  		</header>  		<main> -			<p><button onclick="create('/recipe')">create</button></p> +			<p><button onclick="window.location.href='/recipe/create';">create</button></p>  {{if ne (len .) 0}}  			<p><input id="search" onkeyup="filter()" type="text" placeholder="Search for a recipe ..."></p>  			<ul id="recipes">{{range .}} diff --git a/view/recipe.go b/view/recipe.go index 7b9980d..8236476 100644 --- a/view/recipe.go +++ b/view/recipe.go @@ -51,3 +51,13 @@ func RecipeRead(w http.ResponseWriter, r *http.Request) {  		return  	}  } + +func RecipeCreate(w http.ResponseWriter, r *http.Request) { +	recipe := model.Recipe{} + +	err := html.ExecuteTemplate(w, "recipe-edit", recipe) +	if err != nil { +		http.Error(w, err.Error(), http.StatusInternalServerError) +		return +	} +} | 
