sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/modul/web/models.go

104 lines
3.6 KiB
Go
Raw Normal View History

2016-08-27 02:18:41 +02:00
package web
import (
"database/sql"
"github.com/jinzhu/gorm"
2016-09-11 18:40:33 +02:00
system "dev.sum7.de/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 {
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;primary_key"`
Path string `gorm:"type:varchar(255);column:path;primary_key" json:"path"`
}
// TableName of struct
func (Media) TableName() string { return "web_media" }
// Menu struct
type Menu struct {
ID int64 `gorm:"primary_key"`
2016-09-13 20:00:18 +02:00
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-"`
2016-08-27 02:18:41 +02:00
Name string `gorm:"type:varchar(255);column:name" json:"name"`
Shorturl string `gorm:"type:varchar(255);column:url" json:"url"`
2016-09-13 20:00:18 +02:00
ParentID sql.NullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu" json:"parentid"`
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" }
// Page struct
type Page struct {
ID int64 `gorm:"primary_key"`
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website"`
MenuID int64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE CASCADE ON DELETE CASCADE;column:menu"`
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" }
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
}
if !item.ParentID.Valid {
menuexit = append(menuexit, item)
} else {
if menumap[item.ParentID.Int64] == nil {
menumap[item.ParentID.Int64] = &Menu{ID: -1}
}
menumap[item.ParentID.Int64].Children = append(menumap[item.ParentID.Int64].Children, item)
}
}
return menuexit
}
2016-08-27 02:18:41 +02:00
// SyncModels to verify the database schema
func SyncModels(dbconnection *gorm.DB) {
dbconnection.AutoMigrate(&Website{}, &Domain{}, &Manager{}, &Media{}, &Menu{}, &Page{})
}