From f6a8dfe2f490c5620292e01c042e3d29002f7955 Mon Sep 17 00:00:00 2001 From: Victor Lacasse-Beaudoin Date: Thu, 31 Oct 2024 19:18:45 -0400 Subject: [PATCH] feature(ui): permettre la modification de fichiers par UI --- pkg/agecemorg/client.go | 16 ++++ pkg/agecemorg/handler.go | 146 +++++++++++++++++++++++++++++++++ pkg/agecemorg/routes.go | 8 ++ ui/admin-edit.html | 55 +++++++++++++ ui/admin.html | 3 +- ui/dialog-document-delete.html | 23 ++++++ ui/dialog-document-rename.html | 32 ++++++++ ui/dialog.html | 13 +++ ui/public/css/admin-edit.css | 20 +++++ 9 files changed, 315 insertions(+), 1 deletion(-) create mode 100644 ui/admin-edit.html create mode 100644 ui/dialog-document-delete.html create mode 100644 ui/dialog-document-rename.html create mode 100644 ui/dialog.html create mode 100644 ui/public/css/admin-edit.css diff --git a/pkg/agecemorg/client.go b/pkg/agecemorg/client.go index 443a025..9a8e091 100644 --- a/pkg/agecemorg/client.go +++ b/pkg/agecemorg/client.go @@ -175,6 +175,22 @@ func (a *API) ListBuckets() (response ListBucketsResponse, err error) { return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response) } +func (a *API) ReadBucket(bucket string) (response ReadBucketResponse, err error) { + return response, a.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &response) +} + +func (a *API) UpdateDocumentKey(bucket, document, newKey string) (response UpdateDocumentKeyResponse, err error) { + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(newKey); err != nil { + return response, fmt.Errorf("handler: %s", err) + } + return response, a.Voki.Unmarshal(http.MethodPut, fmt.Sprintf("/v1/bucket/%s/%s/key", bucket, document), &buf, true, &response) +} + +func (a *API) DeleteDocument(bucket, document string) (response DeleteDocumentResponse, err error) { + return response, a.Voki.Unmarshal(http.MethodDelete, fmt.Sprintf("/v1/bucket/%s/%s", bucket, document), nil, true, &response) +} + func (a *API) Seed() (response ExecuteSeedResponse, err error) { request, err := NewV1SeedPOST() if err != nil { diff --git a/pkg/agecemorg/handler.go b/pkg/agecemorg/handler.go index 26ce6fe..5f8b886 100644 --- a/pkg/agecemorg/handler.go +++ b/pkg/agecemorg/handler.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "log" "net/http" "net/url" "sort" @@ -686,6 +687,151 @@ func HandlePageProcesVerbaux() echo.HandlerFunc { } } +func HandleAdminDocumentsEditDelete(api *API) echo.HandlerFunc { + return func(c echo.Context) error { + switch c.Request().Method { + case http.MethodGet: + var data struct { + Error string + Bucket string + Document string + } + + data.Bucket = c.Param("bucket") + data.Document = c.Param("document") + + return c.Render(http.StatusOK, "dialog-document-delete", data) + case http.MethodDelete: + var data struct { + Error string + Bucket string + Document string + DeleteDocumentResponse DeleteDocumentResponse + } + if api == nil { + errMsg := "Error: HandleAdminDocumentsEditDelete called with nil *API" + log.Println(errMsg) + data.Error = errMsg + + return c.String(http.StatusOK, data.Error) + } + data.Bucket = c.Param("bucket") + data.Document = c.Param("document") + + deleteDocumentResponse, err := api.DeleteDocument(data.Bucket, data.Document) + if err != nil { + log.Println(err) + data.Error = err.Error() + return c.Render(http.StatusOK, "dialog-error", data) + } + data.DeleteDocumentResponse = deleteDocumentResponse + + return c.Render(http.StatusOK, "dialog-document-delete-result", data) + default: + return c.String(http.StatusOK, "unsupported method") + } + } +} + +func HandleAdminDocumentsEditKey(api *API) echo.HandlerFunc { + return func(c echo.Context) error { + switch c.Request().Method { + case http.MethodGet: + var data struct { + Error string + Bucket string + Document string + } + + data.Bucket = c.Param("bucket") + data.Document = c.Param("document") + + return c.Render(http.StatusOK, "dialog-document-rename", data) + case http.MethodPut: + var data struct { + Error string + Bucket string + Document string + UpdateDocumentKeyResponse UpdateDocumentKeyResponse + } + if api == nil { + errMsg := "Error: HandleAdminDocumentsEdit called with nil *API" + log.Println(errMsg) + data.Error = errMsg + + return c.String(http.StatusOK, data.Error) + } + + data.Bucket = c.Param("bucket") + data.Document = c.Param("document") + + newKey := c.FormValue("rename-document-form-newkey") + + updateDocumentKeyResponse, err := api.UpdateDocumentKey(data.Bucket, data.Document, newKey) + if err != nil { + log.Println(err) + data.Error = err.Error() + return c.Render(http.StatusOK, "dialog-error", data) + } + data.UpdateDocumentKeyResponse = updateDocumentKeyResponse + + return c.Render(http.StatusOK, "dialog-document-rename-result", data) + default: + return c.String(http.StatusOK, "unsupported method") + } + } +} + +func HandleNothing() echo.HandlerFunc { + return func(c echo.Context) error { + return c.String(http.StatusOK, "") + } +} + +func HandleAdminDocumentsEdit(api *API) echo.HandlerFunc { + return func(c echo.Context) error { + type DataBucket struct { + ID string + DisplayName string + DocumentKeys []string + } + var data struct { + Error string + Buckets []DataBucket + } + + if api == nil { + errMsg := "Error: HandleAdminDocumentsEdit called with nil *API" + log.Println(errMsg) + data.Error = errMsg + return c.Render(http.StatusOK, "admin-edit-html", data) + } + + listBucketsResponse, err := api.ListBuckets() + if err != nil { + log.Println(err) + data.Error = err.Error() + return c.Render(http.StatusOK, "admin-edit-html", data) + } + for name, displayName := range listBucketsResponse.Data.Buckets { + readBucketResponse, err := api.ReadBucket(name) + if err != nil { + log.Println(err) + } + + dataBucket := DataBucket{ + ID: name, + DisplayName: displayName, + DocumentKeys: readBucketResponse.Data.Keys, + } + + data.Buckets = append(data.Buckets, dataBucket) + } + + return c.Render(http.StatusOK, "admin-edit-html", data) + } +} + func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error { var response HandleAdminDocumentsUploadResponse diff --git a/pkg/agecemorg/routes.go b/pkg/agecemorg/routes.go index 73c9f9b..52a73f7 100644 --- a/pkg/agecemorg/routes.go +++ b/pkg/agecemorg/routes.go @@ -152,6 +152,8 @@ func RunServer(cfg Config) { e.GET("/", HandleIndex) + e.GET("/nothing", HandleNothing()) + //e.GET("/a-propos", HandleAPropos) //e.GET("/actualite", HandleActualite) @@ -180,6 +182,12 @@ func RunServer(cfg Config) { groupAdmin.POST("/documents/upload", webHandler.HandleAdminDocumentsUploadPOST) + groupAdmin.GET("/documents/edit", HandleAdminDocumentsEdit(webHandler.ApiClient)) + groupAdmin.GET("/documents/edit/:bucket/:document/delete", HandleAdminDocumentsEditDelete(webHandler.ApiClient)) + groupAdmin.DELETE("/documents/edit/:bucket/:document", HandleAdminDocumentsEditDelete(webHandler.ApiClient)) + groupAdmin.GET("/documents/edit/:bucket/:document/key", HandleAdminDocumentsEditKey(webHandler.ApiClient)) + groupAdmin.PUT("/documents/edit/:bucket/:document/key", HandleAdminDocumentsEditKey(webHandler.ApiClient)) + e.Logger.Fatal(e.Start( fmt.Sprintf(":%d", cfg.Server.Port))) } diff --git a/ui/admin-edit.html b/ui/admin-edit.html new file mode 100644 index 0000000..5867bae --- /dev/null +++ b/ui/admin-edit.html @@ -0,0 +1,55 @@ +{{ define "admin-edit-html" }} + + + + + + AGECEM + {{ template "general-html" }} + + + + + + {{ template "header-html" }} + {{ if .Error }} + {{ .Error }} + {{ end }} +
+

Modifier les documents

+ + {{ range .Buckets }} + {{ $bucketID := .ID }} +

{{ .DisplayName }} ({{ .ID }})

+ {{ if not .DocumentKeys }} +

Aucun document de type {{ .DisplayName }} enregistré, voir le panneau d'administration pour en ajouter.

+ {{ else }} + + + {{ range .DocumentKeys }} + + + + + + {{ end }} +
RenommerEffacer
{{ . }} + + + +
+ {{ end }} + {{ end }} + +
+ {{ template "footer-html" }} + + + +{{ end }} diff --git a/ui/admin.html b/ui/admin.html index 14245d1..867ab41 100644 --- a/ui/admin.html +++ b/ui/admin.html @@ -15,10 +15,11 @@

Admin

{{ template "footer-html" }} -{{ end }} \ No newline at end of file +{{ end }} diff --git a/ui/dialog-document-delete.html b/ui/dialog-document-delete.html new file mode 100644 index 0000000..e784a6f --- /dev/null +++ b/ui/dialog-document-delete.html @@ -0,0 +1,23 @@ +{{ define "dialog-document-delete" }} +{{ if .Error }} +{{ .Error }} +{{ else }} +
+ +
+ + {{ template "dialog-button-clear" }} +
+{{ end }} +{{ end }} + +{{ define "dialog-document-delete-result" }} +{{ . }} +{{ template "dialog-button-clear" "ok" }} +{{ end }} diff --git a/ui/dialog-document-rename.html b/ui/dialog-document-rename.html new file mode 100644 index 0000000..cf030db --- /dev/null +++ b/ui/dialog-document-rename.html @@ -0,0 +1,32 @@ +{{ define "dialog-document-rename" }} +{{ if .Error }} + {{ .Error }} +{{ else }} +
+ + +
+ + {{ template "dialog-button-clear" }} +
+{{ end }} +{{ end }} + +{{ define "dialog-document-rename-result" }} +{{ . }} +{{ template "dialog-button-clear" "ok" }} +{{ end }} diff --git a/ui/dialog.html b/ui/dialog.html new file mode 100644 index 0000000..69b16cb --- /dev/null +++ b/ui/dialog.html @@ -0,0 +1,13 @@ +{{ define "dialog-button-clear" }} + +{{ end }} + +{{ define "dialog-error" }} +

Error: {{ .Error }}

+ {{ template "dialog-button-clear" }} +{{ end }} diff --git a/ui/public/css/admin-edit.css b/ui/public/css/admin-edit.css new file mode 100644 index 0000000..245b839 --- /dev/null +++ b/ui/public/css/admin-edit.css @@ -0,0 +1,20 @@ +table { + border: 1px solid; + border-collapse: collapse; +} + +td, th { + border: 1px solid; +} + +#app-dialog { + position: fixed; + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + background-color: white; + border: 1px solid #ccc; + box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3); + padding: 20px; + z-index: 555; +} -- 2.45.2