2014-03-25 03:31:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
|
|
type Template struct {
|
|
|
|
Id int64 `json:"id"`
|
2014-05-28 23:48:30 +00:00
|
|
|
UserId int64 `json:"-"`
|
2014-03-26 04:53:51 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Html string `json:"html"`
|
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
|
|
|
|
2014-03-27 18:19:57 +00:00
|
|
|
type UserTemplate struct {
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
TemplateId int64 `json:"-"`
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// GetTemplates returns the templates owned by the given user.
|
|
|
|
func GetTemplates(uid int64) ([]Template, error) {
|
|
|
|
ts := []Template{}
|
2014-05-28 23:48:30 +00:00
|
|
|
err := db.Where("user_id=?", uid).Find(&ts).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return ts, err
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
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{}
|
2014-05-28 23:48:30 +00:00
|
|
|
err := db.Where("user_id=? and id=?", uid, id).Find(&t).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return t, err
|
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// GetTemplateByName returns the template, if it exists, specified by the given name and user_id.
|
|
|
|
func GetTemplateByName(n string, uid int64) (Template, error) {
|
|
|
|
t := Template{}
|
|
|
|
err := db.Where("user_id=? and name=?", uid, n).Find(&t).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return t, err
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// PostTemplate creates a new template in the database.
|
2014-05-28 23:48:30 +00:00
|
|
|
func PostTemplate(t *Template) error {
|
2014-03-25 03:31:33 +00:00
|
|
|
// Insert into the DB
|
2014-03-27 18:19:57 +00:00
|
|
|
err := db.Save(t).Error
|
2014-03-25 03:31:33 +00:00
|
|
|
if err != nil {
|
2014-05-28 23:48:30 +00:00
|
|
|
Logger.Println(err)
|
2014-03-25 03:31:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// PutTemplate edits an existing template in the database.
|
|
|
|
// Per the PUT Method RFC, it presumes all data for a template is provided.
|
2014-03-25 03:31:33 +00:00
|
|
|
func PutTemplate(t *Template, uid int64) error {
|
|
|
|
return nil
|
2014-06-02 04:38:21 +00:00
|
|
|
//err :=
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2014-05-28 23:48:30 +00:00
|
|
|
|
2014-06-02 04:38:21 +00:00
|
|
|
// DeleteTemplate deletes an existing template in the database.
|
|
|
|
// An error is returned if a template with the given user id and template id is not found.
|
2014-05-28 23:48:30 +00:00
|
|
|
func DeleteTemplate(id int64, uid int64) error {
|
2014-06-02 04:38:21 +00:00
|
|
|
err := db.Where("user_id=?", uid).Delete(Template{Id: id}).Error
|
2014-05-28 23:48:30 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|