aboutsummaryrefslogtreecommitdiffstats
path: root/web/command/handler/delete.go
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.net>2025-07-14 01:55:10 +0100
committerLeonardo Bishop <me@leonardobishop.net>2025-07-14 01:55:10 +0100
commit4c3e5c256930249798768b0195c42efca1428586 (patch)
tree0492551ddbfdfbaa192804b95d0ce11e6cbdb406 /web/command/handler/delete.go
parent08a3fb8a2b0281c3c329b33215ec7f8866add606 (diff)
Add site deletion
Diffstat (limited to 'web/command/handler/delete.go')
-rw-r--r--web/command/handler/delete.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/web/command/handler/delete.go b/web/command/handler/delete.go
new file mode 100644
index 0000000..f95e278
--- /dev/null
+++ b/web/command/handler/delete.go
@@ -0,0 +1,57 @@
+package handler
+
+import (
+ "fmt"
+ "net/http"
+
+ "github.com/LMBishop/scrapbook/pkg/config"
+ "github.com/LMBishop/scrapbook/pkg/index"
+ "github.com/LMBishop/scrapbook/web/command/html"
+ . "maragu.dev/gomponents"
+ ghttp "maragu.dev/gomponents/http"
+)
+
+func GetDelete(index *index.SiteIndex) func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ siteName := r.PathValue("site")
+ if siteName == "" {
+ return html.ErrorPage("Unknown site: " + siteName), nil
+ }
+ site := index.GetSite(siteName)
+ if site == nil {
+ return html.ErrorPage("Unknown site: " + siteName), nil
+ }
+
+ return html.DeletePage("", "", siteName), nil
+ })
+}
+
+func PostDelete(mainConfig *config.MainConfig, index *index.SiteIndex) func(http.ResponseWriter, *http.Request) {
+ return ghttp.Adapt(func(w http.ResponseWriter, r *http.Request) (Node, error) {
+ siteName := r.PathValue("site")
+ if siteName == "" {
+ return html.ErrorPage("Unknown site: " + siteName), nil
+ }
+ site := index.GetSite(siteName)
+ if site == nil {
+ return html.ErrorPage("Unknown site: " + siteName), nil
+ }
+
+ err := r.ParseForm()
+ if err != nil {
+ return html.DeletePage("", fmt.Errorf("Could not parse form: %w", err).Error(), siteName), nil
+ }
+
+ if r.FormValue("delete") != "on" {
+ return html.DeletePage("", "You need to check the box to continue", siteName), nil
+ }
+
+ err = site.DeleteDataOnDisk()
+ if err != nil {
+ return html.DeletePage("", fmt.Errorf("Error occurred during data deletion: %w", err).Error(), siteName), nil
+ }
+ index.RemoveSite(siteName)
+
+ return html.DeletePage(fmt.Sprintf("Successfully deleted site %s", siteName), "", siteName), nil
+ })
+}