Fixed PUT /api/template/:id - Works now!

pull/24/head
Jordan 2014-07-06 13:06:18 -05:00
parent c9d00059b0
commit ad3e69e6ae
2 changed files with 25 additions and 8 deletions

View File

@ -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) JSONResponse(w, models.Response{Success: true, Message: "Group Deleted Successfully"}, http.StatusOK)
case r.Method == "PUT": case r.Method == "PUT":
// Change this to get from URL and uid (don't bother with id in r.Body)
g = models.Group{} g = models.Group{}
err = json.NewDecoder(r.Body).Decode(&g) err = json.NewDecoder(r.Body).Decode(&g)
if g.Id != id { 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) http.Error(w, "Error: /:id and template_id mismatch", http.StatusBadRequest)
return return
} }
err = t.Validate()
/* if checkError(err, w, http.StatusBadRequest) {
return
}*/
t.ModifiedDate = time.Now() 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) { if checkError(err, w, "Error updating group", http.StatusInternalServerError) {
return return
} }

View File

@ -1,6 +1,9 @@
package models package models
import "time" import (
"errors"
"time"
)
type Template struct { type Template struct {
Id int64 `json:"id"` Id int64 `json:"id"`
@ -12,14 +15,17 @@ type Template struct {
ModifiedDate time.Time `json:"modified_date"` 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 { switch {
case t.Name == "": case t.Name == "":
return "Template Name not specified", false return ErrTemplateNameNotSpecified
case t.Text == "" && t.HTML == "": 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 { type UserTemplate struct {
@ -73,9 +79,14 @@ func PostTemplate(t *Template) error {
// PutTemplate edits an existing template in the database. // PutTemplate edits an existing template in the database.
// Per the PUT Method RFC, it presumes all data for a template is provided. // 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 return nil
//err :=
} }
// DeleteTemplate deletes an existing template in the database. // DeleteTemplate deletes an existing template in the database.