码农笔录博客源码
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

186 righe
4.3 KiB

6 anni fa
package mcontrollers
import (
"beeblog/models"
"beeblog/service"
beego "github.com/beego/beego/v2/server/web"
6 anni fa
"strconv"
"time"
)
type MBlogController struct {
beego.Controller
}
6 anni fa
func (this *MBlogController) Get() {
6 anni fa
blogService := service.BlogService{}
userService := service.UserService{}
6 anni fa
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 anni fa
blog, err := blogService.GetBlog(id)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnData("", blog)
6 anni fa
this.ServeJSON()
6 anni fa
userService.CountBrows(blog.UserId)
blogService.EditBlogBrows(id)
6 anni fa
return
}
6 anni fa
func (this *MBlogController) BlogsPage() {
6 anni fa
blogService := service.BlogService{}
6 anni fa
num, _ := this.GetInt("num")
size, _ := this.GetInt("size")
cat, _ := this.GetInt64("cat")
flag, _ := this.GetInt("flag")
if num <= 0 {
num = 1
}
if size < 15 {
size = 15
}
if cat <= 0 {
cat = -1
}
6 anni fa
pages, err := blogService.FindBlogs(num, size, cat, flag)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnData("", pages)
6 anni fa
this.ServeJSON()
return
}
func (this *MBlogController) EditPage() {
6 anni fa
blogService := service.BlogService{}
6 anni fa
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 anni fa
blog, err := blogService.GetBlog(id)
6 anni fa
if err != nil {
this.Redirect("/404", 302)
return
}
if blog.UserId != uid.(int64) {
this.Redirect("/403", 302)
return
}
this.Data["Blog"] = blog
this.TplName = "editblog.html"
}
func (this *MBlogController) Save() {
6 anni fa
blogService := service.BlogService{}
userService := service.UserService{}
6 anni fa
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)}
6 anni fa
err := blogService.SaveBlog(blog, labels)
6 anni fa
if err == nil {
this.Data["json"] = models.ReurnData("", blog.Id)
6 anni fa
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 anni fa
userService.CountBlog(uid.(int64))
6 anni fa
return
}
func (this *MBlogController) Edit() {
6 anni fa
userService := service.UserService{}
blogService := service.BlogService{}
6 anni fa
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
id, _ := this.GetInt64("id")
6 anni fa
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
blog, err := blogService.GetBlog(id)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
this.ServeJSON()
return
}
blog.Title = title
blog.BlogHtml = blogHtml
blog.CategoryId = catoryId
blog.Utime = time.Now()
6 anni fa
err = blogService.EditBlog(blog, labels)
6 anni fa
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 anni fa
userService.CountBlog(uid.(int64))
6 anni fa
return
}
func (this *MBlogController) Del() {
6 anni fa
blogService := service.BlogService{}
userService := service.UserService{}
6 anni fa
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 anni fa
blog, err := blogService.GetBlog(id)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
if blog.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(503, "")
this.ServeJSON()
return
}
blog.Delflag = 1
6 anni fa
err = blogService.DelBlog(blog)
6 anni fa
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
6 anni fa
userService.CountBlog(uid.(int64))
6 anni fa
return
6 anni fa
}
func (this *MBlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
this.TplName = "newblog.html"
}