From b16c4ef8e1ef3458f186139172362a511f925043 Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 16 Jan 2016 19:39:21 +0100 Subject: =?UTF-8?q?grils=20k=C3=B6nnen=20nun=20zu=20listen=20hinzugef?= =?UTF-8?q?=C3=BCgt=20werden,=20da=20lukas=20zu=20faul=20war.=20javascript?= =?UTF-8?q?=20wird=20vorausgesetzt.=20grils=20werden=20direkt=20hinzugef?= =?UTF-8?q?=C3=BCgt=20beim=20klicken=20ohne=20ladezeit.=20musste=20daf?= =?UTF-8?q?=C3=BCr=20ein=20paar=20hacks=20im=20templating=20verwenden,=20d?= =?UTF-8?q?a=20ein=20gewisser=20jemand=20kein=20frontend=20framework=20ver?= =?UTF-8?q?wenden=20will.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/assets/js/list.js b/assets/js/list.js index 3a58971..25ff80d 100644 --- a/assets/js/list.js +++ b/assets/js/list.js @@ -2,7 +2,13 @@ function clickSearchResult(resId) { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { - alert(xhr.status + ": " + xhr.response); + if (xhr.status !== 200) { + alert(xhr.status + ": " + xhr.response); + return; + } + + data = xhr.responseText; + document.getElementById("gril-list").innerHTML += data; } } xhr.open('POST', window.location, true); diff --git a/frontend/renderer.go b/frontend/renderer.go index 524c2c8..f311462 100644 --- a/frontend/renderer.go +++ b/frontend/renderer.go @@ -26,6 +26,12 @@ type Renderer struct { defaultData map[string]interface{} } +// RangePair für Range-Dingens. Damit man die ID weiterhin accessen kann. +type RangePair struct { + Index int + Value interface{} +} + // DefaultData gibt die Standard-Daten für die Views zurück. Das ist immer eine Kopie weil Reasons. func (r *Renderer) DefaultData() map[string]interface{} { d := make(map[string]interface{}) @@ -53,6 +59,15 @@ func New(path string) *Renderer { "sub": func(a, b int) int { return a - b }, + "makeRangePair": func(idx int, value interface{}) RangePair { + return RangePair{idx, value} + }, + "makeObject": func(name string, existing interface{}, newKey string, newData interface{}) map[string]interface{} { + d := make(map[string]interface{}) + d[name] = existing + d[newKey] = newData + return d + }, } r := &Renderer{ diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 8957c5f..85e6ce5 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -15,6 +15,7 @@ import ( "net/http" "net/url" "strconv" + "sort" ) // Module und so. @@ -40,6 +41,12 @@ type ListGril struct { Order int } +// 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) Less(i, j int) bool { return l[i].Order < l[j].Order } + // Name gibt den Namen des Moduls zurück func (m *Module) Name() string { return "Lists" @@ -90,6 +97,7 @@ func (m *Module) getListGrils(list *List) error { list.Grils = append(list.Grils, lg) } + sort.Sort(ListGrils(list.Grils)) return nil } @@ -258,8 +266,40 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou return } - log.Println("implement: add gril", grilID, "to list", listID) - http.Error(w, "not implemented", 500) + list, err := m.FromID(listID) + if err != nil { + http.Error(w, "invalid list", 404) + return + } + + rank := 0 + if len(list.Grils) > 0 { + rank = list.Grils[len(list.Grils) - 1].Order + 1 + } + + // rein in die DB damit + _, err = m.g.DB.Query(`INSERT INTO grilist.lists_grils(list_id, gril_id, "order") VALUES($1, $2, $3)`, listID, grilID, rank) + if err != nil { + log.Println("error inserting gril into list:", err) + http.Error(w, "could not insert gril", 500) + return + } + + gril, err := m.grils.FromID(grilID) + if err != nil { + log.Println("inserted gril into list but couldnt get gril afterwards:", err) + http.Error(w, "error after insert", 500) + return + } + + data := m.g.Renderer.DefaultData() + data["Index"] = len(list.Grils) + value := make(map[string]interface{}) + value["IsListOwner"] = true + value["Gril"] = ListGril{ gril, rank } + data["Value"] = value + m.g.Renderer.RenderPage("list_gril", w, data) + return } func (m *Module) displayCreateList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { diff --git a/views/list.html b/views/list.html index fa58f91..b9ef7c8 100644 --- a/views/list.html +++ b/views/list.html @@ -18,44 +18,9 @@
{{ $list.Description }}

-
    +
      {{ range $index, $lg := $list.Grils }} -
    • -
      -
      -
      -
      -
      -
      - {{ $lg.Gril.RomajiName }}
      - {{ $lg.Gril.KanjiName }} -
      -
      - {{ if ($user) and eq $user.ID $list.Owner.ID }} -
      -
      -
      -
      - keyboard_arrow_up - keyboard_arrow_down -
      -
      - delete -
      -
      -
      -
      -
      - keyboard_arrow_up - keyboard_arrow_down - delete -
      -
      -
      - {{ end }} -
      -
      -
    • + {{ template "list_gril" makeRangePair $index (makeObject "Gril" $lg "IsListOwner" (and ($user) (eq $user.ID $list.Owner.ID) )) }} {{ end }}
diff --git a/views/list_gril.html b/views/list_gril.html new file mode 100644 index 0000000..5cfd0df --- /dev/null +++ b/views/list_gril.html @@ -0,0 +1,38 @@ +{{ define "list_gril" }} +
  • +
    +
    +
    +
    +
    +
    + {{ .Value.Gril.Gril.RomajiName }}
    + {{ .Value.Gril.Gril.KanjiName }} +
    +
    + {{ if (.Value.IsListOwner) }} +
    +
    +
    +
    + keyboard_arrow_up + keyboard_arrow_down +
    +
    + delete +
    +
    +
    +
    +
    + keyboard_arrow_up + keyboard_arrow_down + delete +
    +
    +
    + {{ end }} +
    +
    +
  • +{{ end }} \ No newline at end of file -- cgit v0.10.1