diff --git a/controllers/book.go b/controllers/book.go index 069ef8e2..91033fa6 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -70,6 +70,7 @@ func (c *BookController) Dashboard() { if err == models.ErrPermissionDenied { c.Abort("403") } + beego.Error(err) c.Abort("500") } @@ -501,7 +502,7 @@ func (c *BookController) Delete() { c.JsonResult(0,"ok") } -//发布项目 +//发布项目. func (c *BookController) Release() { c.Prepare() @@ -527,6 +528,7 @@ func (c *BookController) Release() { c.JsonResult(0,"发布任务已推送到任务队列,稍后将在后台执行。") } +//文档排序. func (c *BookController) SaveSort() { c.Prepare() diff --git a/controllers/document.go b/controllers/document.go index 09f15b1b..b78f7571 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -17,11 +17,12 @@ import ( "github.com/astaxie/beego/orm" ) +//DocumentController struct. type DocumentController struct { BaseController } -//判断用户是否可以阅读文档 +//判断用户是否可以阅读文档. func isReadable (identify,token string,c *DocumentController) *models.BookResult { book, err := models.NewBook().FindByFieldFirst("identify", identify) @@ -81,7 +82,7 @@ func isReadable (identify,token string,c *DocumentController) *models.BookResult return bookResult } - +//文档首页. func (c *DocumentController) Index() { c.Prepare() identify := c.Ctx.Input.Param(":key") @@ -108,7 +109,7 @@ func (c *DocumentController) Index() { c.Data["Title"] = "概要" c.Data["Content"] = bookResult.Description } - +//阅读文档. func (c *DocumentController) Read() { c.Prepare() identify := c.Ctx.Input.Param(":key") @@ -167,6 +168,7 @@ func (c *DocumentController) Read() { c.Data["Content"] = template.HTML(doc.Release) } +//编辑文档. func (c *DocumentController) Edit() { c.Prepare() @@ -273,7 +275,7 @@ func (c *DocumentController) Create() { beego.Error("InsertOrUpdate => ",err) c.JsonResult(6005,"保存失败") }else{ - beego.Info("",document) + c.JsonResult(0,"ok",document) } } @@ -446,6 +448,7 @@ func (c *DocumentController) DownloadAttachment() { c.StopRun() } +//删除文档. func (c *DocumentController) Delete() { c.Prepare() @@ -476,10 +479,12 @@ func (c *DocumentController) Delete() { if err != nil { c.JsonResult(6005,"删除失败") } - + //重置文档数量统计 + models.NewBook().ResetDocumentNumber(doc.BookId) c.JsonResult(0,"ok") } +//获取文档内容. func (c *DocumentController) Content() { c.Prepare() diff --git a/models/book.go b/models/book.go index 734bea92..2c3b6440 100644 --- a/models/book.go +++ b/models/book.go @@ -7,6 +7,7 @@ import ( "github.com/lifei6671/godoc/conf" "github.com/astaxie/beego/logs" "strings" + "github.com/astaxie/beego" ) // Book struct . @@ -325,7 +326,17 @@ func (book *Book) ToBookResult() *BookResult { return m } - +//重置文档数量 +func (m *Book) ResetDocumentNumber(book_id int) { + o := orm.NewOrm() + + totalCount,err := o.QueryTable(NewDocument().TableNameWithPrefix()).Filter("book_id",book_id).Count() + if err == nil { + o.Raw("UPDATE md_books SET doc_count = ? WHERE book_id = ?",int(totalCount),book_id).Exec() + }else{ + beego.Error(err) + } +} diff --git a/models/document.go b/models/document.go index c276f993..fae98513 100644 --- a/models/document.go +++ b/models/document.go @@ -65,6 +65,7 @@ func (m *Document) Find(id int) (*Document,error) { return m,nil } +//插入和更新文档. func (m *Document) InsertOrUpdate(cols... string) error { o := orm.NewOrm() @@ -73,10 +74,12 @@ func (m *Document) InsertOrUpdate(cols... string) error { return err }else{ _,err := o.Insert(m) + NewBook().ResetDocumentNumber(m.BookId) return err } } +//根据指定字段查询一条文档. func (m *Document) FindByFieldFirst(field string,v interface{}) (*Document,error) { o := orm.NewOrm() @@ -112,6 +115,7 @@ func (m *Document) RecursiveDocument(doc_id int) error { return nil } +//发布文档 func (m *Document) ReleaseContent(book_id int) { o := orm.NewOrm() diff --git a/models/document_tree.go b/models/document_tree.go index 9fe3fd14..c3ef8e1a 100644 --- a/models/document_tree.go +++ b/models/document_tree.go @@ -96,7 +96,7 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte for _,item := range array { pid := 0 - if p,ok := item.ParentId.(int);ok { + if p, ok := item.ParentId.(int); ok { pid = p } if pid == parent_id { @@ -126,11 +126,11 @@ func getDocumentTree(array []*DocumentTree,parent_id int,selected_id int,selecte buf.WriteString("\"") buf.WriteString(selected_li) buf.WriteString(">") buf.WriteString(template.HTMLEscapeString(item.DocumentName) + "") - for _,sub := range array { - if p,ok := sub.ParentId.(int);ok && p == item.DocumentId{ - getDocumentTree(array,p,selected_id,selected_parent_id,buf) + for _, sub := range array { + if p, ok := sub.ParentId.(int); ok && p == item.DocumentId { + getDocumentTree(array, p, selected_id, selected_parent_id, buf) + break } } buf.WriteString("")