From 9005de11ef8b7cf32cc8503e8b5f134eca47b4fb Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Sat, 14 Jan 2023 18:07:47 +0100
Subject: Add new recipe edit page

Recipes should be completely editable and removable. Thus this edit page
was added.
---
 web/handler.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 web/router.go  |  2 ++
 2 files changed, 83 insertions(+)

(limited to 'web')

diff --git a/web/handler.go b/web/handler.go
index cab38ed..07d04fa 100644
--- a/web/handler.go
+++ b/web/handler.go
@@ -148,6 +148,87 @@ func recipe(db *utils.Database, templateRoot string) func(http.ResponseWriter, *
 	}
 }
 
+func recipe_edit(db *utils.Database, templateRoot string) func(http.ResponseWriter, *http.Request) {
+
+	return func(w http.ResponseWriter, r *http.Request) {
+
+		// get id from URL parameters
+		ids := r.URL.Query()["id"]
+		if len(ids) != 1 {
+			utils.Err(w, 3, len(ids))
+			return
+		}
+		idStr := ids[0]
+
+		// validate id
+		idRegex := regexp.MustCompile(VALID_ID_REGEX)
+		if !(idRegex.MatchString(idStr)) {
+			utils.Err(w, 4, idStr, VALID_ID_REGEX)
+			return
+		}
+
+		if r.Method == "GET" {
+
+			// get data from database
+			cmd := fmt.Sprintf("SELECT title,upstream_url,description_markdown FROM recipes WHERE (id='%s');", idStr)
+			log.Printf("Query: %s", cmd)
+			rows, err := db.Backend.Query(cmd)
+			if err != nil {
+				utils.Err(w, 5, err)
+				return
+			}
+			defer rows.Close()
+
+			// prepare data store
+			type Element struct {
+				Id string
+				Title string
+				UpstreamUrl string
+				DescriptionMarkdown string
+				RenderedDescriptionMarkdown string
+			}
+			elements := make([]Element, 0)
+
+			// scan database rows to data store
+			for rows.Next() {
+				var element Element
+				element.Id = idStr
+				err := rows.Scan(&element.Title, &element.UpstreamUrl, &element.DescriptionMarkdown)
+				if err != nil {
+					utils.Err(w, 2)
+					return
+				} else {
+					elements = append(elements, element)
+				}
+			}
+
+			// check result
+			if len(elements) != 1 {
+				utils.Err(w, 6, len(elements))
+				return
+			}
+
+			// render markdown
+			// var buf bytes.Buffer
+			// goldmark.Convert([]byte(elements[0].DescriptionMarkdown), &buf)
+			// elements[0].RenderedDescriptionMarkdown = buf.String()
+
+			// render and return template
+			path := filepath.Join(templateRoot, "recipe_edit.html")
+			utils.ServeTemplate(w, "recipe", path, elements[0])
+		}
+
+		if r.Method == "POST" {
+
+			// read request body
+			buffer,_ := ioutil.ReadAll(r.Body)  // FIXME error handling
+			body := string(buffer)
+			updateRecipe(db, body, idStr)
+
+		}
+	}
+}
+
 func updateRecipe(db *utils.Database, body string, idStr string) {
 
 	// execute SQL UPDATE
diff --git a/web/router.go b/web/router.go
index 5b972c2..ec67399 100644
--- a/web/router.go
+++ b/web/router.go
@@ -14,6 +14,8 @@ func RunServer(config utils.HttpConfig, db *utils.Database) {
 
 	http.HandleFunc("/recipe", recipe(db, config.Templates))
 
+	http.HandleFunc("/recipe/edit", recipe_edit(db, config.Templates))
+
 	http.HandleFunc("/recipe/image", image(config.Storage))
 
 	http.HandleFunc("/add_recipes", add_recipes(db, config.Storage, config.Static))
-- 
cgit v1.2.3-70-g09d2