Kaynağa Gözat

like & unlike

layui
yirenyishi 6 yıl önce
ebeveyn
işleme
03b4ddbafa
  1. 12
      controllers/BlogController.go
  2. 52
      controllers/LikeController.go
  3. BIN
      data/beeblog.db
  4. 2
      models/DataInit.go
  5. 4
      models/Like.go
  6. 11
      routers/LikeRouter.go
  7. 2
      service/BlogService.go
  8. 29
      service/LikeService.go
  9. 29
      service/UserService.go
  10. BIN
      static/img/liked.png
  11. 36
      views/blog.html

12
controllers/BlogController.go

@ -44,6 +44,15 @@ func (this *BlogController) Get() {
this.Redirect("/500", 302)
return
}
uid := this.GetSession("userid")
if uid != nil {
if blog.UserId == uid.(int64) {
this.Data["IsAuthor"] = true
}
if flag, err := service.IsLike(id, uid.(int64)); err == nil {
this.Data["IsLike"] = flag
}
}
this.Data["Blog"] = blog
this.Data["NickName"] = this.GetSession("nickname")
this.Data["IsLogin"] = this.GetSession("nickname") != nil
@ -75,7 +84,7 @@ func (this *BlogController) Del() {
}
blog.Delflag = 1
err = service.DelBlog(blog)
if err!=nil {
if err != nil {
this.Data["json"] = models.ReurnError(500, "")
this.ServeJSON()
return
@ -132,5 +141,4 @@ func (this *BlogController) BlogsPage() {
this.Data["Flag"] = flag
this.Data["IsBlog"] = true
this.TplName = "blogs.html"
}

52
controllers/LikeController.go

@ -0,0 +1,52 @@
package controllers
import (
"strconv"
"beeblog/service"
"github.com/astaxie/beego"
"beeblog/models"
)
type LikeController struct {
beego.Controller
}
func (this *LikeController) Save() {
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := service.SaveLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
}else{
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
service.CountLike(uid.(int64))
return
}
func (this *LikeController) Delete() {
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)
like := &models.Like{BlogId: id, UserId: uid.(int64)}
if _, err := service.DelLike(like); err != nil {
this.Data["json"] = models.ReurnError(500, "保存失败")
}else{
this.Data["json"] = models.ReurnSuccess("")
}
this.ServeJSON()
service.CountLike(uid.(int64))
return
}

BIN
data/beeblog.db

İkili dosya gösterilmiyor.

2
models/DataInit.go

@ -19,7 +19,7 @@ func RegistDB() {
os.Create(_DB_NAME)
}
//orm.RegisterModel(new(Attachment),new(Topic))
orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category))
orm.RegisterModel(new(Attachment),new(User),new(Blog),new(NLabel),new(Note),new(NoteColl),new(Category),new(Like))
orm.RegisterDriver(_SQLITE3_DRIVER,orm.DRSqlite)
orm.RegisterDataBase("default",_SQLITE3_DRIVER,_DB_NAME,10)
}

4
models/Like.go

@ -6,5 +6,7 @@ type Like struct {
Id int64
UserId int64
BlogId int64
Ltime time.Time `orm:"datetime"`
Ltime time.Time `orm:"auto_now_add;type(datetime)"`
Note Note `orm:"-"`
}

11
routers/LikeRouter.go

@ -0,0 +1,11 @@
package routers
import (
"beeblog/controllers"
"github.com/astaxie/beego"
)
func init() {
beego.Router("/like/:id([0-9]+)", &controllers.LikeController{}, "get:Save")
beego.Router("/unlike/:id([0-9]+)", &controllers.LikeController{}, "get:Delete")
}

2
service/BlogService.go

@ -6,8 +6,6 @@ import (
"beeblog/utils"
)
type BlogService struct {
}
func count(num int, size int, cat int64) (*utils.Page, error) {
o := orm.NewOrm()

29
service/LikeService.go

@ -0,0 +1,29 @@
package service
import (
"beeblog/models"
"github.com/astaxie/beego/orm"
"fmt"
)
func SaveLike(like *models.Like) (int64, error) {
return orm.NewOrm().Insert(like)
}
func DelLike(like *models.Like) (int64, error) {
return orm.NewOrm().QueryTable(models.Like{}).Filter("BlogId",like.BlogId).Filter("UserId", like.UserId).Delete()
}
func IsLike(bid int64, uid int64) (bool, error) {
totalCount, err := orm.NewOrm().QueryTable(&models.Like{}).Filter("BlogId", bid).Filter("UserId", uid).Count()
if err == nil {
fmt.Println(totalCount,"like count")
if totalCount > 0 {
return true, nil
} else {
return false, nil
}
}else{
fmt.Println(err)
return false,err
}
}

29
service/UserService.go

@ -8,11 +8,11 @@ import (
type UserService struct {
}
func GetUser(id int64) (*models.User,error) {
func GetUser(id int64) (*models.User, error) {
o := orm.NewOrm()
user := &models.User{Id:id}
user := &models.User{Id: id}
err := o.Read(user)
return user,err
return user, err
}
func FindByUserName(username string) (*models.User, error) {
@ -43,29 +43,22 @@ func SaveUser(user *models.User) error {
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")
browses := 0
o.Raw("UPDATE `user` SET `blog_count` = (SELECT count(id) FROM blog where delflag = 0 and user_id =?1 ) WHERE `id` = ?2 ", uid, uid).QueryRow(&browses)
return
}
func CountBrows(uid int64){
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) //获取总条数
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) {
o := orm.NewOrm()
browses := 0
o.Raw("UPDATE `user` SET `blog_like` = (select count(id) from like where user_id = ?1) WHERE `id` = ?2 ", uid, uid).QueryRow(&browses)
return
}

BIN
static/img/liked.png

İkili dosya gösterilmiyor.

Sonra

Genişlik:  |  Yükseklik:  |  Boyut: 600 B

36
views/blog.html

@ -36,6 +36,11 @@
<div class="blog-root">
<h1 style="text-align: center">{{.Blog.Title}}</h1>
<div class="blog-info">
{{if .IsLike}}
<span><img src="/static/img/liked.png" alt="" onclick="unlike({{.Blog.Id}})"></span>
{{else}}
<span><img src="/static/img/like.png" alt="" onclick="like({{.Blog.Id}})"></span>
{{end}}
<span><a href="">{{.Blog.User.NickName}}</a></span>
<span><em>时间:</em>{{.Blog.Ctime.Format "2006-01-02"}}</span>
<span><em>阅读数:</em>{{.Blog.Browses}}</span>
@ -53,4 +58,35 @@
</div>
</div>
</body>
<script>
function like(id) {
$.get('/like/' + id,
function (data) {
if (data.Status == 0) {
window.location.href = window.location.href
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}
function unlike(id) {
$.get('/unlike/' + id,
function (data) {
if (data.Status == 0) {
window.location.href = window.location.href
} else if (data.Status == 401) {
window.location.href = "/login"
} else if (data.Status == 403) {
layer.msg("暂无权限", {icon: 5});
} else {
layer.msg("服务器异常", {icon: 5});
}
}, 'json')
}
</script>
</html>
Yükleniyor…
İptal
Kaydet