码农笔录博客源码
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 lines
4.3 KiB

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