码农笔录博客源码
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.

160 lines
3.5 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,"保存失败")
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 {
пре 6 година
this.Data["json"] = models.ReurnError(500,"保存失败")
}
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{
this.Data["json"] = models.ReurnError(401, "")
this.ServeJSON()
пре 6 година
return
пре 6 година
}
пре 6 година
note := &models.Note{Id:id}
err1 := service.GetNote(note)
if err1 != nil {
this.Data["json"] = models.ReurnError(500,"保存失败")
пре 6 година
this.ServeJSON()
пре 6 година
return
пре 6 година
}
if uid != note.UserId {
пре 6 година
this.Data["json"] = models.ReurnError(403,"")
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{
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 {
пре 6 година
this.Data["json"] = note
пре 6 година
} else {
пре 6 година
this.Data["json"] = models.ReurnError(500,"保存失败")
пре 6 година
}
this.ServeJSON()
пре 6 година
return
пре 6 година
}
func (this *NoteController) Get() {
пре 6 година
uid := this.GetSession("userid")
if uid == nil{
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) Delete() {
uid := this.GetSession("userid")
if uid == nil{
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,"")
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,"")
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"
}