本章节主要学习下beego 文章评论数及最后评论时间展示、分类对应文章数,以及文章添加标签处理。
1、文章评论数及最后评论时间展示:
a、修改models/models.go的AddReply方法,底部增加:
topic := &Topic{Id: tidNum}
if o.Read(topic) == nil {
topic.ReplyTime = time.Now()
topic.ReplyCount++
_, err = o.Update(topic)
}
return err
b、修改models/models.go的DeleteReply方法,底部增加:
replies := make([]*Comment, 0)
qs := o.QueryTable("comment")
_, err = qs.Filter("tid", tidNum).OrderBy("-created").All(&replies)
if err != nil {
return err
}
topic := &Topic{Id: tidNum}
if o.Read(topic) == nil {
topic.ReplyTime = replies[0].Created
topic.ReplyCount = int64(len(replies))
_, err = o.Update(topic)
}
return err
c、模板修改,就不细说。
2、分类对应文章数修改:
a、修改models/models.go的AddTopic方法,底部增加:
cate := new(Category)
qs := o.QueryTable("category")
err = qs.Filter("title", category).One(cate)
if err == nil {
// 如果不存在我们就直接忽略,只当分类存在时进行更新
cate.TopicCount++
_, err = o.Update(cate)
}
return err
b、修改models/models.go的ModifyTopic方法,底部增加:
if len(oldCate) > 0 {
cate := new(Category)
qs := o.QueryTable("category")
err = qs.Filter("title", oldCate).One(cate)
if err == nil {
cate.TopicCount--
_, err = o.Update(cate)
}
}
cate := new(Category)
qs := o.QueryTable("category")
err = qs.Filter("title", category).One(cate)
if err == nil {
cate.TopicCount++
_, err = o.Update(cate)
}
return nil
c、模板修改,不细说。
3、文章添加标签
a、controllers下topic.go修改,加多lable参数传递,分别是models.AddTopic和models.ModifyTopic方法。
b、models下的models.go修改:
AddTopic方法:
lable = “$” + strings.Join(strings.Split(lable, ” “), “#$”) + “#”
GetTopic方法:
topic.Lables = strings.Replace(strings.Replace(
topic.Lables, “#”, ” “, -1), “$”, “”, -1)
ModifyTopic方法:
lable = “$” + strings.Join(strings.Split(lable, ” “), “#$”) + “#”
GetAllTopics方法:
if len(lable) > 0 {
qs = qs.Filter(“lables__contains”, “$”+lable+”#”)
}
github:https://github.com/yangsir/beego_study
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/98495.html