码农笔录博客源码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

186 lignes
4.3 KiB

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