/
albumsComments.go
72 lines (63 loc) · 1.53 KB
/
albumsComments.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
package albums
import (
"fmt"
"opms/models"
"opms/utils"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
)
type AlbumsComment struct {
Id int64 `orm:"pk;column(comtid);"`
Userid int64
Albumid int64
Content string
Created int64
Status int
}
func (this *AlbumsComment) TableName() string {
return models.TableName("albums_comment")
}
func init() {
orm.RegisterModel(new(AlbumsComment))
}
func AddAlbumComment(upd AlbumsComment) error {
o := orm.NewOrm()
comment := new(AlbumsComment)
comment.Id = upd.Id
comment.Userid = upd.Userid
comment.Albumid = upd.Albumid
comment.Content = upd.Content
comment.Status = 1
comment.Created = time.Now().Unix()
_, err := o.Insert(comment)
if err == nil {
ChangeAlbumRelationNum(upd.Albumid, "comment")
}
return err
}
func ListAlbumComment(albumid int64, page, offset int) (ops []AlbumsComment) {
if page < 1 {
page = 1
}
if offset < 1 {
offset = 100
}
start := (page - 1) * offset
var comments []AlbumsComment
var err error
err = utils.GetCache("ListAlbumComment.id."+fmt.Sprintf("%d", albumid), &comments)
if err != nil {
cache_expire, _ := beego.AppConfig.Int("cache_expire")
o := orm.NewOrm()
o.Using("default")
qs := o.QueryTable(models.TableName("albums_comment"))
cond := orm.NewCondition()
cond = cond.And("albumid", albumid)
cond = cond.And("status", 1)
qs = qs.SetCond(cond)
qs.Limit(offset, start).All(&comments)
utils.SetCache("ListAlbumComment.id."+fmt.Sprintf("%d", albumid), comments, cache_expire)
}
return comments
}