码农笔录博客源码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

198 рядки
4.4 KiB

6 роки тому
package controllers
6 роки тому
import (
"github.com/astaxie/beego"
"strconv"
"beeblog/models"
"beeblog/service"
)
6 роки тому
type NoteController struct {
beego.Controller
}
6 роки тому
func (this *NoteController) Save() {
6 роки тому
pid, _ := this.GetInt64("pid")
6 роки тому
title := this.GetString("title")
6 роки тому
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "保存失败")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
note := &models.Note{Title: title, Pid: pid, UserId: uid.(int64)}
6 роки тому
err := service.SaveNote(note)
if err == nil {
6 роки тому
this.Data["json"] = note
6 роки тому
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
6 роки тому
}
this.ServeJSON()
6 роки тому
return
6 роки тому
}
func (this *NoteController) Edit() {
idStr := this.Ctx.Input.Param(":id")
noteHtml := this.GetString("noteHtml")
id, _ := strconv.ParseInt(idStr, 10, 64)
6 роки тому
uid := this.GetSession("userid")
if uid == nil {
6 роки тому
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
note := &models.Note{Id: id}
6 роки тому
err1 := service.GetNote(note)
if err1 != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
if uid != note.UserId {
this.Data["json"] = models.ReurnError(403, "")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
note.NoteHtml = noteHtml
6 роки тому
err := service.EditNote(note)
6 роки тому
if err == nil {
6 роки тому
this.Data["json"] = models.ReurnSuccess("")
6 роки тому
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
6 роки тому
}
this.ServeJSON()
6 роки тому
return
6 роки тому
}
func (this *NoteController) SaveNoteColl() {
title := this.GetString("title")
6 роки тому
uid := this.GetSession("userid")
if uid == nil {
6 роки тому
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
note := &models.NoteColl{Title: title, UserId: uid.(int64)}
6 роки тому
err := service.SaveNoteColl(note)
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
6 роки тому
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
}
this.ServeJSON()
return
}
func (this *NoteController) EditNoteColl() {
title := this.GetString("title")
id, _ := this.GetInt64("id")
uid := this.GetSession("userid")
if uid == nil {
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
return
}
err := service.EditNoteColl(title, id, uid.(int64))
if err == nil {
this.Data["json"] = models.ReurnSuccess("")
} else {
this.Data["json"] = models.ReurnError(500, "保存失败")
6 роки тому
}
this.ServeJSON()
6 роки тому
return
6 роки тому
}
func (this *NoteController) Get() {
6 роки тому
uid := this.GetSession("userid")
if uid == nil {
6 роки тому
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
6 роки тому
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
err := service.GetNote(note)
if err == nil {
this.Data["json"] = note
}
6 роки тому
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
this.ServeJSON()
6 роки тому
return
6 роки тому
}
func (this *NoteController) DelNoteColl() {
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)
err := service.DelNoteColl(id, uid.(int64))
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
} else {
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
return
}
6 роки тому
func (this *NoteController) Delete() {
uid := this.GetSession("userid")
if uid == nil {
6 роки тому
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
idStr := this.Ctx.Input.Param(":id")
id, _ := strconv.ParseInt(idStr, 10, 64)
note := &models.Note{Id: id}
err := service.GetNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
if note.UserId != uid.(int64) {
this.Data["json"] = models.ReurnError(403, "")
this.ServeJSON()
6 роки тому
return
6 роки тому
}
err = service.DelNote(note)
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
this.Data["json"] = models.ReurnSuccess("")
6 роки тому
this.ServeJSON()
6 роки тому
return
6 роки тому
}
6 роки тому
func (this *NoteController) Note() {
uid := this.GetSession("userid")
if uid == nil {
6 роки тому
this.Redirect("/login", 302)
6 роки тому
}
noteColls, err := service.GetNoteColl(uid.(int64))
6 роки тому
if err == nil {
6 роки тому
if len(noteColls) > 0 {
for i := 0; i < len(noteColls); i++ {
notes, err1 := service.GetNoteByPid(noteColls[i].Id)
6 роки тому
if err1 == nil {
6 роки тому
noteColls[i].Notes = notes
}
}
}
} else {
noteColls = make([]*models.NoteColl, 0)
}
this.Data["NoteColls"] = noteColls
this.TplName = "note.html"
}