[TASK] add blog support for media/preview of a post
This commit is contained in:
parent
e3886606fc
commit
80fab46918
|
@ -110,10 +110,10 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
if !dbconnection.Where(blog).Preload("Posts", func(db *gorm.DB) *gorm.DB {
|
if !dbconnection.Where(blog).Preload("Posts", func(db *gorm.DB) *gorm.DB {
|
||||||
return db.Order("createat DESC")
|
return db.Order("createat DESC")
|
||||||
}).First(&blog).RecordNotFound() {
|
}).Preload("Posts.Preview").First(&blog).RecordNotFound() {
|
||||||
blogpost.BlogID = blog.ID
|
blogpost.BlogID = blog.ID
|
||||||
if (blog.PostURL == 0 && len(urlParts) > 1) || (blog.PostURL == 1 && len(urlParts) > 2) {
|
if (blog.PostURL == 0 && len(urlParts) > 1) || (blog.PostURL == 1 && len(urlParts) > 2) {
|
||||||
db := dbconnection.Where(blogpost).Where("LOWER(title) = LOWER(?)", urlParts[len(urlParts)-1])
|
db := dbconnection.Where(blogpost).Preload("Preview").Where("LOWER(title) = LOWER(?)", urlParts[len(urlParts)-1])
|
||||||
if blog.PostURL == 1 {
|
if blog.PostURL == 1 {
|
||||||
db = db.Where("to_char(createat,'YYYY-MM') = ?", strings.Join(urlParts[len(urlParts)-3:len(urlParts)-1], "-"))
|
db = db.Where("to_char(createat,'YYYY-MM') = ?", strings.Join(urlParts[len(urlParts)-3:len(urlParts)-1], "-"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
package data
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonNullInt64 sql.NullInt64
|
||||||
|
|
||||||
|
func (v JsonNullInt64) MarshalJSON() ([]byte, error) {
|
||||||
|
if v.Valid {
|
||||||
|
return json.Marshal(v.Int64)
|
||||||
|
} else {
|
||||||
|
return json.Marshal(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *JsonNullInt64) UnmarshalJSON(data []byte) error {
|
||||||
|
// Unmarshalling into a pointer will let us detect null
|
||||||
|
var x *int64
|
||||||
|
if err := json.Unmarshal(data, &x); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if x != nil {
|
||||||
|
v.Valid = true
|
||||||
|
v.Int64 = *x
|
||||||
|
} else {
|
||||||
|
v.Valid = false
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package host
|
||||||
import (
|
import (
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
"dev.sum7.eu/sum7/warehost/lib/data"
|
||||||
"dev.sum7.eu/sum7/warehost/system"
|
"dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -79,7 +80,7 @@ type Mail struct {
|
||||||
Domain *Domain `gorm:"foreignkey:Domain;unique_index:idx_host_domain_mail" json:"domain"`
|
Domain *Domain `gorm:"foreignkey:Domain;unique_index:idx_host_domain_mail" json:"domain"`
|
||||||
Name string `sql:"type:varchar(255);column:name" gorm:"unique_index:idx_host_domain_mail" json:"name"`
|
Name string `sql:"type:varchar(255);column:name" gorm:"unique_index:idx_host_domain_mail" json:"name"`
|
||||||
Forwards []*MailForward `json:"forwards"`
|
Forwards []*MailForward `json:"forwards"`
|
||||||
LoginID *int64 `sql:"type:bigint NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"`
|
LoginID data.JsonNullInt64 `sql:"type:bigint NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName of struct
|
// TableName of struct
|
||||||
|
|
|
@ -19,7 +19,7 @@ func getBlog(w http.ResponseWriter, r *http.Request) (blog Blog, returnerr *liba
|
||||||
}
|
}
|
||||||
|
|
||||||
blog = Blog{}
|
blog = Blog{}
|
||||||
dbconnection.Where(map[string]interface{}{"id": id, "website": ctx.Value("websiteid").(int64)}).Preload("URL").Preload("Posts").Find(&blog)
|
dbconnection.Where(map[string]interface{}{"id": id, "website": ctx.Value("websiteid").(int64)}).Preload("URL").Preload("Posts").Preload("Posts.Preview").Find(&blog)
|
||||||
|
|
||||||
if blog.ID <= 0 {
|
if blog.ID <= 0 {
|
||||||
returnerr = &libapi.ErrorResult{Fields: []string{"blog"}, Message: "not found"}
|
returnerr = &libapi.ErrorResult{Fields: []string{"blog"}, Message: "not found"}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"goji.io/pat"
|
"goji.io/pat"
|
||||||
|
|
||||||
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
|
"dev.sum7.eu/sum7/warehost/lib/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
// to add a new blog post
|
// to add a new blog post
|
||||||
|
@ -29,7 +30,7 @@ func blogpostAdd(w http.ResponseWriter, r *http.Request) {
|
||||||
blogpost.BlogID = blog.ID
|
blogpost.BlogID = blog.ID
|
||||||
|
|
||||||
if blogpost.Preview != nil {
|
if blogpost.Preview != nil {
|
||||||
blogpost.PreviewID = &blogpost.Preview.ID
|
blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true}
|
||||||
blogpost.Preview = nil
|
blogpost.Preview = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +71,7 @@ func blogpostEdit(w http.ResponseWriter, r *http.Request) {
|
||||||
blogpost.ID = blogpostid
|
blogpost.ID = blogpostid
|
||||||
|
|
||||||
if blogpost.Preview != nil {
|
if blogpost.Preview != nil {
|
||||||
blogpost.PreviewID = &blogpost.Preview.ID
|
blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true}
|
||||||
blogpost.Preview = nil
|
blogpost.Preview = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"goji.io/pat"
|
"goji.io/pat"
|
||||||
|
|
||||||
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
|
"dev.sum7.eu/sum7/warehost/lib/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MenuTree to give the tree of a menu back
|
// MenuTree to give the tree of a menu back
|
||||||
|
@ -42,7 +43,7 @@ func menuAdd(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
if menuEntry.URL != nil {
|
if menuEntry.URL != nil {
|
||||||
menuEntry.URLID = &menuEntry.URL.ID
|
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
|
||||||
menuEntry.URL = nil
|
menuEntry.URL = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ func menuEdit(w http.ResponseWriter, r *http.Request) {
|
||||||
menuEntry.ID = menuid
|
menuEntry.ID = menuid
|
||||||
|
|
||||||
if menuEntry.URL != nil {
|
if menuEntry.URL != nil {
|
||||||
menuEntry.URLID = &menuEntry.URL.ID
|
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
|
||||||
menuEntry.URL = nil
|
menuEntry.URL = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
|
||||||
|
"dev.sum7.eu/sum7/warehost/lib/data"
|
||||||
system "dev.sum7.eu/sum7/warehost/system"
|
system "dev.sum7.eu/sum7/warehost/system"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,8 +44,8 @@ func (Manager) TableName() string { return "web_manager" }
|
||||||
// Media struct
|
// Media struct
|
||||||
type Media struct {
|
type Media struct {
|
||||||
ID int64 `gorm:"primary_key"`
|
ID int64 `gorm:"primary_key"`
|
||||||
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website;unique_index:media_website_path" json:"-"`
|
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" gorm:"unique_index:idx_media_website_path" json:"-"`
|
||||||
Path string `gorm:"type:varchar(255);column:path;unique_index:media_website_path" json:"path"`
|
Path string `gorm:"type:varchar(255);column:path;unique_index:idx_media_website_path" json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName of struct
|
// TableName of struct
|
||||||
|
@ -55,8 +56,8 @@ type Menu struct {
|
||||||
ID int64 `gorm:"primary_key"`
|
ID int64 `gorm:"primary_key"`
|
||||||
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" json:"-"`
|
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"`
|
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"`
|
ParentID data.JsonNullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu" json:"parentid"`
|
||||||
URLID *int64 `sql:"type:bigint REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE SET NULL;column:url" json:"-"`
|
URLID data.JsonNullInt64 `sql:"type:bigint REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE SET NULL;column:url" json:"-"`
|
||||||
URL *URL `gorm:"foreignkey:URL" json:"url"`
|
URL *URL `gorm:"foreignkey:URL" json:"url"`
|
||||||
Position int `sql:"type:int;column:position" json:"position"`
|
Position int `sql:"type:int;column:position" json:"position"`
|
||||||
Children []*Menu `json:"children"`
|
Children []*Menu `json:"children"`
|
||||||
|
@ -68,7 +69,7 @@ func (Menu) TableName() string { return "web_menu" }
|
||||||
// URL struct
|
// URL struct
|
||||||
type URL struct {
|
type URL struct {
|
||||||
ID int64 `gorm:"primary_key"`
|
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:"-"`
|
WebsiteID int64 `sql:"type:bigint REFERENCES web_website(id) ON UPDATE CASCADE ON DELETE CASCADE;column:website" gorm:"unique_index:idx_url_website_path" json:"-"`
|
||||||
Path string `gorm:"type:varchar(255);column:path;unique_index:idx_url_website_path" json:"path"`
|
Path string `gorm:"type:varchar(255);column:path;unique_index:idx_url_website_path" json:"path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,8 +130,8 @@ type BlogPost struct {
|
||||||
Start time.Time `gorm:"column:start" json:"start"`
|
Start time.Time `gorm:"column:start" json:"start"`
|
||||||
End time.Time `gorm:"column:end" json:"end"`
|
End time.Time `gorm:"column:end" json:"end"`
|
||||||
Link string `gorm:"type:varchar(255);column:link" json:"link"`
|
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:"-"`
|
PreviewID data.JsonNullInt64 `sql:"type:bigint REFERENCES web_media(id) ON UPDATE CASCADE ON DELETE SET NULL;column:preview" json:"-"`
|
||||||
Preview *Media `gorm:"foreignkey:Media" json:"preview"`
|
Preview *Media `gorm:"foreignkey:PreviewID" json:"preview"`
|
||||||
Summary string `gorm:"type:text;column:summary" json:"summary"`
|
Summary string `gorm:"type:text;column:summary" json:"summary"`
|
||||||
Content string `gorm:"type:text;column:content" json:"content"`
|
Content string `gorm:"type:text;column:content" json:"content"`
|
||||||
}
|
}
|
||||||
|
@ -167,13 +168,13 @@ func BuildMenuTree(items []*Menu) []*Menu {
|
||||||
menumap[item.ID] = item
|
menumap[item.ID] = item
|
||||||
menumap[item.ID].Children = tmp
|
menumap[item.ID].Children = tmp
|
||||||
}
|
}
|
||||||
if item.ParentID == nil {
|
if !item.ParentID.Valid {
|
||||||
menuexit = append(menuexit, item)
|
menuexit = append(menuexit, item)
|
||||||
} else {
|
} else {
|
||||||
if menumap[*item.ParentID] == nil {
|
if menumap[item.ParentID.Int64] == nil {
|
||||||
menumap[*item.ParentID] = &Menu{ID: -1}
|
menumap[item.ParentID.Int64] = &Menu{ID: -1}
|
||||||
}
|
}
|
||||||
menumap[*item.ParentID].Children = append(menumap[*item.ParentID].Children, item)
|
menumap[item.ParentID.Int64].Children = append(menumap[item.ParentID.Int64].Children, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return menuexit
|
return menuexit
|
||||||
|
|
Reference in New Issue