2015-02-07 02:24:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Page contains the fields used for a Page model
|
|
|
|
type Page struct {
|
2015-02-07 20:31:41 +00:00
|
|
|
Id int64 `json:"id" gorm:"column:id; primary_key:yes"`
|
|
|
|
UserId int64 `json:"-" gorm:"column:user_id"`
|
2015-02-07 02:24:10 +00:00
|
|
|
Name string `json:"name"`
|
2015-02-07 20:31:41 +00:00
|
|
|
HTML string `json:"html" gorm:"column:html"`
|
2015-02-07 02:24:10 +00:00
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrPageNameNotSpecified is thrown if the name of the landing page is blank.
|
2015-02-07 20:31:41 +00:00
|
|
|
var ErrPageNameNotSpecified = errors.New("Page Name not specified")
|
2015-02-07 02:24:10 +00:00
|
|
|
|
|
|
|
// Validate ensures that a page contains the appropriate details
|
|
|
|
func (p *Page) Validate() error {
|
|
|
|
if p.Name == "" {
|
|
|
|
return ErrPageNameNotSpecified
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPages returns the pages owned by the given user.
|
|
|
|
func GetPages(uid int64) ([]Page, error) {
|
|
|
|
ps := []Page{}
|
|
|
|
err := db.Where("user_id=?", uid).Find(&ps).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return ps, err
|
|
|
|
}
|
|
|
|
return ps, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPage returns the page, if it exists, specified by the given id and user_id.
|
|
|
|
func GetPage(id int64, uid int64) (Page, error) {
|
|
|
|
p := Page{}
|
|
|
|
err := db.Where("user_id=? and id=?", uid, id).Find(&p).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return p, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPageByName returns the page, if it exists, specified by the given name and user_id.
|
|
|
|
func GetPageByName(n string, uid int64) (Page, error) {
|
|
|
|
p := Page{}
|
|
|
|
err := db.Where("user_id=? and name=?", uid, n).Find(&p).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-02-07 20:31:41 +00:00
|
|
|
return p, err
|
2015-02-07 02:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostPage creates a new page in the database.
|
|
|
|
func PostPage(p *Page) error {
|
2015-02-07 16:41:53 +00:00
|
|
|
err := p.Validate()
|
|
|
|
if err != nil {
|
2015-02-07 20:31:41 +00:00
|
|
|
Logger.Println(err)
|
2015-02-07 16:41:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-02-07 02:24:10 +00:00
|
|
|
// Insert into the DB
|
2015-02-07 16:41:53 +00:00
|
|
|
err = db.Save(p).Error
|
2015-02-07 02:24:10 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutPage edits an existing Page in the database.
|
|
|
|
// Per the PUT Method RFC, it presumes all data for a page is provided.
|
|
|
|
func PutPage(p *Page) error {
|
2015-10-23 23:35:42 +00:00
|
|
|
err := db.Where("id=?", p.Id).Save(p).Error
|
2015-02-07 02:24:10 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePage deletes an existing page in the database.
|
|
|
|
// An error is returned if a page with the given user id and page id is not found.
|
|
|
|
func DeletePage(id int64, uid int64) error {
|
|
|
|
err = db.Where("user_id=?", uid).Delete(Page{Id: id}).Error
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|