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

209 lines
4.9 KiB

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