yirenyishi
vor 6 Jahren
14 geänderte Dateien mit 327 neuen und 19 gelöschten Zeilen
@ -0,0 +1,83 @@ |
|||
package controllers |
|||
|
|||
import ( |
|||
"github.com/astaxie/beego" |
|||
"beeblog/models" |
|||
"beeblog/service" |
|||
"strconv" |
|||
) |
|||
|
|||
type CommentController struct { |
|||
beego.Controller |
|||
} |
|||
|
|||
func (this *CommentController) Save() { |
|||
uid := this.GetSession("userid") |
|||
if uid == nil { |
|||
this.Data["json"] = models.ReurnError(401, "") |
|||
this.ServeJSON() |
|||
return |
|||
} |
|||
blogId, berr := this.GetInt64("blog") |
|||
if blogId == 0 || berr != nil { |
|||
this.Data["json"] = models.ReurnError(403, "") |
|||
this.ServeJSON() |
|||
return |
|||
} |
|||
commVal := this.GetString("commval") |
|||
blog, err := service.ReadBlog(blogId) |
|||
if err != nil { |
|||
this.Data["json"] = models.ReurnError(403, "") |
|||
this.ServeJSON() |
|||
return |
|||
} |
|||
comm := &models.Comment{BlogId: blogId, CuserId: uid.(int64), BuserId: blog.UserId, ComVal: commVal} |
|||
if pid, _ := this.GetInt64("pid"); pid != 0 { |
|||
parent := &models.Comment{Id: pid} |
|||
if err := service.ReadComment(parent); err == nil { |
|||
comm.BuserId = parent.CuserId |
|||
} |
|||
comm.Pid = pid |
|||
} |
|||
err = service.SaveComment(comm) |
|||
if err == nil { |
|||
this.Data["json"] = models.ReurnData("", comm) |
|||
} else { |
|||
this.Data["json"] = models.ReurnError(500, "保存失败") |
|||
} |
|||
this.ServeJSON() |
|||
service.CountComments(uid.(int64)) |
|||
return |
|||
} |
|||
|
|||
func (this *CommentController) Del() { |
|||
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) |
|||
comm := &models.Comment{Id: id} |
|||
err := service.ReadComment(comm) |
|||
if err != nil { |
|||
this.Data["json"] = models.ReurnError(500, "") |
|||
this.ServeJSON() |
|||
return |
|||
} |
|||
if comm.CuserId != uid.(int64) { |
|||
this.Data["json"] = models.ReurnError(403, "") |
|||
this.ServeJSON() |
|||
return |
|||
} |
|||
err = service.DelComment(id) |
|||
if err == nil { |
|||
this.Data["json"] = models.ReurnSuccess("") |
|||
} else { |
|||
this.Data["json"] = models.ReurnError(500, "保存失败") |
|||
} |
|||
this.ServeJSON() |
|||
service.CountComments(uid.(int64)) |
|||
return |
|||
} |
@ -0,0 +1,11 @@ |
|||
package routers |
|||
|
|||
import ( |
|||
"github.com/astaxie/beego" |
|||
"beeblog/controllers" |
|||
) |
|||
|
|||
func init() { |
|||
beego.Router("/comms/save", &controllers.CommentController{}, "post:Save") |
|||
beego.Router("/comms/del/:id([0-9]+)", &controllers.CommentController{}, "get:Del") |
|||
} |
@ -0,0 +1,80 @@ |
|||
package service |
|||
|
|||
import ( |
|||
"beeblog/models" |
|||
"github.com/astaxie/beego/orm" |
|||
"fmt" |
|||
) |
|||
|
|||
func FindCommentByBlog(bid int64) ([]*models.Comment, error) { |
|||
var comms []*models.Comment |
|||
o := orm.NewOrm() |
|||
_, err := o.QueryTable(&models.Comment{}).Filter("Pid", 0).OrderBy("-Ctime").All(&comms) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
if len(comms) > 0 { |
|||
for i := 0; i < len(comms); i++ { |
|||
var childs []*models.Comment |
|||
_, childerrr := o.QueryTable(&models.Comment{}).Filter("Pid", comms[i].Id).OrderBy("-Ctime").All(&childs) |
|||
if childerrr == nil { |
|||
if len(childs) > 0{ |
|||
comms[i].Childs = childs |
|||
for j:=0 ; j<len(childs);j++{ |
|||
cuser := &models.User{Id: childs[j].CuserId} |
|||
o.Read(cuser) |
|||
childs[j].CUser = cuser |
|||
buser := &models.User{Id: childs[j].BuserId} |
|||
o.Read(buser) |
|||
childs[j].BUser = buser |
|||
} |
|||
} |
|||
} |
|||
cuser := &models.User{Id: comms[i].CuserId} |
|||
o.Read(cuser) |
|||
comms[i].CUser = cuser |
|||
} |
|||
} |
|||
return comms, nil |
|||
} |
|||
|
|||
func SaveComment(comment *models.Comment) error { |
|||
o := orm.NewOrm() |
|||
id, err := o.Insert(comment) |
|||
if err == nil { |
|||
comment.Id = id |
|||
cuser := &models.User{Id: comment.CuserId} |
|||
o.Read(cuser) |
|||
comment.CUser = cuser |
|||
if comment.BuserId !=0 { |
|||
buser := &models.User{Id: comment.BuserId} |
|||
o.Read(buser) |
|||
comment.BUser = buser |
|||
} |
|||
return nil |
|||
} |
|||
return err |
|||
} |
|||
|
|||
func ReadComment(comment *models.Comment) error { |
|||
return orm.NewOrm().Read(comment) |
|||
} |
|||
|
|||
func DelComment(id int64) error { |
|||
comm := &models.Comment{Id: id} |
|||
o := orm.NewOrm() |
|||
err := o.Read(comm) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
if comm.Pid != 0 { |
|||
if _, err := o.QueryTable(models.Comment{}).Filter("Pid", id).Delete(); err != nil { |
|||
fmt.Println(err) |
|||
} |
|||
|
|||
} |
|||
if _, err := o.Delete(comm); err != nil { |
|||
return err |
|||
} |
|||
return nil |
|||
} |
Laden…
In neuem Issue referenzieren