// 创建文章 func (this *ArticleController) Create() { //显示创建的页面 if this.Ctx.Input.Method() == "GET" { var err error this.Data["Title"] = "管理后台 - 创建文章" this.Data["Categories"], err = models.GetAllCategories() if err != nil { beego.Error(err) } this.Layout = "manager/layout.html" this.TplNames = "manager/article_create.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "manager/article_create_heade.html" return } // 处理 post 请求 title := this.GetString("title") category := this.GetString("category") content := this.GetString("content") if title == "" || category == "" || content == "" { this.Data["json"] = map[string]string{"code": "error", "info": "必填选项不能为空!"} this.ServeJson() return } err := models.AddArticle(title, category, content) if err != nil { this.Data["json"] = map[string]string{"code": "error", "info": err.Error()} } else { this.Data["json"] = map[string]string{"code": "success", "info": "文章添加成功!"} } this.ServeJson() return }
//博客首页 func (this *IndexController) Get() { //先处理分页 pageSize, err := beego.AppConfig.Int("pagesize") if err != nil { beego.Error(err) } count, err := models.GetArticleCount([]int{1}) if err != nil { beego.Error(err) } paginator := pagination.NewPaginator(this.Ctx.Request, pageSize, count) this.Data["paginator"] = paginator this.Data["Articles"], err = models.GetArticles(paginator.Offset(), pageSize, []int{1}) if err != nil { beego.Error(err) } this.Data["Categories"], err = models.GetAllCategories() if err != nil { beego.Error(err) } this.Layout = "index/layout.html" this.TplNames = "index/index.html" return }
// 修改文章 func (this *ArticleController) Edit() { // 显示修改文章的页面 if this.Ctx.Input.Method() == "GET" { id := this.Ctx.Input.Param(":id") intid, err := strconv.ParseInt(id, 10, 64) if err != nil { this.Ctx.WriteString(err.Error()) return } this.Data["Categories"], err = models.GetAllCategories() if err != nil { beego.Error(err) } this.Data["Article"], err = models.GetArticle(intid) if err != nil { this.Abort("404") } this.Data["Title"] = "管理后台 - 修改文章" this.Layout = "manager/layout.html" this.TplNames = "manager/article_edit.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "manager/article_edit_heade.html" return } // 处理修改文章请求 id, err := this.GetInt64("id") if err != nil { this.Data["json"] = map[string]string{"code": "error", "info": err.Error()} this.ServeJson() return } title := this.GetString("title") category := this.GetString("category") content := this.GetString("content") if title == "" || category == "" || content == "" { this.Data["json"] = map[string]string{"code": "error", "info": "必填选项不能为空!"} this.ServeJson() return } err = models.EditArticle(id, title, category, content) if err != nil { this.Data["json"] = map[string]string{"code": "error", "info": err.Error()} } else { this.Data["json"] = map[string]string{"code": "success", "info": "文章修改完成!"} } this.ServeJson() return }
//查看一篇文章 func (this *IndexController) Posts() { id := this.Ctx.Input.Param(":id") intid, err := strconv.ParseInt(id, 10, 64) if err != nil { this.Abort("404") } this.Data["Article"], err = models.GetArticle(intid) if err != nil { this.Abort("404") } this.Data["Categories"], err = models.GetAllCategories() if err != nil { this.Abort("404") } this.Layout = "index/layout.html" this.TplNames = "index/view.html" this.LayoutSections = make(map[string]string) this.LayoutSections["HtmlHead"] = "index/view_heade.html" return }