Browse Source

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

pull/24/head
Jordan 7 years ago
parent
commit
ad3e69e6ae
2 changed files with 25 additions and 8 deletions
  1. +7
    -1
      controllers/api.go
  2. +18
    -7
      models/template.go

+ 7
- 1
controllers/api.go 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)
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
}

+ 18
- 7
models/template.go View File

@ -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.

Loading…
Cancel
Save