From 16f6b2565d33be12c84dc78ed5fef147d8f435d3 Mon Sep 17 00:00:00 2001 From: jan Date: Thu, 31 Dec 2015 01:19:09 +0100 Subject: =?UTF-8?q?listen=20k=C3=B6nnen=20jetzt=20erstellt=20werden.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 2b1ced3..ae1ec65 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -61,6 +61,8 @@ func (m *Module) Init(g *grilist.Grilist) { m.grils = grilsModule m.g.Router.GET("/list/:id", m.viewList) m.g.Router.POST("/list/:id", m.addGrilToList) + m.g.Router.GET("/new/list", m.displayCreateList) + m.g.Router.POST("/new/list", m.createList) } func (m *Module) getListGrils(list *List) error { @@ -260,6 +262,41 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou http.Error(w, "not implemented", 500) } +func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + user, _ := m.g.Charakterin.GetUserFromRequest(r) + + data := m.g.Renderer.DefaultData() + data["user"] = user + + m.g.Renderer.RenderPage("create_list", w, data) +} + +func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + user, err := m.g.Charakterin.GetUserFromRequest(r) + if err != nil { + log.Println(err) + http.Error(w, "500", http.StatusInternalServerError) + return + } + + values, err := readBody(r) + if err != nil { + log.Println(err) + http.Error(w, "500", http.StatusInternalServerError) + return + } + + var id int + err = m.g.DB.QueryRow(`INSERT INTO grilist.lists(user_id, name, description) VALUES($1, $2, $3) RETURNING id`, user.ID, values.Get("name"), values.Get("description")).Scan(&id) + if err != nil { + log.Println(err) + http.Error(w, "500", http.StatusInternalServerError) + return + } + + http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) +} + // New erstellt eine neue Instanz des Modules func New() *Module { return &Module{} diff --git a/views/create_list.html b/views/create_list.html new file mode 100644 index 0000000..375dddd --- /dev/null +++ b/views/create_list.html @@ -0,0 +1,47 @@ +{{ define "create_list" }} + + + {{ template "materialize" }} + grilist + + + + + {{ template "navbar" . }} +
+
+
+ {{ if .error }} +
+
+
+ Fehler +

Es gab einen Fehler: {{ .error }}

+
+
+
+ {{ end }} + +
+
+ list + +
+
+ + +
+
+
+ +
+
+
+
+
+
+ + +{{ end }} diff --git a/views/navbar.html b/views/navbar.html index 8905a1d..c456112 100644 --- a/views/navbar.html +++ b/views/navbar.html @@ -4,6 +4,7 @@