From ad3e69e6ae2c2385492e83bb8d06e9552f227400 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 6 Jul 2014 13:06:18 -0500 Subject: [PATCH] Fixed PUT /api/template/:id - Works now! --- controllers/api.go | 8 +++++++- models/template.go | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index 8e5c8684..fe7be5ea 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -162,6 +162,7 @@ func API_Groups_Id(w http.ResponseWriter, r *http.Request) { } JSONResponse(w, models.Response{Success: true, Message: "Group Deleted Successfully"}, http.StatusOK) case r.Method == "PUT": + // Change this to get from URL and uid (don't bother with id in r.Body) g = models.Group{} err = json.NewDecoder(r.Body).Decode(&g) if g.Id != id { @@ -237,8 +238,13 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) { http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest) return } + err = t.Validate() +/* if checkError(err, w, http.StatusBadRequest) { + return + }*/ t.ModifiedDate = time.Now() - err = models.PutTemplate(&t, ctx.Get(r, "user_id").(int64)) + t.UserId = ctx.Get(r, "user_id").(int64) + err = models.PutTemplate(&t) if checkError(err, w, "Error updating group", http.StatusInternalServerError) { return } diff --git a/models/template.go b/models/template.go index d28f35dd..3d20512c 100644 --- a/models/template.go +++ b/models/template.go @@ -1,6 +1,9 @@ package models -import "time" +import ( + "errors" + "time" +) type Template struct { Id int64 `json:"id"` @@ -12,14 +15,17 @@ type Template struct { ModifiedDate time.Time `json:"modified_date"` } -func (t *Template) Validate() (string, bool) { +var ErrTemplateNameNotSpecified = errors.New("Template Name not specified") +var ErrTemplateMissingParameter = errors.New("Need to specify at least plaintext or HTML format") + +func (t *Template) Validate() error { switch { case t.Name == "": - return "Template Name not specified", false + return ErrTemplateNameNotSpecified case t.Text == "" && t.HTML == "": - return "Need to specify at least plaintext or HTML format", false + return ErrTemplateMissingParameter } - return "", true + return nil } type UserTemplate struct { @@ -73,9 +79,14 @@ func PostTemplate(t *Template) error { // PutTemplate edits an existing template in the database. // Per the PUT Method RFC, it presumes all data for a template is provided. -func PutTemplate(t *Template, uid int64) error { +func PutTemplate(t *Template) error { + Logger.Println(t) + err := db.Debug().Where("id=?", t.Id).Save(t).Error + if err != nil { + Logger.Println(err) + return err + } return nil - //err := } // DeleteTemplate deletes an existing template in the database.