添加评论内容 添加评分 score的值只能是1-5,然后需要对score x 10,50则表示5.0分
(uid, bookId, score int)
| 228 | //添加评分 |
| 229 | //score的值只能是1-5,然后需要对score x 10,50则表示5.0分 |
| 230 | func (this *Score) AddScore(uid, bookId, score int) (err error) { |
| 231 | //查询评分是否已存在 |
| 232 | o := orm.NewOrm() |
| 233 | var scoreObj = Score{Uid: uid, BookId: bookId} |
| 234 | o.Read(&scoreObj, "uid", "book_id") |
| 235 | if scoreObj.Id > 0 { //评分已存在 |
| 236 | err = errors.New("您已给当前文档打过分了") |
| 237 | return |
| 238 | } |
| 239 | |
| 240 | // 评分不存在,添加评分记录 |
| 241 | score = score * 10 |
| 242 | scoreObj.Score = score |
| 243 | scoreObj.TimeCreate = time.Now() |
| 244 | o.Insert(&scoreObj) |
| 245 | if scoreObj.Id > 0 { //评分添加成功,更行当前书籍书籍的评分 |
| 246 | //评分人数+1 |
| 247 | var book = Book{BookId: bookId} |
| 248 | o.Read(&book, "book_id") |
| 249 | if book.CntScore == 0 { |
| 250 | book.CntScore = 1 |
| 251 | book.Score = 0 |
| 252 | } else { |
| 253 | book.CntScore = book.CntScore + 1 |
| 254 | } |
| 255 | book.Score = (book.Score*(book.CntScore-1) + score) / book.CntScore |
| 256 | _, err = o.Update(&book, "cnt_score", "score") |
| 257 | if err != nil { |
| 258 | beego.Error(err.Error()) |
| 259 | err = errors.New("评分失败,内部错误") |
| 260 | } |
| 261 | } |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | //添加评论 |
| 266 | func (this *Comments) AddComments(uid, bookId, pid, docId int, content string) (err error) { |
no test coverage detected