From 25cbaf92ce9d88db1ee6538567c68e4bfffc02ee Mon Sep 17 00:00:00 2001 From: Jordan Date: Wed, 28 May 2014 18:48:30 -0500 Subject: [PATCH] Changed templates to have a 1-1 relationship with user (will implement sharing differently later) Working on implementing /api/template/:id methods Removed API_Campaigns_Id_Launch method (will just POST using template from angularjs scope later) --- controllers/api.go | 49 ++++++++++++++++--- models/models.go | 1 - models/template.go | 30 +++++++++--- .../js/app/partials/modals/templateModal.html | 4 +- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index a8e40ace..53758e40 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -124,10 +124,6 @@ func API_Campaigns_Id(w http.ResponseWriter, r *http.Request) { } } -func API_Campaigns_Id_Launch(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/", 302) -} - // API_Groups returns details about the requested group. If the campaign is not // valid, API_Groups returns null. // Example: @@ -267,7 +263,8 @@ func API_Templates(w http.ResponseWriter, r *http.Request) { return } t.ModifiedDate = time.Now() - err = models.PostTemplate(&t, ctx.Get(r, "user_id").(int64)) + t.UserId = ctx.Get(r, "user_id").(int64) + err = models.PostTemplate(&t) if checkError(err, w, "Error inserting template", http.StatusInternalServerError) { return } @@ -280,7 +277,47 @@ func API_Templates(w http.ResponseWriter, r *http.Request) { } func API_Templates_Id(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/", 302) + vars := mux.Vars(r) + id, _ := strconv.ParseInt(vars["id"], 0, 64) + switch { + case r.Method == "GET": + t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64)) + if checkError(err, w, "No template found", http.StatusNotFound) { + return + } + tj, err := json.MarshalIndent(t, "", " ") + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { + return + } + writeJSON(w, tj) + case r.Method == "DELETE": + err := models.DeleteTemplate(id, ctx.Get(r, "user_id").(int64)) + if checkError(err, w, "Error deleting group", http.StatusInternalServerError) { + return + } + writeJSON(w, []byte("{\"success\" : \"true\"}")) + case r.Method == "PUT": + _, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64)) + if checkError(err, w, "No group found", http.StatusNotFound) { + return + } + t := models.Template{} + err = json.NewDecoder(r.Body).Decode(&t) + if t.Id != id { + http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest) + return + } + t.ModifiedDate = time.Now() + err = models.PutTemplate(&t, ctx.Get(r, "user_id").(int64)) + if checkError(err, w, "Error updating group", http.StatusInternalServerError) { + return + } + tj, err := json.MarshalIndent(t, "", " ") + if checkError(err, w, "Error creating JSON response", http.StatusInternalServerError) { + return + } + writeJSON(w, tj) + } } func writeJSON(w http.ResponseWriter, c []byte) { diff --git a/models/models.go b/models/models.go index 3b3aed0a..76bc804e 100644 --- a/models/models.go +++ b/models/models.go @@ -50,7 +50,6 @@ func Setup() error { db.CreateTable(Group{}) db.CreateTable(GroupTarget{}) db.CreateTable(Template{}) - db.CreateTable(UserTemplate{}) db.CreateTable(Campaign{}) //Create the default user init_user := User{ diff --git a/models/template.go b/models/template.go index a26c1d8e..48ebf928 100644 --- a/models/template.go +++ b/models/template.go @@ -4,6 +4,7 @@ import "time" type Template struct { Id int64 `json:"id"` + UserId int64 `json:"-"` Name string `json:"name"` Text string `json:"text"` Html string `json:"html"` @@ -18,32 +19,45 @@ type UserTemplate struct { // GetTemplates returns the templates owned by the given user. func GetTemplates(uid int64) ([]Template, error) { ts := []Template{} - err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=?", uid).Scan(&ts).Error + err := db.Where("user_id=?", uid).Find(&ts).Error + if err != nil { + Logger.Println(err) + return ts, err + } return ts, err } // GetTemplate returns the template, if it exists, specified by the given id and user_id. func GetTemplate(id int64, uid int64) (Template, error) { t := Template{} - err := db.Table("templates t").Select("t.*").Joins("left join user_templates ut ON t.id = ut.template_id").Where("ut.user_id=? and t.id=?", uid, id).Scan(&t).Error + err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error + if err != nil { + Logger.Println(err) + return t, err + } return t, err } // PostTemplate creates a new template in the database. -func PostTemplate(t *Template, uid int64) error { +func PostTemplate(t *Template) error { // Insert into the DB err := db.Save(t).Error if err != nil { + Logger.Println(err) return err } - // Now, let's add the user->user_templates->template mapping - err = db.Exec("INSERT OR IGNORE INTO user_templates VALUES (?,?)", uid, t.Id).Error - if err != nil { - Logger.Printf("Error adding many-many mapping for template %s\n", t.Name) - } return nil } func PutTemplate(t *Template, uid int64) error { return nil } + +func DeleteTemplate(id int64, uid int64) error { + err := db.Debug().Where("user_id=?", uid).Delete(Template{Id: id}).Error + if err != nil { + Logger.Println(err) + return err + } + return nil +} diff --git a/static/js/app/partials/modals/templateModal.html b/static/js/app/partials/modals/templateModal.html index b222780a..dddf7b6d 100644 --- a/static/js/app/partials/modals/templateModal.html +++ b/static/js/app/partials/modals/templateModal.html @@ -1,6 +1,6 @@ @@ -34,6 +34,6 @@