Commit 3591fad0715074b049386a0ef6ee8cda133e7f83

Thomas de Grivel 2024-10-09T14:43:21

Facts.open

diff --git a/http/http_request.c b/http/http_request.c
index a231383..39dcd68 100644
--- a/http/http_request.c
+++ b/http/http_request.c
@@ -169,6 +169,11 @@ s_tag * http_request_buf_parse (s_tag *req, s_buf *buf)
         ! compare_str_case_insensitive(&urlencoded, content_type)) {
       if (! url_www_form_decode(body_str, &body))
         goto restore;
+      if (true) {
+        err_write_1("http_request_buf_parse: body: ");
+        err_inspect_tag(&body);
+        err_write_1("\n");
+      }
       str_clean(body_str);
       tmp_req.body = body;
     }
diff --git a/http/url.c b/http/url.c
index 4515809..c1ce2c2 100644
--- a/http/url.c
+++ b/http/url.c
@@ -205,6 +205,7 @@ s_tag * url_www_form_decode (const s_str *src, s_tag *dest)
     }
     str_clean(&key);
     str_clean(&value);
+    tail = &(*tail)->next.data.list;
   }
   tmp.type = TAG_LIST;
   tmp.data.list = list;
diff --git a/httpd/fx/app/controllers/fx_controller.kc3 b/httpd/fx/app/controllers/fx_controller.kc3
index 71a6947..ddb4c0b 100644
--- a/httpd/fx/app/controllers/fx_controller.kc3
+++ b/httpd/fx/app/controllers/fx_controller.kc3
@@ -150,49 +150,26 @@ defmodule FXController do
   }
 
   def property_create = fn (path, property, value) {
-    body = """
-<html>
-  <head>
-    <title>Property create</title>
-  </head>
-  <body>
-    <p><h1>Property create</h1></p>
-    <p>Path : #{HTML.escape(inspect(path))}</p>
-    <p>Property : #{HTML.escape(inspect(property))}</p>
-    <p>Value : #{HTML.escape(inspect(value))}</p>
-  </body>
-</html>
-"""
-    %HTTP.Response{body: body}
+    Fx.property_add(path, property, value)
+    HTTPd.redirect_to("/" + path)
   }
 
   def property_delete = fn (path, property, value) {
-    body = """
-<html>
-  <head>
-    <title>Property delete</title>
-  </head>
-  <body>
-    <p><h1>Property delete</h1></p>
-    <p>Path : #{HTML.escape(inspect(path))}</p>
-    <p>Property : #{HTML.escape(inspect(property))}</p>
-    <p>Value : #{HTML.escape(inspect(value))}</p>
-  </body>
-</html>
-"""
-    %HTTP.Response{body: body}
+    Fx.property_remove(path, property, value)
+    HTTPd.redirect_to("/" + path)
   }
 
   def properties_route = fn (req) {
-    url = if req.url == "/properties/fx" do
+    url = if (req.url == "/properties/fx") do
       req.url + "/"
     else
       req.url
     end
-    if (Str.starts_with?(url, "/properties/fx/")) do
+    if Str.starts_with?(url, "/properties/fx/") do
       path = Str.slice(url, 12, -1)
-      property = req.body.property_key
-      value = req.body.property_value
+      body = req.body
+      property = body["property_key"]
+      value = body["property_value"]
       if (property == void || value == void) do
         HTTPd.error_400_page(req)
       else
diff --git a/httpd/fx/app/models/fx.kc3 b/httpd/fx/app/models/fx.kc3
index b79a1e6..741dd30 100644
--- a/httpd/fx/app/models/fx.kc3
+++ b/httpd/fx/app/models/fx.kc3
@@ -2,14 +2,25 @@ defmodule Fx do
 
   def db = Facts.database()
 
-  # Facts.open(db, "db/fx.facts")
+  Facts.open(db, "db/fx.facts")
 
   def property_index = fn (path) {
+    if Str.starts_with?(path, "./") do
+      path = Str.slice(path, 2, -1)
+    end
     Facts.collect_with_tags(db, path, p = ?, o = ?, fn (fact) {
       {p, o}
     })
   }
 
+  def property_add = fn (path, property, value) {
+    Facts.add_tags(db, path, property, value)
+  }
+
+  def property_remove = fn (path, property, value) {
+    Facts.remove_tags(db, path, property, value)
+  }
+
   def tag_index = fn (path) {
     Facts.collect_with_tags(db, path, :tag, tag = ?, fn (fact) {
       tag
diff --git a/httpd/fx/app/templates/fx/properties.html.ekc3 b/httpd/fx/app/templates/fx/properties.html.ekc3
index d67756c..7481f8f 100644
--- a/httpd/fx/app/templates/fx/properties.html.ekc3
+++ b/httpd/fx/app/templates/fx/properties.html.ekc3
@@ -6,18 +6,23 @@
     <td>#{HTML.escape(value)}</td>
     <!-- if is_admin(auth_user) do -->
     <td>  
-      <form action="/properties/<%= URL.escape(path) %>"
-            enctype="multipart/form-data"
-            method="DELETE">
-        <input id="property_key"
+      <form id="delete_property"
+            action="/properties/#{URL.escape(path)}"
+            enctype="application/x-www-form-urlencoded"
+            method="POST">
+        <input id="delete_method"
+               name="_method"
+               type="hidden"
+               value="DELETE" />
+        <input id="delete_property_key"
                name="property_key"
                type="hidden"
                value="#{HTML.escape(key)}" />
-        <input id="property_value"
+        <input id="delete_property_value"
                name="property_value"
                type="hidden"
                value="#{HTML.escape(value)}" />
-        <button type="submit"><i class="fa fa-remove" /></button>
+        <button type="submit"><i class="fas fa-trash" /></button>
       </form>
     </td>
     <!-- end -->
@@ -30,12 +35,12 @@
       enctype="application/x-www-form-urlencoded"
       method="POST">
   <div>
-    <label for="property_key">Key</label>
-    <input id="property_key" name="property_key" type="text" />
+    <label for="new_property_key">Key</label>
+    <input id="new_property_key" name="property_key" type="text" />
   </div>
   <div>
-    <label for="property_value">Value</label>
-    <input id="property_value" name="property_value" type="text" />
+    <label for="new_property_value">Value</label>
+    <input id="new_property_value" name="property_value" type="text" />
   </div>
   <div>
     <button type="submit">Add property</button>
diff --git a/httpd/fx/db/.keep b/httpd/fx/db/.keep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/httpd/fx/db/.keep
diff --git a/lib/kc3/0.1/facts.kc3 b/lib/kc3/0.1/facts.kc3
index 88e3b53..cba238d 100644
--- a/lib/kc3/0.1/facts.kc3
+++ b/lib/kc3/0.1/facts.kc3
@@ -32,6 +32,8 @@ defmodule Facts do
   def first_with_tags = cfn Tag "kc3_facts_first_with_tags" (Facts, Tag,
     Tag, Tag, Fn, Result)
 
+  def open = cfn Sw "facts_open_file" (Facts, Str)
+
   # returns true if fact was removed or is already absent
   def remove_tags = cfn Bool "kc3_facts_remove_tags" (Facts, Tag, Tag, Tag, Result)
 
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 1793a03..0191e05 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -109,6 +109,13 @@ defmodule HTTPd do
     }
   }
 
+  def redirect_to = fn (url) {
+    %HTTP.Response{code: 303,
+                   message: "See other",
+                   headers: [{"Location", url}],
+                   body: ""}
+  }
+
   def debug_page = fn (request) {
     body = "<html>
   <head>