From cfd4e23b2b461f0f8fe639045d8a875b09f3888b Mon Sep 17 00:00:00 2001 From: Jordan Date: Thu, 6 Feb 2014 13:30:05 -0600 Subject: [PATCH] Updated API (/api/groups) --- controllers/api.go | 15 ++++----------- db/db.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 90d616ed..813eec43 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -137,19 +137,12 @@ RESULT { "name" : "Test Group", func API_Groups(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": - gs := []models.Group{} - _, err := db.Conn.Select(&gs, "SELECT g.id, g.name, g.modified_date FROM groups g, users u, user_groups ug WHERE ug.uid=u.id AND ug.gid=g.id AND u.api_key=?", ctx.Get(r, "api_key")) - if err != nil { - fmt.Println(err) - } - for i, _ := range gs { - _, err := db.Conn.Select(&gs[i].Targets, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", gs[i].Id) - if checkError(err, w, "Error looking up groups") { - return - } + gs, err := db.GetGroups(ctx.Get(r, "api_key")) + if checkError(err, w, "Cannot retrieve group information") { + return } gj, err := json.MarshalIndent(gs, "", " ") - if checkError(err, w, "Error looking up groups") { + if checkError(err, w, "Error marshaling group information") { return } writeJSON(w, gj) diff --git a/db/db.go b/db/db.go index 2062a66f..ed442f9f 100644 --- a/db/db.go +++ b/db/db.go @@ -128,3 +128,19 @@ func PutCampaign(c *models.Campaign) error { _, err := Conn.Update(c) return err } + +func GetGroups(key interface{}) ([]models.Group, error) { + gs := []models.Group{} + _, err := Conn.Select(&gs, "SELECT g.id, g.name, g.modified_date FROM groups g, user_groups ug, users u WHERE ug.uid=u.id AND ug.gid=g.id AND u.api_key=?", key) + if err != nil { + fmt.Println(err) + return gs, err + } + for i, _ := range gs { + _, err := Conn.Select(&gs[i].Targets, "SELECT t.id, t.email FROM targets t, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", gs[i].Id) + if err != nil { + fmt.Println(err) + } + } + return gs, nil +}