diff --git a/controllers/api.go b/controllers/api.go index 27ea2c34..90d616ed 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -143,7 +143,7 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { fmt.Println(err) } 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) + _, 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 } @@ -188,13 +188,14 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { fmt.Printf("Found invalid email %s\n", t.Email) continue } - res, err := db.Conn.Exec("INSERT OR IGNORE INTO targets VALUES (null, ?)", t.Email) + _, err := db.Conn.Exec("INSERT OR IGNORE INTO targets VALUES (null, ?)", t.Email) if err != nil { fmt.Printf("Error adding email: %s\n", t.Email) } - t.Id, err = res.LastInsertId() + // Bug: res.LastInsertId() does not work for this, so we need to select it manually (how frustrating.) + t.Id, err = db.Conn.SelectInt("SELECT id FROM targets WHERE email=?", t.Email) if err != nil { - fmt.Printf("Error getting last insert id for email: %s\n", t.Email) + fmt.Printf("Error getting id for email: %s\n", t.Email) } _, err = db.Conn.Exec("INSERT OR IGNORE INTO group_targets VALUES (?,?)", g.Id, t.Id) if err != nil { diff --git a/db/db.go b/db/db.go index 66acbd93..2062a66f 100644 --- a/db/db.go +++ b/db/db.go @@ -35,7 +35,7 @@ func Setup() error { `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, 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 groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL, UNIQUE(name));`, `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));`, }