Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8391a1

Browse files
committed
喜欢功能开发完毕(未登录时还需要处理一下)
1 parent 04d3cc0 commit c8391a1

File tree

28 files changed

+531
-106
lines changed

28 files changed

+531
-106
lines changed

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,13 @@ studygolang
77
目前只开发了主要功能,还有不少功能在不断开发中。欢迎有兴趣的gopher们参与进来,一起构建一个完善的 Golang 中文社区,Go语言爱好者的学习家园。
88

99
#目前在正开发或需要开发的功能
10-
1. 热门节点
11-
2. 小贴士
12-
3. 喜欢
13-
4. 收藏
14-
5. 活跃用户(已完成)
15-
6. 关注
16-
7. 用第三方账号登录
17-
8. 绑定github后显示其代码
18-
9. 同步到微博?
19-
10. wiki(已完成,有些细节待完善)
20-
11. 资源
21-
12. 酷站
22-
13. 后台管理
10+
1. 小贴士
11+
2. 收藏
12+
3. 关注
13+
4. 用第三方账号登录
14+
5. 绑定github后显示其代码
15+
6. 同步到微博?
16+
7. 博文评论 @用户 待完善
2317

2418
# 本地搭建一个 Golang 社区 #
2519

websites/code/studygolang/src/controller/article.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919

2020
const limit = 20
2121

22-
// 在需要评论且要回调的地方注册评论对象
22+
// 在需要评论(喜欢)且要回调的地方注册评论(喜欢)对象
2323
func init() {
24-
// 注册评论对象
24+
// 注册评论(喜欢)对象
2525
service.RegisterCommentObject(model.TYPE_ARTICLE, service.ArticleComment{})
26+
service.RegisterLikeObject(model.TYPE_ARTICLE, service.ArticleLike{})
2627
}
2728

2829
// 网友文章列表页
@@ -80,9 +81,17 @@ func ArticlesHandler(rw http.ResponseWriter, req *http.Request) {
8081
"next_id": nextId,
8182
}
8283

84+
// 获取当前用户喜欢对象信息
85+
user, ok := filter.CurrentUser(req)
86+
var likeFlags map[int]int
87+
if ok {
88+
uid := user["uid"].(int)
89+
likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, articles[0].Id, nextId)
90+
}
91+
8392
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/list.html")
8493
// 设置模板数据
85-
filter.SetData(req, map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo})
94+
filter.SetData(req, map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo, "likeflags": likeFlags})
8695
}
8796

8897
// 文章详细页
@@ -99,10 +108,17 @@ func ArticleDetailHandler(rw http.ResponseWriter, req *http.Request) {
99108
util.Redirect(rw, req, "/articles")
100109
}
101110

111+
likeFlag := 0
112+
user, ok := filter.CurrentUser(req)
113+
if ok {
114+
uid := user["uid"].(int)
115+
likeFlag = service.HadLike(uid, article.Id, model.TYPE_ARTICLE)
116+
}
117+
102118
service.Views.Incr(req, model.TYPE_ARTICLE, article.Id)
103119

104120
// 设置内容模板
105121
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html")
106122
// 设置模板数据
107-
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1]})
123+
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag})
108124
}

websites/code/studygolang/src/controller/index.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
package controller
88

99
import (
10-
"filter"
1110
"math/rand"
1211
"net/http"
12+
13+
"filter"
14+
"model"
1315
"service"
1416
)
1517

@@ -24,6 +26,18 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
2426
// 获得最新博文
2527
// blogs := service.FindNewBlogs()
2628
recentArticles := service.FindArticles("0", "10")
29+
// 获取当前用户喜欢对象信息
30+
var likeFlags map[int]int
31+
32+
if len(recentArticles) > 0 {
33+
user, ok := filter.CurrentUser(req)
34+
if ok {
35+
uid := user["uid"].(int)
36+
37+
likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id)
38+
}
39+
}
40+
2741
// TODO:开源项目(暂时使用 resource 表)
2842
resources := service.FindResourcesByCatid("2")
2943

@@ -36,5 +50,5 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
3650
// 设置内容模板
3751
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html")
3852
// 设置模板数据
39-
filter.SetData(req, map[string]interface{}{"topics": newTopics, "articles": recentArticles, "resources": resources[start:end]})
53+
filter.SetData(req, map[string]interface{}{"topics": newTopics, "articles": recentArticles, "likeflags": likeFlags, "resources": resources[start:end]})
4054
}

websites/code/studygolang/src/controller/like.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The StudyGolang Authors. All rights reserved.
1+
// Copyright 2014 The StudyGolang Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44
// http://studygolang.com
@@ -9,10 +9,36 @@ package controller
99
// 喜欢系统
1010

1111
import (
12+
"fmt"
1213
"net/http"
14+
15+
"filter"
16+
"github.com/studygolang/mux"
17+
"service"
18+
"util"
1319
)
1420

1521
// 喜欢(或取消喜欢)
1622
// uri: /like/{objid:[0-9]+}.json
1723
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
24+
vars := mux.Vars(req)
25+
user, _ := filter.CurrentUser(req)
26+
27+
if !util.CheckInt(req.PostForm, "objtype") || !util.CheckInt(req.PostForm, "flag") {
28+
fmt.Fprint(rw, `{"ok": 0, "error":"参数错误"}`)
29+
return
30+
}
31+
32+
uid := user["uid"].(int)
33+
objid := util.MustInt(vars["objid"])
34+
objtype := util.MustInt(req.PostFormValue("objtype"))
35+
likeFlag := util.MustInt(req.PostFormValue("flag"))
36+
37+
err := service.LikeObject(uid, objid, objtype, likeFlag)
38+
if err != nil {
39+
fmt.Fprint(rw, `{"ok": 0, "error":"服务器内部错误"}`)
40+
return
41+
}
42+
43+
fmt.Fprint(rw, `{"ok": 1, "msg":"success", "data":""}`)
1844
}

websites/code/studygolang/src/model/article.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ func (this *Article) FindAll(selectCol ...string) ([]*Article, error) {
9191
}
9292

9393
// 为了支持连写
94-
func (this *Article) Where(condition string) *Article {
95-
this.Dao.Where(condition)
94+
func (this *Article) Where(condition string, args ...interface{}) *Article {
95+
this.Dao.Where(condition, args...)
9696
return this
9797
}
9898

@@ -207,8 +207,8 @@ func (this *CrawlRule) FindAll(selectCol ...string) ([]*CrawlRule, error) {
207207
}
208208

209209
// 为了支持连写
210-
func (this *CrawlRule) Where(condition string) *CrawlRule {
211-
this.Dao.Where(condition)
210+
func (this *CrawlRule) Where(condition string, args ...interface{}) *CrawlRule {
211+
this.Dao.Where(condition, args...)
212212
return this
213213
}
214214

websites/code/studygolang/src/model/authority.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func (this *Authority) FindAll(selectCol ...string) ([]*Authority, error) {
7070
}
7171

7272
// 为了支持连写
73-
func (this *Authority) Where(condition string) *Authority {
74-
this.Dao.Where(condition)
73+
func (this *Authority) Where(condition string, args ...interface{}) *Authority {
74+
this.Dao.Where(condition, args...)
7575
return this
7676
}
7777

websites/code/studygolang/src/model/blog.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ func (this *Blog) FindAll(selectCol ...string) ([]*Blog, error) {
6060
return blogList, nil
6161
}
6262

63-
func (this *Blog) Where(condition string) *Blog {
64-
this.Dao.Where(condition)
63+
// 为了支持连写
64+
func (this *Blog) Where(condition string, args ...interface{}) *Blog {
65+
this.Dao.Where(condition, args...)
6566
return this
6667
}
6768

websites/code/studygolang/src/model/comment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"util"
1212
)
1313

14+
// 不要修改常量的顺序
1415
const (
1516
TYPE_TOPIC = iota // 帖子
1617
TYPE_ARTICLE // 博文
@@ -87,8 +88,8 @@ func (this *Comment) FindAll(selectCol ...string) ([]*Comment, error) {
8788
}
8889

8990
// 为了支持连写
90-
func (this *Comment) Where(condition string) *Comment {
91-
this.Dao.Where(condition)
91+
func (this *Comment) Where(condition string, args ...interface{}) *Comment {
92+
this.Dao.Where(condition, args...)
9293
return this
9394
}
9495

websites/code/studygolang/src/model/dao.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (this *Dao) Increment(field string, num int) error {
120120
if num > 0 {
121121
setClause += fmt.Sprintf("+%d", num)
122122
} else {
123-
setClause += fmt.Sprintf("-%d", num)
123+
setClause += fmt.Sprintf("-%d", -num)
124124
}
125125
strSql := fmt.Sprintf("UPDATE `%s` SET %s %s", this.tablename, setClause, where)
126126
logger.Debugln("Increment sql:", strSql)
@@ -411,9 +411,20 @@ func (this *Dao) ColValues() []interface{} {
411411
return this.colValues
412412
}
413413

414-
// 查询条件处理(TODO:暂时没有处理between和in)
414+
func (this *Dao) Where(condition string, args ...interface{}) {
415+
if len(args) == 0 {
416+
this._where(condition)
417+
return
418+
}
419+
420+
this.where = condition
421+
this.whereVal = args
422+
}
423+
424+
// 查询条件处理(TODO:暂时没有处理between)
415425
// bug: aid='' 时,会自动去掉条件(FindAuthority)
416-
func (this *Dao) Where(condition string) {
426+
// 兼容之前的写法
427+
func (this *Dao) _where(condition string) {
417428
this.whereVal = make([]interface{}, 0, 5)
418429
stringBuilder := util.NewBuffer()
419430
conditions := SplitIn(condition, []string{" and ", " AND ", " or ", " OR "}...)

websites/code/studygolang/src/model/dynamic.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func (this *Dynamic) FindAll(selectCol ...string) ([]*Dynamic, error) {
6868
}
6969

7070
// 为了支持连写
71-
func (this *Dynamic) Where(condition string) *Dynamic {
72-
this.Dao.Where(condition)
71+
func (this *Dynamic) Where(condition string, args ...interface{}) *Dynamic {
72+
this.Dao.Where(condition, args...)
7373
return this
7474
}
7575

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2014 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author:polaris [email protected]
6+
7+
package model
8+
9+
import (
10+
"logger"
11+
"util"
12+
)
13+
14+
const (
15+
FLAG_CANCEL = iota
16+
FLAG_LIKE // 喜欢
17+
FLAG_UNLIKE // 不喜欢(暂时不支持)
18+
)
19+
20+
// 评论信息(通用)
21+
type Like struct {
22+
Uid int `json:"uid"`
23+
Objid int `json:"objid"`
24+
Objtype int `json:"objtype"`
25+
Flag int `json:"flag"`
26+
Ctime string `json:"ctime"`
27+
28+
// 数据库访问对象
29+
*Dao
30+
}
31+
32+
func NewLike() *Like {
33+
return &Like{
34+
Dao: &Dao{tablename: "Likes"},
35+
}
36+
}
37+
38+
func (this *Like) Insert() (int64, error) {
39+
this.prepareInsertData()
40+
result, err := this.Dao.Insert()
41+
if err != nil {
42+
return 0, err
43+
}
44+
return result.RowsAffected()
45+
}
46+
47+
func (this *Like) Find(selectCol ...string) error {
48+
return this.Dao.Find(this.colFieldMap(), selectCol...)
49+
}
50+
51+
func (this *Like) FindAll(selectCol ...string) ([]*Like, error) {
52+
if len(selectCol) == 0 {
53+
selectCol = util.MapKeys(this.colFieldMap())
54+
}
55+
rows, err := this.Dao.FindAll(selectCol...)
56+
if err != nil {
57+
return nil, err
58+
}
59+
// TODO:
60+
likeList := make([]*Like, 0, 10)
61+
colNum := len(selectCol)
62+
for rows.Next() {
63+
like := NewLike()
64+
err = this.Scan(rows, colNum, like.colFieldMap(), selectCol...)
65+
if err != nil {
66+
logger.Errorln("Like FindAll Scan Error:", err)
67+
continue
68+
}
69+
likeList = append(likeList, like)
70+
}
71+
return likeList, nil
72+
}
73+
74+
// 为了支持连写
75+
func (this *Like) Where(condition string, args ...interface{}) *Like {
76+
this.Dao.Where(condition, args...)
77+
return this
78+
}
79+
80+
// 为了支持连写
81+
func (this *Like) Set(clause string, args ...interface{}) *Like {
82+
this.Dao.Set(clause, args...)
83+
return this
84+
}
85+
86+
// 为了支持连写
87+
func (this *Like) Limit(limit string) *Like {
88+
this.Dao.Limit(limit)
89+
return this
90+
}
91+
92+
// 为了支持连写
93+
func (this *Like) Order(order string) *Like {
94+
this.Dao.Order(order)
95+
return this
96+
}
97+
98+
func (this *Like) prepareInsertData() {
99+
this.columns = []string{"objid", "objtype", "uid", "flag"}
100+
this.colValues = []interface{}{this.Objid, this.Objtype, this.Uid, this.Flag}
101+
}
102+
103+
func (this *Like) colFieldMap() map[string]interface{} {
104+
return map[string]interface{}{
105+
"uid": &this.Uid,
106+
"objid": &this.Objid,
107+
"objtype": &this.Objtype,
108+
"flag": &this.Flag,
109+
"ctime": &this.Ctime,
110+
}
111+
}

0 commit comments

Comments
 (0)