package controllers import ( "github.com/astaxie/beego" "myapp/models" ) type TopicController struct { beego.Controller } func (this *TopicController) Get() { this.Data["isActive"] = "topic" this.TplName = "topic.html" this.Data["IsLogin"] = checkAccount(this.Ctx) topics, err := models.GetAllTopics(false) if err != nil { beego.Error(err) } this.Data["Topics"] = topics } func (this *TopicController) Post() { if !checkAccount(this.Ctx) { this.Redirect("/login", 302) return } // 解析表单 tid := this.Input().Get("tid") title := this.Input().Get("title") content := this.Input().Get("content") var err error if len(tid) == 0 { err = models.AddTopic(title, content) } else { err = models.ModifyTopic(tid, title, content) } if err != nil { beego.Error(err) } this.Redirect("/topic", 302) } func (this *TopicController) Add() { if !checkAccount(this.Ctx) { this.Redirect("/login", 302) return } this.Data["isActive"] = "" this.TplName = "topic_add.html" } func (this *TopicController) Delete() { if !checkAccount(this.Ctx) { this.Redirect("/login", 302) return } err := models.DeleteTopic(this.Input().Get("tid")) if err != nil { beego.Error(err) } this.Redirect("/topic", 302) } func (this *TopicController) Modify() { this.TplName = "topic_modify.html" this.Data["isActive"] = "" tid := this.Input().Get("tid") topic, err := models.GetTopic(tid) if err != nil { beego.Error(err) this.Redirect("/", 302) return } this.Data["Topic"] = topic this.Data["Tid"] = tid } func (this *TopicController) View() { this.TplName = "topic_view.html" this.Data["isActive"] = "" topic, err := models.GetTopic(this.Ctx.Input.Params()["0"]) if err != nil { beego.Error(err) this.Redirect("/", 302) return } this.Data["Topic"] = topic }models文件夹下models.go添加:
func AddTopic(title, content string) error { o := orm.NewOrm() topic := &Topic{ Title: title, Content: content, Created: time.Now(), Updated: time.Now(), ReplyTime: time.Now(), } _, err := o.Insert(topic) return err } func GetTopic(tid string) (*Topic, error) { tidNum, err := strconv.ParseInt(tid, 10, 64) if err != nil { return nil, err } o := orm.NewOrm() topic := new(Topic) qs := o.QueryTable("topic") err = qs.Filter("id", tidNum).One(topic) if err != nil { return nil, err } topic.Views++ _, err = o.Update(topic) return topic, nil } func ModifyTopic(tid, title, content string) error { tidNum, err := strconv.ParseInt(tid, 10, 64) if err != nil { return err } o := orm.NewOrm() topic := &Topic{Id: tidNum} if o.Read(topic) == nil { topic.Title = title topic.Content = content topic.Updated = time.Now() o.Update(topic) } return nil } func DeleteTopic(tid string) error { tidNum, err := strconv.ParseInt(tid, 10, 64) if err != nil { return err } o := orm.NewOrm() topic := &Topic{Id: tidNum} _, err = o.Delete(topic) return err } func GetAllTopics(isDesc bool) (topics []*Topic, err error) { o := orm.NewOrm() topics = make([]*Topic, 0) qs := o.QueryTable("topic") if isDesc { _, err = qs.OrderBy("-created").All(&topics) } else { _, err = qs.All(&topics) } return topics, err }其他文件修改:
beego.Router("/topic", &controllers.TopicController{}) beego.AutoRouter(&controllers.TopicController{})views文件夹下添加topic的各种模板
已有1条评论