From e0676fb384b1a0f634025156db1233f51b5819e7 Mon Sep 17 00:00:00 2001 From: Jan C Date: Mon, 15 Feb 2016 09:13:46 +0100 Subject: =?UTF-8?q?grils=20k=C3=B6nnen=20nun=20auch=20im=20backend=20herum?= =?UTF-8?q?geworfen=20werden.=20sobald=20die=20datenbankfunktion=20funktio?= =?UTF-8?q?niert=20geht=20das=20auch=20so=20richtig.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/assets_src/js/list.js b/assets_src/js/list.js index 8246950..b3541e0 100644 --- a/assets_src/js/list.js +++ b/assets_src/js/list.js @@ -19,18 +19,36 @@ async function resultClicked(grilId) { document.getElementById('gril-list').innerHTML += response; } -function listButtonHandler(e) { +async function updateGrilOrder(gril, pos) { + return await ajax.post( + `${window.location}/order`, + `gril=${gril}&pos=${pos}`, + { headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + } }); +} + +async function listButtonHandler(e) { let liFilter = x => x.tagName && x.tagName.toLowerCase() === 'li'; let item = dom.closest(e.target, liFilter); let pItem = dom.previous(item, liFilter), nItem = dom.next(dom.next(item, liFilter), liFilter); switch (e.target.classList[0]) { case 'gril-sort-up': - //TODO: backend + try { + await updateGrilOrder(item.id, -1); + } catch(e) { + alert(e.message); + } item.parentNode.insertBefore(item, pItem); break; case 'gril-sort-down': - //TODO: backend + try { + await updateGrilOrder(item.id, 1); + } catch(e) { + alert(e.message); + } + if (nItem) { item.parentNode.insertBefore(item, nItem); } else { diff --git a/modules/lists/lists.go b/modules/lists/lists.go index ec29ea8..b40ee18 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -67,6 +67,7 @@ func (m *Module) Init(g *grilist.Grilist) { } m.grils = grilsModule m.g.Router.GET("/list/:id", m.viewList) + m.g.Router.POST("/list/:id/order", m.updateGrilOrder) m.g.Router.POST("/list/:id", m.addGrilToList) m.g.Router.GET("/new/list", m.displayCreateList) m.g.Router.POST("/new/list", m.createList) @@ -341,6 +342,85 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter http.Redirect(w, r, fmt.Sprintf("/list/%d", id), 302) } +func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + slistID := p.ByName("id") + + user, err := m.g.Charakterin.GetUserFromRequest(r) + if err != nil { + http.Error(w, "403", http.StatusForbidden) + return + } + + listID, err := strconv.Atoi(slistID) + if err != nil { + http.Error(w, "invalid list ID (type mismatch)", 400) + return + } + + values, err := readBody(r) + if err != nil { + http.Error(w, "invalid POST data", 400) + return + } + + grilID, err := strconv.Atoi(values.Get("gril")) + if err != nil { + http.Error(w, "invalid gril ID", 404) + return + } + + diff, err := strconv.Atoi(values.Get("pos")) + if err != nil { + http.Error(w, "invalid position diff", 400) + return + } + + list, err := m.FromID(listID) + if err != nil { + http.Error(w, "invalid list", 404) + return + } + + // find the gril in our list + var lgril *ListGril = nil + log.Println(list.Grils) + for _, g := range list.Grils { + if g.Gril.ID == grilID { + lgril = g + break + } + } + if lgril == nil { + http.Error(w, "gril not found in the list", 404) + return + } + + newPos := lgril.Order + diff + + if newPos < 0 { + newPos = 0 + } else if newPos >= len(list.Grils) { + newPos = len(list.Grils) - 1 + } + + if newPos == lgril.Order { + http.Error(w, "no change in order", 400) + return + } + + // rein in die DB damit + _, err = m.g.DB.Query(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, newPos) + if err != nil { + log.Println("error reordering gril:", err) + http.Error(w, "could not update gril order", 500) + return + } + + w.WriteHeader(200) + w.Write([]byte("ok")) + return +} + // New erstellt eine neue Instanz des Modules func New() *Module { return &Module{} diff --git a/views/includes/list_gril.html b/views/includes/list_gril.html index 57694aa..4b5e55c 100644 --- a/views/includes/list_gril.html +++ b/views/includes/list_gril.html @@ -1,5 +1,5 @@ {{ define "list_gril" }} -
  • +
  • -- cgit v0.10.1