Updated API (/api/groups)

pull/24/head
Jordan 2014-02-06 13:30:05 -06:00
parent 4b97a88238
commit cfd4e23b2b
2 changed files with 20 additions and 11 deletions

View File

@ -137,19 +137,12 @@ RESULT { "name" : "Test Group",
func API_Groups(w http.ResponseWriter, r *http.Request) { func API_Groups(w http.ResponseWriter, r *http.Request) {
switch { switch {
case r.Method == "GET": case r.Method == "GET":
gs := []models.Group{} gs, err := db.GetGroups(ctx.Get(r, "api_key"))
_, 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 checkError(err, w, "Cannot retrieve group information") {
if err != nil { return
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
}
} }
gj, err := json.MarshalIndent(gs, "", " ") gj, err := json.MarshalIndent(gs, "", " ")
if checkError(err, w, "Error looking up groups") { if checkError(err, w, "Error marshaling group information") {
return return
} }
writeJSON(w, gj) writeJSON(w, gj)

View File

@ -128,3 +128,19 @@ func PutCampaign(c *models.Campaign) error {
_, err := Conn.Update(c) _, err := Conn.Update(c)
return err 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
}