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

103 rindas
2.3 KiB

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