aboutsummaryrefslogtreecommitdiffstats
path: root/web/command/handler/create.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-08 23:26:05 +0100
commitcdb75d3fcbc9339b897f8c6ff4d69a577f017393 (patch)
tree5e757cd236540c2cea9874c1bc09f19548db05d5 /web/command/handler/create.go
parentb56101f1a11552067f594679a497ebd4cf7427d4 (diff)
Rewrite in Go
Diffstat (limited to 'web/command/handler/create.go')
-rw-r--r--web/command/handler/create.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/web/command/handler/create.go b/web/command/handler/create.go
new file mode 100644
index 0000000..21c6b1a
--- /dev/null
+++ b/web/command/handler/create.go
@@ -0,0 +1,53 @@
+package handler
+
+import (
+ "fmt"
+ "net/http"
+ "path"
+
+ "github.com/LMBishop/scrapbook/pkg/config"
+ "github.com/LMBishop/scrapbook/pkg/constants"
+ "github.com/LMBishop/scrapbook/pkg/index"
+ "github.com/LMBishop/scrapbook/pkg/site"
+ "github.com/LMBishop/scrapbook/web/command/html"
+ . "maragu.dev/gomponents"
+ ghttp "maragu.dev/gomponents/http"
+)
+
+func GetCreate() func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ return html.CreatePage("", "", html.CreatePageForm{}), nil
+ })
+}
+
+func PostCreate(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ err := r.ParseForm()
+ if err != nil {
+ return html.CreatePage("", err.Error(), html.CreatePageForm{}), nil
+ }
+
+ name := r.Form.Get("name")
+ host := r.Form.Get("host")
+
+ formValues := html.CreatePageForm{Name: name, Host: host}
+
+ if name == "" {
+ return html.CreatePage("", "A name must be specified", formValues), nil
+ }
+
+ if host == "" {
+ return html.CreatePage("", "A host must be specified", formValues), nil
+ }
+
+ site, err := site.CreateNewSite(name, path.Join(constants.SysDataDir, "sites"), host)
+
+ if err != nil {
+ return html.CreatePage("", fmt.Errorf("Unexpected error: %w", err).Error(), formValues), nil
+ }
+
+ index.AddSite(site)
+
+ return html.CreatePage("Successfully created new site", "", formValues), nil
+ })
+}