diff options
author | jan <jan@ruken.pw> | 2016-10-12 14:03:40 (UTC) |
---|---|---|
committer | jan <jan@ruken.pw> | 2016-10-12 14:03:40 (UTC) |
commit | def8ba82c69891244eb0f8eebe0cb0b3bb23955b (patch) | |
tree | d126befcea381077bd3074661821602d4b0c4c42 /modules/lists | |
parent | c6165a8bbcca97867205fa87456ba238beb760f9 (diff) |
alle models verschoben
Diffstat (limited to 'modules/lists')
-rw-r--r-- | modules/lists/lists.go | 49 |
1 files changed, 15 insertions, 34 deletions
diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 65e969f..cf6d7c5 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go | |||
@@ -1,7 +1,6 @@ | |||
1 | package lists | 1 | package lists |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "database/sql" | ||
5 | "encoding/json" | 4 | "encoding/json" |
6 | "errors" | 5 | "errors" |
7 | "fmt" | 6 | "fmt" |
@@ -20,7 +19,6 @@ import ( | |||
20 | "fagott.pw/grilist/models" | 19 | "fagott.pw/grilist/models" |
21 | "fagott.pw/grilist/modules/grils" | 20 | "fagott.pw/grilist/modules/grils" |
22 | "github.com/julienschmidt/httprouter" | 21 | "github.com/julienschmidt/httprouter" |
23 | "github.com/lib/pq" | ||
24 | ) | 22 | ) |
25 | 23 | ||
26 | // Module und so. | 24 | // Module und so. |
@@ -30,25 +28,8 @@ type Module struct { | |||
30 | grils *grils.GrilsModule | 28 | grils *grils.GrilsModule |
31 | } | 29 | } |
32 | 30 | ||
33 | // List ist eine Liste an DINGEN. | ||
34 | type List struct { | ||
35 | ID int | ||
36 | Name string | ||
37 | Description string | ||
38 | Owner *charakterin.User | ||
39 | ForkOf sql.NullInt64 | ||
40 | UpdatedAt pq.NullTime | ||
41 | Grils []*ListGril | ||
42 | } | ||
43 | |||
44 | // ListGril ist ein geranktes Gril | ||
45 | type ListGril struct { | ||
46 | Gril *models.Gril | ||
47 | Order int | ||
48 | } | ||
49 | |||
50 | // ListGrils ist die Sort-Interface Implementation für Grils einer Liste. | 31 | // ListGrils ist die Sort-Interface Implementation für Grils einer Liste. |
51 | type ListGrils []*ListGril | 32 | type ListGrils []*models.ListGril |
52 | 33 | ||
53 | func (l ListGrils) Len() int { return len(l) } | 34 | func (l ListGrils) Len() int { return len(l) } |
54 | func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] } | 35 | func (l ListGrils) Swap(i, j int) { l[i], l[j] = l[j], l[i] } |
@@ -88,7 +69,7 @@ func (m *Module) Init(g *grilist.Grilist) { | |||
88 | m.c = cache.New() | 69 | m.c = cache.New() |
89 | } | 70 | } |
90 | 71 | ||
91 | func (m *Module) getListGrils(list *List) error { | 72 | func (m *Module) getListGrils(list *models.List) error { |
92 | rows, err := m.g.DB.Query(`SELECT gril_id, "order" FROM grilist.lists_grils WHERE list_id = $1 ORDER BY "order" ASC`, list.ID) | 73 | rows, err := m.g.DB.Query(`SELECT gril_id, "order" FROM grilist.lists_grils WHERE list_id = $1 ORDER BY "order" ASC`, list.ID) |
93 | if err != nil { | 74 | if err != nil { |
94 | return err | 75 | return err |
@@ -98,7 +79,7 @@ func (m *Module) getListGrils(list *List) error { | |||
98 | list.Grils = list.Grils[:0] | 79 | list.Grils = list.Grils[:0] |
99 | for rows.Next() { | 80 | for rows.Next() { |
100 | var grilID int | 81 | var grilID int |
101 | lg := &ListGril{} | 82 | lg := &models.ListGril{} |
102 | 83 | ||
103 | if err := rows.Scan(&grilID, &lg.Order); err != nil { | 84 | if err := rows.Scan(&grilID, &lg.Order); err != nil { |
104 | log.Println("error scanning row in getListGrils:", err) | 85 | log.Println("error scanning row in getListGrils:", err) |
@@ -118,8 +99,8 @@ func (m *Module) getListGrils(list *List) error { | |||
118 | return nil | 99 | return nil |
119 | } | 100 | } |
120 | 101 | ||
121 | func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, error) { | 102 | func (m *Module) getLists(whereClause string, params ...interface{}) ([]*models.List, error) { |
122 | var lists []*List | 103 | var lists []*models.List |
123 | 104 | ||
124 | rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, name, description, fork_of, updated_at, user_id FROM grilist.lists WHERE %s`, whereClause), params...) | 105 | rows, err := m.g.DB.Query(fmt.Sprintf(`SELECT id, name, description, fork_of, updated_at, user_id FROM grilist.lists WHERE %s`, whereClause), params...) |
125 | if err != nil { | 106 | if err != nil { |
@@ -128,7 +109,7 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e | |||
128 | 109 | ||
129 | defer rows.Close() | 110 | defer rows.Close() |
130 | for rows.Next() { | 111 | for rows.Next() { |
131 | list := &List{} | 112 | list := &models.List{} |
132 | var ownerID int | 113 | var ownerID int |
133 | if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt, &ownerID); err != nil { | 114 | if err := rows.Scan(&list.ID, &list.Name, &list.Description, &list.ForkOf, &list.UpdatedAt, &ownerID); err != nil { |
134 | log.Println("error scanning row in getLists:", err) | 115 | log.Println("error scanning row in getLists:", err) |
@@ -151,7 +132,7 @@ func (m *Module) getLists(whereClause string, params ...interface{}) ([]*List, e | |||
151 | } | 132 | } |
152 | 133 | ||
153 | // GetUserLists gibt die Listen eines Benutzers zurück. | 134 | // GetUserLists gibt die Listen eines Benutzers zurück. |
154 | func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List { | 135 | func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*models.List { |
155 | lists, err := m.getLists(`user_id = $1`, u.ID) | 136 | lists, err := m.getLists(`user_id = $1`, u.ID) |
156 | if err != nil { | 137 | if err != nil { |
157 | log.Println(err) | 138 | log.Println(err) |
@@ -168,9 +149,9 @@ func (m *Module) GetUserLists(u *charakterin.User, withGrils bool) []*List { | |||
168 | } | 149 | } |
169 | 150 | ||
170 | // FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. | 151 | // FromID sucht nach der Liste mit der gegebenen ID und gibt sie, falls sie existiert, zurück. |
171 | func (m *Module) FromID(id int, withGrils bool) (*List, error) { | 152 | func (m *Module) FromID(id int, withGrils bool) (*models.List, error) { |
172 | if lst, ok := m.c.Get(id); ok { | 153 | if lst, ok := m.c.Get(id); ok { |
173 | l := lst.(*List) | 154 | l := lst.(*models.List) |
174 | // einen neuen User holen, wir wollen ja sicher gehen und so. | 155 | // einen neuen User holen, wir wollen ja sicher gehen und so. |
175 | if owner, err := m.g.Charakterin.GetUserByID(l.Owner.ID); err == nil { | 156 | if owner, err := m.g.Charakterin.GetUserByID(l.Owner.ID); err == nil { |
176 | l.Owner = owner | 157 | l.Owner = owner |
@@ -204,7 +185,7 @@ func (m *Module) FromID(id int, withGrils bool) (*List, error) { | |||
204 | return lists[0], nil | 185 | return lists[0], nil |
205 | } | 186 | } |
206 | 187 | ||
207 | func ListsToCards(lists []*List) []frontend.Card { | 188 | func ListsToCards(lists []*models.List) []frontend.Card { |
208 | var cards []frontend.Card | 189 | var cards []frontend.Card |
209 | 190 | ||
210 | for _, list := range lists { | 191 | for _, list := range lists { |
@@ -553,7 +534,7 @@ func (m *Module) addGrilToList(w http.ResponseWriter, r *http.Request, p httprou | |||
553 | return | 534 | return |
554 | } | 535 | } |
555 | 536 | ||
556 | lg := &ListGril{gril, rank} | 537 | lg := &models.ListGril{gril, rank} |
557 | data := m.g.Renderer.DefaultData() | 538 | data := m.g.Renderer.DefaultData() |
558 | data["Index"] = len(list.Grils) | 539 | data["Index"] = len(list.Grils) |
559 | value := make(map[string]interface{}) | 540 | value := make(map[string]interface{}) |
@@ -667,7 +648,7 @@ func (m *Module) updateGrilOrder(w http.ResponseWriter, r *http.Request, p httpr | |||
667 | 648 | ||
668 | // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird. | 649 | // wenn die liste im cache ist, die Grils clearen, damit beim naechsten aufruf die Gril-Liste neu geholt wird. |
669 | if l, ok := m.c.Get(listID); ok { | 650 | if l, ok := m.c.Get(listID); ok { |
670 | ls := l.(*List) | 651 | ls := l.(*models.List) |
671 | ls.Grils = ls.Grils[:0] | 652 | ls.Grils = ls.Grils[:0] |
672 | } | 653 | } |
673 | 654 | ||
@@ -724,7 +705,7 @@ func (m *Module) removeGrilFromList(w http.ResponseWriter, r *http.Request, p ht | |||
724 | } | 705 | } |
725 | 706 | ||
726 | if l, ok := m.c.Get(listID); ok { | 707 | if l, ok := m.c.Get(listID); ok { |
727 | ls := l.(*List) | 708 | ls := l.(*models.List) |
728 | ls.Grils = ls.Grils[:0] | 709 | ls.Grils = ls.Grils[:0] |
729 | } | 710 | } |
730 | 711 | ||
@@ -744,12 +725,12 @@ func (m *Module) APIgetUserLists(w http.ResponseWriter, r *http.Request, p httpr | |||
744 | } | 725 | } |
745 | 726 | ||
746 | lists := m.GetUserLists(user, false) | 727 | lists := m.GetUserLists(user, false) |
747 | var pubLists []List | 728 | var pubLists []models.List |
748 | 729 | ||
749 | for _, list := range lists { | 730 | for _, list := range lists { |
750 | // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) | 731 | // owner wegen SICHERHEIT rausfiltern (nodumppassword2k16) |
751 | // list.Owner = nil | 732 | // list.Owner = nil |
752 | var tempList List | 733 | var tempList models.List |
753 | tempList.Description = list.Description | 734 | tempList.Description = list.Description |
754 | tempList.ForkOf = list.ForkOf | 735 | tempList.ForkOf = list.ForkOf |
755 | tempList.ID = list.ID | 736 | tempList.ID = list.ID |