From 0bb9dc186c272be07eee4a9d36799c1b1244eb45 Mon Sep 17 00:00:00 2001 From: Jordan Date: Tue, 4 Feb 2014 21:53:11 -0600 Subject: [PATCH] Finished implementing first version of GET, POST /api/groups --- controllers/api.go | 17 +++++++++++++---- db/db.go | 2 +- models/models.go | 1 - 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 53e58931..de9cf3a0 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -127,7 +127,12 @@ func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) { /* POST /api/groups { "name" : "Test Group", - "targets" : ["test@example.com", "test2@example.com"] + "targets" : [ + { + "email" : "test@example.com" + }, + { "email" : test2@example.com" + }] } RESULT { "name" : "Test Group", @@ -139,12 +144,12 @@ 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 WHERE g.uid=u.id AND u.api_key=?", 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 err != nil { fmt.Println(err) } - for _, g := range gs { - _, err := db.Conn.Select(&g.Targets, "SELECT t.id t.email FROM targets t, groups g, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", g.Id) + for i, _ := range gs { + _, err := db.Conn.Select(&gs[i].Targets, "SELECT t.id, t.email FROM targets t, groups g, group_targets gt WHERE gt.gid=? AND gt.tid=t.id", gs[i].Id) if checkError(err, w, "Error looking up groups") { return } @@ -179,6 +184,10 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { return } // Now, let's add the user->user_groups->group mapping + _, err = db.Conn.Exec("INSERT OR IGNORE INTO user_groups VALUES (?,?)", ctx.Get(r, "user_id").(int64), g.Id) + if err != nil { + fmt.Printf("Error adding many-many mapping for group %s\n", g.Name) + } // TODO for _, t := range g.Targets { if _, err = mail.ParseAddress(t.Email); err != nil { diff --git a/db/db.go b/db/db.go index 15eb73fa..947d992d 100644 --- a/db/db.go +++ b/db/db.go @@ -32,7 +32,7 @@ func Setup() error { //Create tables `CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key));`, `CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id));`, - `CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT NOT NULL, UNIQUE(address));`, + `CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email));`, `CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL);`, `CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid))`, `CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid));`, diff --git a/models/models.go b/models/models.go index 14cdc422..8fdc9fb8 100644 --- a/models/models.go +++ b/models/models.go @@ -55,7 +55,6 @@ type Group struct { Name string `json:"name"` ModifiedDate time.Time `json:"modified_date" db:"modified_date"` Targets []Target `json:"targets" db:"-"` - Uid int64 `json:"-" db:"-"` } type Target struct {