码农笔录博客源码
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

237 rindas
5.6 KiB

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