添加评论
(uid, bookId, pid, docId int, content string)
| 264 | |
| 265 | //添加评论 |
| 266 | func (this *Comments) AddComments(uid, bookId, pid, docId int, content string) (err error) { |
| 267 | var comment Comments |
| 268 | |
| 269 | //查询该用户现有的评论 |
| 270 | second := beego.AppConfig.DefaultInt("CommentInterval", 60) |
| 271 | now := time.Now() |
| 272 | o := orm.NewOrm() |
| 273 | o.QueryTable("md_comments").Filter("uid", uid).Filter("TimeCreate__gt", now.Add(-time.Duration(second)*time.Second)).OrderBy("-Id").One(&comment, "Id") |
| 274 | if comment.Id > 0 { |
| 275 | return fmt.Errorf("您距离上次发表评论时间小于 %v 秒,请歇会儿再发。", second) |
| 276 | } |
| 277 | |
| 278 | var comments = Comments{ |
| 279 | Uid: uid, |
| 280 | BookId: bookId, |
| 281 | Content: content, |
| 282 | TimeCreate: now, |
| 283 | DocId: docId, |
| 284 | Pid: pid, |
| 285 | } |
| 286 | |
| 287 | if _, err = o.Insert(&comments); err != nil { |
| 288 | beego.Error(err.Error()) |
| 289 | err = errors.New("发表评论失败") |
| 290 | return |
| 291 | } |
| 292 | if docId == 0 { |
| 293 | // 书籍被评论数量量+1 |
| 294 | SetIncreAndDecre("md_books", "cnt_comment", fmt.Sprintf("book_id=%v", bookId), true) |
| 295 | } |
| 296 | return |
| 297 | } |
no test coverage detected