diff --git a/controllers/BlogController.go b/controllers/BlogController.go index 3dc8521..6dd5a97 100644 --- a/controllers/BlogController.go +++ b/controllers/BlogController.go @@ -24,7 +24,7 @@ func (this *BlogController) Save() { catory := this.GetString("catory") catoryId, _ := strconv.ParseInt(catory, 10, 64) labels := this.GetStrings("labels[]") - blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: 1} + blog := &models.Blog{Title: title, BlogHtml: blogHtml, CategoryId: catoryId, UserId: uid.(int64)} err := service.SaveBlog(blog, labels) if err == nil { this.Data["json"] = models.ReurnSuccess("") @@ -32,6 +32,7 @@ func (this *BlogController) Save() { this.Data["json"] = models.ReurnError(500, "保存失败") } this.ServeJSON() + service.CountBlog(uid.(int64)) return } @@ -39,12 +40,17 @@ func (this *BlogController) Get() { idStr := this.Ctx.Input.Param(":id") id, _ := strconv.ParseInt(idStr, 10, 64) blog, err := service.GetBlog(id) - if err == nil { - this.Data["Blog"] = blog + if err != nil { + this.Redirect("/500.html",302) + return } + this.Data["Blog"] = blog this.Data["NickName"] = this.GetSession("nickname") this.Data["IsLogin"] = this.GetSession("nickname") != nil this.TplName = "blog.html" + service.CountBrows(blog.UserId) + service.EditBlogBrows(id) + return } func (this *BlogController) New() { diff --git a/controllers/PageController.go b/controllers/PageController.go index d26277a..9851316 100644 --- a/controllers/PageController.go +++ b/controllers/PageController.go @@ -1,6 +1,9 @@ package controllers -import "github.com/astaxie/beego" +import ( + "github.com/astaxie/beego" + "beeblog/service" +) type PageController struct { beego.Controller @@ -8,6 +11,12 @@ type PageController struct { // @router /iframe/blog [get] func (this *PageController) Blog() { + cats, err := service.GetCats() + if err != nil { + this.Redirect("500.html", 302) + return + } + this.Data["Cats"] = cats this.TplName = "iframe/blog.html" } diff --git a/data/beeblog.db b/data/beeblog.db index 8189a01..ad8228a 100644 Binary files a/data/beeblog.db and b/data/beeblog.db differ diff --git a/service/BlogService.go b/service/BlogService.go index 8054874..a080f3c 100644 --- a/service/BlogService.go +++ b/service/BlogService.go @@ -23,6 +23,16 @@ func count(num int, size int, cat int64) (*utils.Page, error) { return utils.PageUtil(totalCount, num, size), nil } +func EditBlogBrows(id int64) { + o := orm.NewOrm() + blog := &models.Blog{Id: id} + err := o.Read(blog) + if err == nil { + blog.Browses += 1 + o.Update(blog, "Browses") + } +} + func GetBlog(id int64) (*models.Blog, error) { o := orm.NewOrm() blog := &models.Blog{Id: id} diff --git a/service/UserService.go b/service/UserService.go index cb29119..8d5f49b 100644 --- a/service/UserService.go +++ b/service/UserService.go @@ -33,3 +33,32 @@ func SaveUser(user *models.User) error { } return nil } + +func CountBlog(uid int64) { + o := orm.NewOrm() + totalItem := 0 + err := o.Raw("SELECT count(*) FROM blog where delflag = 0 and user_id =? ", uid).QueryRow(&totalItem) //获取总条数 + if err != nil { + return + } + user := &models.User{Id: uid} + err = o.Read(user) + if err != nil { + return + } + user.BlogCount = totalItem + o.Update(user, "BlogCount") + return +} +func CountBrows(uid int64){ + o := orm.NewOrm() + browses := 0 + o.Raw("UPDATE `user` SET `blog_browes` = (select SUM(browses) browses from blog where user_id = ?1) WHERE `id` = ?2 ", uid,uid).QueryRow(&browses) //获取总条数 + return +} +func CountComments(uid int64) { + +} +func CountLike(uid int64) { + +} diff --git a/views/iframe/blog.html b/views/iframe/blog.html index e839b1c..d8d0fab 100644 --- a/views/iframe/blog.html +++ b/views/iframe/blog.html @@ -28,11 +28,9 @@ 开发语言: @@ -48,8 +46,8 @@
- - + +