2014-03-25 03:31:33 +00:00
package models
import "time"
type Template struct {
Id int64 ` json:"id" `
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-03-27 18:19:57 +00:00
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
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-03-27 18:19:57 +00:00
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
2014-03-25 03:31:33 +00:00
return t , err
}
// PostTemplate creates a new template in the database.
func PostTemplate ( t * Template , uid int64 ) error {
// 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 {
return err
}
// Now, let's add the user->user_templates->template mapping
2014-03-27 18:19:57 +00:00
err = db . Exec ( "INSERT OR IGNORE INTO user_templates VALUES (?,?)" , uid , t . Id ) . Error
2014-03-25 03:31:33 +00:00
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
}