From 876530b3da8377e7072824bca5b3844c682eb37a Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 23 Mar 2016 23:01:52 +0100 Subject: jetzt spammen wir auch keine pq connections mehr diff --git a/modules/lists/lists.go b/modules/lists/lists.go index a42c51c..3febdba 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -2,7 +2,7 @@ package lists import ( "database/sql" - "encoding/json" + "encoding/json" "errors" "fagott.pw/charakterin" "fagott.pw/grilist/frontend" @@ -15,8 +15,8 @@ import ( "log" "net/http" "net/url" - "strconv" "sort" + "strconv" ) // Module und so. @@ -44,8 +44,9 @@ type ListGril struct { // ListGrils ist die Sort-Interface Implementation für Grils einer Liste. type ListGrils []*ListGril -func (l ListGrils) Len() int { return len(l) } -func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] } + +func (l ListGrils) Len() int { return len(l) } +func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] } func (l ListGrils) Less(i, j int) bool { return l[i].Order < l[j].Order } // Name gibt den Namen des Moduls zurück @@ -68,13 +69,13 @@ 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/order", m.updateGrilOrder) m.g.Router.POST("/list/:id", m.addGrilToList) m.g.Router.DELETE("/list/:id/order", m.removeGrilFromList) m.g.Router.GET("/new/list", m.displayCreateList) m.g.Router.POST("/new/list", m.createList) - - m.g.Router.GET("/api/lists/user", m.APIgetUserLists) + + m.g.Router.GET("/api/lists/user", m.APIgetUserLists) } func (m *Module) getListGrils(list *List) error { @@ -143,14 +144,14 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List { if err != nil { log.Println(err) } - - if withGrils { - for _, list := range lists { - if err := m.getListGrils(list); err != nil { - log.Println(err) - } - } - } + + if withGrils { + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + log.Println(err) + } + } + } return lists } @@ -160,14 +161,14 @@ func (m *Module) FromID(id int, withGrils bool) (*List, error) { if err != nil { return nil, err } - - if withGrils { - for _, list := range lists { - if err := m.getListGrils(list); err != nil { - return nil, err - } - } - } + + if withGrils { + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + return nil, err + } + } + } if len(lists) == 0 { return nil, errors.New("no list found") @@ -213,11 +214,11 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar log.Println(err) return categories } - for _, list := range lists { - if err := m.getListGrils(list); err != nil { - log.Println(err) - } - } + for _, list := range lists { + if err := m.getListGrils(list); err != nil { + log.Println(err) + } + } categories = append(categories, grilist.DashboardCategory{ Title: "Neueste Listen", @@ -264,12 +265,12 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P func (m *Module) addGrilToList(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 - } + + 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 { @@ -294,15 +295,15 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou http.Error(w, "invalid list", 404) return } - - if list.Owner.ID != user.ID { - http.Error(w, "403", http.StatusForbidden) - return - } + + if list.Owner.ID != user.ID { + http.Error(w, "403", http.StatusForbidden) + return + } rank := 0 if len(list.Grils) > 0 { - rank = list.Grils[len(list.Grils) - 1].Order + 1 + rank = list.Grils[len(list.Grils)-1].Order + 1 } // rein in die DB damit @@ -324,7 +325,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou data["Index"] = len(list.Grils) value := make(map[string]interface{}) value["IsListOwner"] = true - value["Gril"] = ListGril{ gril, rank } + value["Gril"] = ListGril{gril, rank} data["Value"] = value m.g.Renderer.RenderPage("list_gril", w, data) return @@ -371,8 +372,8 @@ func (m *Module) createList(w http.ResponseWriter, r *http.Request, p httprouter func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httprouter.Params) { slistID := p.ByName("id") - - user, err := m.g.Charakterin.GetUserFromRequest(r) + + user, err := m.g.Charakterin.GetUserFromRequest(r) if err != nil { http.Error(w, "403", http.StatusForbidden) return @@ -395,23 +396,23 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr http.Error(w, "invalid gril ID", 404) return } - - pos, err := strconv.Atoi(values.Get("pos")) - if err != nil { - http.Error(w, "invalid position", 400) - return - } - + + pos, err := strconv.Atoi(values.Get("pos")) + if err != nil { + http.Error(w, "invalid position", 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, pos) + _, err = m.g.DB.Exec(`SELECT grilist.set_gril_order($1, $2, $3, $4)`, user.ID, listID, grilID, pos) 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")) + w.WriteHeader(200) + w.Write([]byte("ok")) return } @@ -435,47 +436,47 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht http.Error(w, "invalid gril ID", 404) return } - - res, err := m.g.DB.Exec(`DELETE FROM grilist.lists_grils WHERE list_id=$1 AND gril_id=$2`, listID, grilID) + + res, err := m.g.DB.Exec(`DELETE FROM grilist.lists_grils WHERE list_id=$1 AND gril_id=$2`, listID, grilID) if err != nil { log.Println("error removing gril:", err) http.Error(w, "could not remove gril", 500) return } - - if r, err := res.RowsAffected(); err != nil || r == 0 { - log.Println("invalid rows affected in removing gril") - http.Error(w, "gril not in list / other error", 500) - return - } - w.WriteHeader(200) - w.Write([]byte("ok")) + if r, err := res.RowsAffected(); err != nil || r == 0 { + log.Println("invalid rows affected in removing gril") + http.Error(w, "gril not in list / other error", 500) + return + } + + w.WriteHeader(200) + w.Write([]byte("ok")) return } func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - user, err := m.g.Charakterin.GetUserFromRequest(r) + user, err := m.g.Charakterin.GetUserFromRequest(r) if err != nil { http.Error(w, "403", http.StatusForbidden) return } - - lists := m.GetUserLists(user, false) - - for _, list := range lists { - // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) - list.Owner = nil - } - - data, err := json.Marshal(&lists) - if err != nil { - log.Println(err) + + lists := m.GetUserLists(user, false) + + for _, list := range lists { + // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) + list.Owner = nil + } + + data, err := json.Marshal(&lists) + if err != nil { + log.Println(err) http.Error(w, "500", http.StatusInternalServerError) - return - } - w.WriteHeader(200) - w.Write(data) + return + } + w.WriteHeader(200) + w.Write(data) } // New erstellt eine neue Instanz des Modules -- cgit v0.10.1