2016-08-27 02:18:41 +02:00
package web
import (
2017-02-26 17:02:05 +01:00
"strconv"
"strings"
"time"
2016-08-27 02:18:41 +02:00
"github.com/jinzhu/gorm"
2016-09-11 18:40:33 +02:00
2016-10-11 20:16:24 +02:00
system "dev.sum7.eu/sum7/warehost/system"
2016-08-27 02:18:41 +02:00
)
// Website struct
type Website struct {
ID int64
Name string ` gorm:"type:varchar(255);column:name" json:"name" `
Domains [ ] Domain ` gorm:"foreignkey:Domain" json:"domains" `
}
// TableName of struct
func ( Website ) TableName ( ) string { return "web_website" }
// Domain struct
type Domain struct {
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key" `
2016-09-11 23:04:26 +02:00
Name string ` gorm:"type:varchar(255);unique;column:name;primary_key" json:"name" `
2016-08-27 02:18:41 +02:00
}
// TableName of struct
func ( Domain ) TableName ( ) string { return "web_domain" }
// Manager struct
type Manager struct {
2016-09-11 18:40:33 +02:00
LoginID int64 ` sql:"type:bigint REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login;primary_key" json:"-" `
Login system . Login ` gorm:"foreignkey:Login" json:"login" `
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key" json:"-" `
Website Website ` gorm:"foreignkey:Website" json:"website" `
2016-08-27 02:18:41 +02:00
}
// TableName of struct
func ( Manager ) TableName ( ) string { return "web_manager" }
// Media struct
type Media struct {
2017-02-20 18:58:53 +01:00
ID int64 ` gorm:"primary_key" `
2017-02-26 17:02:05 +01:00
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;unique_index:media_website_path" json:"-" `
Path string ` gorm:"type:varchar(255);column:path;unique_index:media_website_path" json:"path" `
2016-08-27 02:18:41 +02:00
}
// TableName of struct
func ( Media ) TableName ( ) string { return "web_media" }
// Menu struct
type Menu struct {
2016-11-15 20:50:57 +01:00
ID int64 ` gorm:"primary_key" `
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-" `
Name string ` gorm:"type:varchar(255);column:name" json:"name" `
ParentID * int64 ` sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu" json:"parentid" `
2017-02-20 18:58:53 +01:00
URLID * int64 ` sql:"type:bigint REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE SET NULL;column:url" json:"-" `
URL * URL ` gorm:"foreignkey:URL" json:"url" `
2016-11-15 20:50:57 +01:00
Position int ` sql:"type:int;column:position" json:"position" `
Children [ ] * Menu ` json:"children" `
2016-08-27 02:18:41 +02:00
}
// TableName of struct
func ( Menu ) TableName ( ) string { return "web_menu" }
2017-02-20 18:58:53 +01:00
// URL struct
type URL struct {
ID int64 ` gorm:"primary_key" `
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;unique_index:url_website_path" gorm:"unique_index:idx_url_website_path" json:"-" `
Path string ` gorm:"type:varchar(255);column:path;unique_index:idx_url_website_path" json:"path" `
}
// TableName of struct
func ( URL ) TableName ( ) string { return "web_url" }
2016-08-27 02:18:41 +02:00
// Page struct
type Page struct {
ID int64 ` gorm:"primary_key" `
2016-09-27 11:13:01 +02:00
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-" `
2017-02-20 18:58:53 +01:00
URLID int64 ` sql:"type:bigint unique REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE CASCADE;column:url" json:"-" `
URL * URL ` gorm:"foreignkey:URL" json:"url" `
2016-08-27 02:18:41 +02:00
Title string ` gorm:"type:varchar(255);column:title" json:"title" `
Content string ` gorm:"type:text;column:content" json:"content" `
}
// TableName of struct
func ( Page ) TableName ( ) string { return "web_page" }
2017-02-26 17:02:05 +01:00
// PostURLType of URL Schema on a blog
type PostURLType int
const (
// PostURLDate http(s)://DOMAIN/url/year/month/day/posttitle
PostURLDate = 0
// PostURLTitle http(s)://DOMAIN/url/posttitle
PostURLTitle = 1
)
// Blog struct
type Blog struct {
ID int64 ` gorm:"primary_key" `
WebsiteID int64 ` sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-" `
URLID int64 ` sql:"type:bigint unique REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE CASCADE;column:url" json:"-" `
URL * URL ` gorm:"foreignkey:URL" json:"url" `
PostURL PostURLType ` sql:"type:int;column:posturl" json:"posturl" `
PreviewEnable bool ` gorm:"column:preview_enable" json:"preview_enable" `
AuthorEnable bool ` gorm:"column:author_enable" json:"author_enable" `
CreateAtEnable bool ` gorm:"column:createat_enable" json:"createat_enable" `
TimerangeEnable bool ` gorm:"column:timerange_enable" json:"timerange_enable" `
Title string ` gorm:"type:varchar(255);column:title" json:"title" `
Content string ` gorm:"type:text;column:content" json:"content" `
Posts [ ] * BlogPost ` gorm:"foreignkey:Blog" json:"posts" `
}
// TableName of struct
func ( Blog ) TableName ( ) string { return "web_blog" }
// BlogPost struct
type BlogPost struct {
ID int64 ` gorm:"primary_key" `
BlogID int64 ` sql:"type:bigint REFERENCES web_blog(id) ON UPDATE CASCADE ON DELETE CASCADE;column:blog" json:"-" `
Blog * Blog ` json:"blog" `
CreateAt time . Time ` sql:"default:current_timestamp" gorm:"column:createat" json:"createat" `
Author string ` gorm:"type:varchar(255);column:author" json:"author" `
Title string ` gorm:"type:varchar(255);column:title" json:"title" `
Location string ` gorm:"type:varchar(255);column:location" json:"location" `
Start time . Time ` gorm:"column:start" json:"start" `
End time . Time ` gorm:"column:end" json:"end" `
Link string ` gorm:"type:varchar(255);column:link" json:"link" `
PreviewID * int64 ` sql:"type:bigint REFERENCES web_media(id) ON UPDATE CASCADE ON DELETE SET NULL;column:preview" json:"-" `
Preview * Media ` gorm:"foreignkey:Media" json:"preview" `
Summary string ` gorm:"type:text;column:summary" json:"summary" `
Content string ` gorm:"type:text;column:content" json:"content" `
}
func ( bg * BlogPost ) GetURL ( ) string {
var parts [ ] string
if blog := bg . Blog ; blog != nil {
parts = append ( parts , blog . URL . Path )
if blog . PostURL == 1 {
month := strconv . Itoa ( int ( bg . CreateAt . Month ( ) ) )
if len ( month ) == 1 {
month = "0" + month
}
parts = append ( parts , strconv . Itoa ( bg . CreateAt . Year ( ) ) , month )
}
}
parts = append ( parts , bg . Title )
return "/" + FixPath ( strings . Join ( parts , "/" ) )
}
// TableName of struct
func ( BlogPost ) TableName ( ) string { return "web_blogpost" }
2016-09-12 20:34:09 +02:00
// BuildMenuTree format a array of Menu to a Tree of Menu (with Children)
func BuildMenuTree ( items [ ] * Menu ) [ ] * Menu {
menumap := make ( map [ int64 ] * Menu )
var menuexit [ ] * Menu
for _ , item := range items {
if menumap [ item . ID ] == nil {
menumap [ item . ID ] = item
}
if menumap [ item . ID ] . ID == - 1 {
tmp := menumap [ item . ID ] . Children
menumap [ item . ID ] = item
menumap [ item . ID ] . Children = tmp
}
2016-11-15 20:50:57 +01:00
if item . ParentID == nil {
2016-09-12 20:34:09 +02:00
menuexit = append ( menuexit , item )
} else {
2016-11-15 20:50:57 +01:00
if menumap [ * item . ParentID ] == nil {
menumap [ * item . ParentID ] = & Menu { ID : - 1 }
2016-09-12 20:34:09 +02:00
}
2016-11-15 20:50:57 +01:00
menumap [ * item . ParentID ] . Children = append ( menumap [ * item . ParentID ] . Children , item )
2016-09-12 20:34:09 +02:00
}
}
return menuexit
}
2016-08-27 02:18:41 +02:00
// SyncModels to verify the database schema
func SyncModels ( dbconnection * gorm . DB ) {
2017-02-26 17:02:05 +01:00
dbconnection . AutoMigrate ( & Website { } , & Domain { } , & Manager { } , & URL { } , & Media { } , & Menu { } , & Page { } , & Blog { } , & BlogPost { } )
2016-08-27 02:18:41 +02:00
}