sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[TASK] add blog support for media/preview of a post

This commit is contained in:
Martin Geno 2017-04-29 17:17:28 +02:00
parent e3886606fc
commit 80fab46918
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
7 changed files with 76 additions and 41 deletions

View File

@ -110,10 +110,10 @@ func handlerfunc(w http.ResponseWriter, r *http.Request) {
} else {
if !dbconnection.Where(blog).Preload("Posts", func(db *gorm.DB) *gorm.DB {
return db.Order("createat DESC")
}).First(&blog).RecordNotFound() {
}).Preload("Posts.Preview").First(&blog).RecordNotFound() {
blogpost.BlogID = blog.ID
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 {
db = db.Where("to_char(createat,'YYYY-MM') = ?", strings.Join(urlParts[len(urlParts)-3:len(urlParts)-1], "-"))
}

31
lib/data/main.go Normal file
View File

@ -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
}

View File

@ -3,6 +3,7 @@ package host
import (
"github.com/jinzhu/gorm"
"dev.sum7.eu/sum7/warehost/lib/data"
"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"`
Name string `sql:"type:varchar(255);column:name" gorm:"unique_index:idx_host_domain_mail" json:"name"`
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

View File

@ -19,7 +19,7 @@ func getBlog(w http.ResponseWriter, r *http.Request) (blog Blog, returnerr *liba
}
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 {
returnerr = &libapi.ErrorResult{Fields: []string{"blog"}, Message: "not found"}

View File

@ -7,6 +7,7 @@ import (
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
"dev.sum7.eu/sum7/warehost/lib/data"
)
// to add a new blog post
@ -29,7 +30,7 @@ func blogpostAdd(w http.ResponseWriter, r *http.Request) {
blogpost.BlogID = blog.ID
if blogpost.Preview != nil {
blogpost.PreviewID = &blogpost.Preview.ID
blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true}
blogpost.Preview = nil
}
@ -70,7 +71,7 @@ func blogpostEdit(w http.ResponseWriter, r *http.Request) {
blogpost.ID = blogpostid
if blogpost.Preview != nil {
blogpost.PreviewID = &blogpost.Preview.ID
blogpost.PreviewID = data.JsonNullInt64{Int64: blogpost.Preview.ID, Valid: true}
blogpost.Preview = nil
}

View File

@ -7,6 +7,7 @@ import (
"goji.io/pat"
libapi "dev.sum7.eu/sum7/warehost/lib/api"
"dev.sum7.eu/sum7/warehost/lib/data"
)
// 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)
if menuEntry.URL != nil {
menuEntry.URLID = &menuEntry.URL.ID
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
menuEntry.URL = nil
}
@ -76,7 +77,7 @@ func menuEdit(w http.ResponseWriter, r *http.Request) {
menuEntry.ID = menuid
if menuEntry.URL != nil {
menuEntry.URLID = &menuEntry.URL.ID
menuEntry.URLID = data.JsonNullInt64{Int64: menuEntry.URL.ID, Valid: true}
menuEntry.URL = nil
}

View File

@ -7,6 +7,7 @@ import (
"github.com/jinzhu/gorm"
"dev.sum7.eu/sum7/warehost/lib/data"
system "dev.sum7.eu/sum7/warehost/system"
)
@ -43,8 +44,8 @@ func (Manager) TableName() string { return "web_manager" }
// Media struct
type Media 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:media_website_path" json:"-"`
Path string `gorm:"type:varchar(255);column:path;unique_index:media_website_path" json:"path"`
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:idx_media_website_path" json:"path"`
}
// TableName of struct
@ -55,8 +56,8 @@ type Menu struct {
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"`
URLID *int64 `sql:"type:bigint REFERENCES web_url(id) ON UPDATE CASCADE ON DELETE SET NULL;column:url" json:"-"`
ParentID data.JsonNullInt64 `sql:"type:bigint REFERENCES web_menu(id) ON UPDATE SET NULL ON DELETE SET NULL;column:menu" json:"parentid"`
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"`
Position int `sql:"type:int;column:position" json:"position"`
Children []*Menu `json:"children"`
@ -68,7 +69,7 @@ func (Menu) TableName() string { return "web_menu" }
// 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:"-"`
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"`
}
@ -129,8 +130,8 @@ type BlogPost struct {
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"`
PreviewID data.JsonNullInt64 `sql:"type:bigint REFERENCES web_media(id) ON UPDATE CASCADE ON DELETE SET NULL;column:preview" json:"-"`
Preview *Media `gorm:"foreignkey:PreviewID" json:"preview"`
Summary string `gorm:"type:text;column:summary" json:"summary"`
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].Children = tmp
}
if item.ParentID == nil {
if !item.ParentID.Valid {
menuexit = append(menuexit, item)
} else {
if menumap[*item.ParentID] == nil {
menumap[*item.ParentID] = &Menu{ID: -1}
if menumap[item.ParentID.Int64] == nil {
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