码农笔录博客源码
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

237 righe
5.6 KiB

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