-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathBookmarkController.go
78 lines (66 loc) · 1.91 KB
/
BookmarkController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package controllers
import (
"time"
"github.com/TruthHun/BookStack/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
type BookmarkController struct {
BaseController
}
func (this *BookmarkController) Prepare() {
this.BaseController.Prepare()
if this.Member.MemberId == 0 {
this.JsonResult(1, "请先登录")
}
}
//添加或者移除书签
func (this *BookmarkController) Bookmark() {
docId, _ := this.GetInt(":id")
if docId <= 0 {
this.JsonResult(1, "收藏失败,文档id参数错误")
}
insert, err := new(models.Bookmark).InsertOrDelete(this.Member.MemberId, docId)
if err != nil {
beego.Error(err.Error())
if insert {
this.JsonResult(1, "添加书签失败", insert)
}
this.JsonResult(1, "移除书签失败", insert)
}
if insert {
this.JsonResult(0, "添加书签成功", insert)
}
this.JsonResult(0, "移除书签成功", insert)
}
//获取书签列表
func (this *BookmarkController) List() {
bookId, _ := this.GetInt(":book_id")
if bookId <= 0 {
this.JsonResult(1, "获取书签列表失败:参数错误")
}
bl, rows, err := new(models.Bookmark).List(this.Member.MemberId, bookId)
if err != nil {
beego.Error(err.Error())
this.JsonResult(1, "获取书签列表失败")
}
var (
book = new(models.Book)
lists []map[string]interface{}
)
orm.NewOrm().QueryTable(book).Filter("book_id", bookId).One(book, "identify")
for _, item := range bl {
var list = make(map[string]interface{})
list["url"] = beego.URLFor("DocumentController.Read", ":key", book.Identify, ":id", item.Identify)
list["title"] = item.Title
list["doc_id"] = item.DocId
list["del"] = beego.URLFor("BookmarkController.Bookmark", ":id", item.DocId)
list["time"] = time.Unix(int64(item.CreateAt), 0).Format("01-02 15:04")
lists = append(lists, list)
}
this.JsonResult(0, "获取书签列表成功", map[string]interface{}{
"count": rows,
"book_id": bookId,
"list": lists,
})
}