From 6611640a35bf8621db9891b70554c722c99d623a Mon Sep 17 00:00:00 2001 From: kekskurse Date: Sat, 15 Jan 2022 12:43:27 +0100 Subject: [PATCH] #4 Update Gallery Name --- gallery.go | 74 ++++++++++++++++++++++++++++++++++++- main.go | 4 +- templates/gallery_edit.tmpl | 11 ++++-- templates/gallery_list.tmpl | 24 ++++++++++++ templates/inc/top.tmpl | 3 ++ templates/user_gallery.tmpl | 2 +- 6 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 templates/gallery_list.tmpl diff --git a/gallery.go b/gallery.go index c1519c5..4972ccb 100644 --- a/gallery.go +++ b/gallery.go @@ -100,6 +100,78 @@ func galleryNew(t template.Template, data template.Data, s session.Session, c fl } -func galleryEdit(t template.Template) { +func galleryEditList(t template.Template, data template.Data, s session.Session, c flamego.Context) { + if s.Get("user_id") == nil { + c.Redirect("/login", http.StatusTemporaryRedirect) + return + } + + galleries := []Gallery{} + err := sqlConnection.Select(&galleries, "SELECT * FROM gallery WHERE owner_id = ? ORDER BY id DESC", s.Get("user_id")) + if err != nil { + panic(err) + } + + data["galleries"] = galleries + + var list [][]Gallery + var tmp []Gallery + + for _, g := range galleries { + if len(tmp) == 6 { + list = append(list, tmp) + tmp = []Gallery{} + } + tmp = append(tmp, g) + } + list = append(list, tmp) + + data["list"] = list + t.HTML(http.StatusOK, "gallery_list") + return +} + +func galleryEditSave(c flamego.Context, s session.Session) { + if s.Get("user_id") == nil { + c.Redirect("/login", http.StatusTemporaryRedirect) + return + } + + galleries := []Gallery{} + err := sqlConnection.Select(&galleries, "SELECT * FROM gallery WHERE id = ? ORDER BY id DESC", c.Param("id")) + if err != nil { + log.Warn().Err(err).Msg("Cant find Gallery") + return + } + + if galleries[0].OwnerId != int64(s.Get("user_id").(int)) { + c.ResponseWriter().Write([]byte("No Access")) + c.ResponseWriter().WriteHeader(http.StatusNotAcceptable) + return + } + + _, err = sqlConnection.NamedExec("UPDATE gallery SET `name` = :name WHERE `id` = :id LIMIT 1", map[string]interface{}{ + "name": c.Request().FormValue("name"), + "id": c.Param("id"), + }) + + if err != nil { + log.Error().Err(err).Msg("Cant update Gallery") + } + + c.Redirect("/gallery/edit/"+c.Param("id")) + +} + +func galleryEdit(t template.Template,data template.Data, c flamego.Context) { + galleries := []Gallery{} + err := sqlConnection.Select(&galleries, "SELECT * FROM gallery WHERE id = ? ORDER BY id DESC", c.Param("id")) + if err != nil { + log.Warn().Err(err).Msg("Cant find Gallery") + return + } + data["gallery"] = galleries[0] + + t.HTML(http.StatusOK, "gallery_edit") } \ No newline at end of file diff --git a/main.go b/main.go index 6463f69..06b5bba 100644 --- a/main.go +++ b/main.go @@ -182,7 +182,9 @@ func main() { f.Get("/gallery/new", galleryNewForm) f.Post("/gallery/new", galleryNew) - f.Get("/gallery/edit", galleryEdit) + f.Get("/gallery/edit", galleryEditList) + f.Get("/gallery/edit/{id}", galleryEdit) + f.Post("/gallery/edit/{id}", galleryEditSave) f.Get("/file/upload", uploadImageForm) f.Post("/file/upload", uploadImage) diff --git a/templates/gallery_edit.tmpl b/templates/gallery_edit.tmpl index e32bb34..81ea815 100644 --- a/templates/gallery_edit.tmpl +++ b/templates/gallery_edit.tmpl @@ -22,26 +22,29 @@ A disabled link item!--> +
+
-
- +
+ + {{template "bottom" .}} \ No newline at end of file diff --git a/templates/gallery_list.tmpl b/templates/gallery_list.tmpl new file mode 100644 index 0000000..35b4cdd --- /dev/null +++ b/templates/gallery_list.tmpl @@ -0,0 +1,24 @@ +{{template "top" .}} + +
+
+

Select Gallery

+ {{ range .list }} +
+ {{ range . }} +
+
+
{{ .Name }}
+
+

+
+
+ +
+ {{ end }} +
+ {{ end }} +
+
+ + {{template "bottom" .}} \ No newline at end of file diff --git a/templates/inc/top.tmpl b/templates/inc/top.tmpl index 50ae482..d6f9ec4 100644 --- a/templates/inc/top.tmpl +++ b/templates/inc/top.tmpl @@ -17,6 +17,9 @@