diff --git a/controllers/api.go b/controllers/api.go index 4e955fdb..53e58931 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/mail" "strconv" "time" @@ -122,6 +123,18 @@ func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) { // API_Groups returns details about the requested group. If the campaign is not // valid, API_Groups returns null. +// Example: +/* +POST /api/groups + { "name" : "Test Group", + "targets" : ["test@example.com", "test2@example.com"] + } + +RESULT { "name" : "Test Group", + "targets" : ["test@example.com", "test2@example.com"] + "id" : 1 + } +*/ func API_Groups(w http.ResponseWriter, r *http.Request) { switch { case r.Method == "GET": @@ -160,6 +173,34 @@ func API_Groups(w http.ResponseWriter, r *http.Request) { if checkError(err, w, "Cannot insert group into database") { return } + // Let's start a transaction to handle the bulk inserting + trans, err := db.Conn.Begin() + if checkError(err, w, "Error starting transaction to insert data") { + return + } + // Now, let's add the user->user_groups->group mapping + // TODO + for _, t := range g.Targets { + if _, err = mail.ParseAddress(t.Email); err != nil { + fmt.Printf("Found invalid email %s\n", t.Email) + continue + } + res, 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() + if err != nil { + fmt.Printf("Error getting last insert 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 { + fmt.Printf("Error adding many-many mapping for %s\n", t.Email) + } + } + if checkError(trans.Commit(), w, "Error committing transaction") { + return + } gj, err := json.MarshalIndent(g, "", " ") if checkError(err, w, "Error creating JSON response") { return diff --git a/models/models.go b/models/models.go index acaeb463..14cdc422 100644 --- a/models/models.go +++ b/models/models.go @@ -55,7 +55,7 @@ type Group struct { Name string `json:"name"` ModifiedDate time.Time `json:"modified_date" db:"modified_date"` Targets []Target `json:"targets" db:"-"` - Uid int64 `json:"-"` + Uid int64 `json:"-" db:"-"` } type Target struct { diff --git a/templates/register.html b/templates/register.html index 38085385..6a84b371 100644 --- a/templates/register.html +++ b/templates/register.html @@ -7,7 +7,7 @@ - + {{%end%}}