码农笔录博客源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 rivejä
4.3 KiB

6 vuotta sitten
package mcontrollers
import (
"beeblog/models"
"beeblog/service"
3 vuotta sitten
beego "github.com/beego/beego/v2/server/web"
6 vuotta sitten
"strconv"
"time"
)
type MBlogController struct {
beego.Controller
}
6 vuotta sitten
func (this *MBlogController) Get() {
6 vuotta sitten
blogService := service.BlogService{}
userService := service.UserService{}
6 vuotta sitten
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 vuotta sitten
blog, err := blogService.GetBlog(id)
6 vuotta sitten
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
3 vuotta sitten
this.Data["json"] = models.ReurnData("", blog)
6 vuotta sitten
this.ServeJSON()
6 vuotta sitten
userService.CountBrows(blog.UserId)
blogService.EditBlogBrows(id)
6 vuotta sitten
return
}
6 vuotta sitten
func (this *MBlogController) BlogsPage() {
6 vuotta sitten
blogService := service.BlogService{}
6 vuotta sitten
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 vuotta sitten
pages, err := blogService.FindBlogs(num, size, cat, flag)
6 vuotta sitten
if err != nil {
this.Data["json"] = models.ReurnServerError(500)
this.ServeJSON()
return
}
3 vuotta sitten
this.Data["json"] = models.ReurnData("", pages)
6 vuotta sitten
this.ServeJSON()
return
}
func (this *MBlogController) EditPage() {
6 vuotta sitten
blogService := service.BlogService{}
6 vuotta sitten
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 vuotta sitten
blog, err := blogService.GetBlog(id)
6 vuotta sitten
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 vuotta sitten
blogService := service.BlogService{}
userService := service.UserService{}
6 vuotta sitten
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 vuotta sitten
err := blogService.SaveBlog(blog, labels)
6 vuotta sitten
if err == nil {
3 vuotta sitten
this.Data["json"] = models.ReurnData("", blog.Id)
6 vuotta sitten
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 vuotta sitten
userService.CountBlog(uid.(int64))
6 vuotta sitten
return
}
func (this *MBlogController) Edit() {
6 vuotta sitten
userService := service.UserService{}
blogService := service.BlogService{}
6 vuotta sitten
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
3 vuotta sitten
id, _ := this.GetInt64("id")
6 vuotta sitten
title := this.GetString("title")
blogHtml := this.GetString("blogHtml")
catory := this.GetString("catory")
catoryId, _ := strconv.ParseInt(catory, 10, 64)
labels := this.GetStrings("labels[]")
3 vuotta sitten
blog, err := blogService.GetBlog(id)
6 vuotta sitten
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 vuotta sitten
err = blogService.EditBlog(blog, labels)
6 vuotta sitten
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
6 vuotta sitten
userService.CountBlog(uid.(int64))
6 vuotta sitten
return
}
func (this *MBlogController) Del() {
6 vuotta sitten
blogService := service.BlogService{}
userService := service.UserService{}
6 vuotta sitten
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 vuotta sitten
blog, err := blogService.GetBlog(id)
6 vuotta sitten
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 vuotta sitten
err = blogService.DelBlog(blog)
6 vuotta sitten
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
}
this.Data["json"] = models.ReurnSuccess("")
this.ServeJSON()
6 vuotta sitten
userService.CountBlog(uid.(int64))
6 vuotta sitten
return
6 vuotta sitten
}
func (this *MBlogController) New() {
uid := this.GetSession("userid")
if uid == nil {
this.Redirect("/login", 302)
return
}
this.TplName = "newblog.html"
3 vuotta sitten
}