码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

186 rindas
4.3 KiB

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