Skip to content

Commit

Permalink
喜欢功能开发完毕(未登录时还需要处理一下)
Browse files Browse the repository at this point in the history
  • Loading branch information
polaris1119 committed Oct 6, 2014
1 parent 04d3cc0 commit c8391a1
Show file tree
Hide file tree
Showing 28 changed files with 531 additions and 106 deletions.
20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ studygolang
目前只开发了主要功能,还有不少功能在不断开发中。欢迎有兴趣的gopher们参与进来,一起构建一个完善的 Golang 中文社区,Go语言爱好者的学习家园。

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

# 本地搭建一个 Golang 社区 #

Expand Down
24 changes: 20 additions & 4 deletions websites/code/studygolang/src/controller/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (

const limit = 20

// 在需要评论且要回调的地方注册评论对象
// 在需要评论(喜欢)且要回调的地方注册评论(喜欢)对象
func init() {
// 注册评论对象
// 注册评论(喜欢)对象
service.RegisterCommentObject(model.TYPE_ARTICLE, service.ArticleComment{})
service.RegisterLikeObject(model.TYPE_ARTICLE, service.ArticleLike{})
}

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

// 获取当前用户喜欢对象信息
user, ok := filter.CurrentUser(req)
var likeFlags map[int]int
if ok {
uid := user["uid"].(int)
likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, articles[0].Id, nextId)
}

req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/list.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo})
filter.SetData(req, map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo, "likeflags": likeFlags})
}

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

likeFlag := 0
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)
likeFlag = service.HadLike(uid, article.Id, model.TYPE_ARTICLE)
}

service.Views.Incr(req, model.TYPE_ARTICLE, article.Id)

// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/articles/detail.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1]})
filter.SetData(req, map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag})
}
18 changes: 16 additions & 2 deletions websites/code/studygolang/src/controller/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
package controller

import (
"filter"
"math/rand"
"net/http"

"filter"
"model"
"service"
)

Expand All @@ -24,6 +26,18 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
// 获得最新博文
// blogs := service.FindNewBlogs()
recentArticles := service.FindArticles("0", "10")
// 获取当前用户喜欢对象信息
var likeFlags map[int]int

if len(recentArticles) > 0 {
user, ok := filter.CurrentUser(req)
if ok {
uid := user["uid"].(int)

likeFlags, _ = service.FindUserLikeObjects(uid, model.TYPE_ARTICLE, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id)
}
}

// TODO:开源项目(暂时使用 resource 表)
resources := service.FindResourcesByCatid("2")

Expand All @@ -36,5 +50,5 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
// 设置内容模板
req.Form.Set(filter.CONTENT_TPL_KEY, "/template/index.html")
// 设置模板数据
filter.SetData(req, map[string]interface{}{"topics": newTopics, "articles": recentArticles, "resources": resources[start:end]})
filter.SetData(req, map[string]interface{}{"topics": newTopics, "articles": recentArticles, "likeflags": likeFlags, "resources": resources[start:end]})
}
28 changes: 27 additions & 1 deletion websites/code/studygolang/src/controller/like.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013 The StudyGolang Authors. All rights reserved.
// Copyright 2014 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
Expand All @@ -9,10 +9,36 @@ package controller
// 喜欢系统

import (
"fmt"
"net/http"

"filter"
"github.com/studygolang/mux"
"service"
"util"
)

// 喜欢(或取消喜欢)
// uri: /like/{objid:[0-9]+}.json
func LikeHandler(rw http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
user, _ := filter.CurrentUser(req)

if !util.CheckInt(req.PostForm, "objtype") || !util.CheckInt(req.PostForm, "flag") {
fmt.Fprint(rw, `{"ok": 0, "error":"参数错误"}`)
return
}

uid := user["uid"].(int)
objid := util.MustInt(vars["objid"])
objtype := util.MustInt(req.PostFormValue("objtype"))
likeFlag := util.MustInt(req.PostFormValue("flag"))

err := service.LikeObject(uid, objid, objtype, likeFlag)
if err != nil {
fmt.Fprint(rw, `{"ok": 0, "error":"服务器内部错误"}`)
return
}

fmt.Fprint(rw, `{"ok": 1, "msg":"success", "data":""}`)
}
8 changes: 4 additions & 4 deletions websites/code/studygolang/src/model/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (this *Article) FindAll(selectCol ...string) ([]*Article, error) {
}

// 为了支持连写
func (this *Article) Where(condition string) *Article {
this.Dao.Where(condition)
func (this *Article) Where(condition string, args ...interface{}) *Article {
this.Dao.Where(condition, args...)
return this
}

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

// 为了支持连写
func (this *CrawlRule) Where(condition string) *CrawlRule {
this.Dao.Where(condition)
func (this *CrawlRule) Where(condition string, args ...interface{}) *CrawlRule {
this.Dao.Where(condition, args...)
return this
}

Expand Down
4 changes: 2 additions & 2 deletions websites/code/studygolang/src/model/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func (this *Authority) FindAll(selectCol ...string) ([]*Authority, error) {
}

// 为了支持连写
func (this *Authority) Where(condition string) *Authority {
this.Dao.Where(condition)
func (this *Authority) Where(condition string, args ...interface{}) *Authority {
this.Dao.Where(condition, args...)
return this
}

Expand Down
5 changes: 3 additions & 2 deletions websites/code/studygolang/src/model/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func (this *Blog) FindAll(selectCol ...string) ([]*Blog, error) {
return blogList, nil
}

func (this *Blog) Where(condition string) *Blog {
this.Dao.Where(condition)
// 为了支持连写
func (this *Blog) Where(condition string, args ...interface{}) *Blog {
this.Dao.Where(condition, args...)
return this
}

Expand Down
5 changes: 3 additions & 2 deletions websites/code/studygolang/src/model/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"util"
)

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

// 为了支持连写
func (this *Comment) Where(condition string) *Comment {
this.Dao.Where(condition)
func (this *Comment) Where(condition string, args ...interface{}) *Comment {
this.Dao.Where(condition, args...)
return this
}

Expand Down
17 changes: 14 additions & 3 deletions websites/code/studygolang/src/model/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (this *Dao) Increment(field string, num int) error {
if num > 0 {
setClause += fmt.Sprintf("+%d", num)
} else {
setClause += fmt.Sprintf("-%d", num)
setClause += fmt.Sprintf("-%d", -num)
}
strSql := fmt.Sprintf("UPDATE `%s` SET %s %s", this.tablename, setClause, where)
logger.Debugln("Increment sql:", strSql)
Expand Down Expand Up @@ -411,9 +411,20 @@ func (this *Dao) ColValues() []interface{} {
return this.colValues
}

// 查询条件处理(TODO:暂时没有处理between和in)
func (this *Dao) Where(condition string, args ...interface{}) {
if len(args) == 0 {
this._where(condition)
return
}

this.where = condition
this.whereVal = args
}

// 查询条件处理(TODO:暂时没有处理between)
// bug: aid='' 时,会自动去掉条件(FindAuthority)
func (this *Dao) Where(condition string) {
// 兼容之前的写法
func (this *Dao) _where(condition string) {
this.whereVal = make([]interface{}, 0, 5)
stringBuilder := util.NewBuffer()
conditions := SplitIn(condition, []string{" and ", " AND ", " or ", " OR "}...)
Expand Down
4 changes: 2 additions & 2 deletions websites/code/studygolang/src/model/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (this *Dynamic) FindAll(selectCol ...string) ([]*Dynamic, error) {
}

// 为了支持连写
func (this *Dynamic) Where(condition string) *Dynamic {
this.Dao.Where(condition)
func (this *Dynamic) Where(condition string, args ...interface{}) *Dynamic {
this.Dao.Where(condition, args...)
return this
}

Expand Down
111 changes: 111 additions & 0 deletions websites/code/studygolang/src/model/like.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2014 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author:polaris studygolang@gmail.com

package model

import (
"logger"
"util"
)

const (
FLAG_CANCEL = iota
FLAG_LIKE // 喜欢
FLAG_UNLIKE // 不喜欢(暂时不支持)
)

// 评论信息(通用)
type Like struct {
Uid int `json:"uid"`
Objid int `json:"objid"`
Objtype int `json:"objtype"`
Flag int `json:"flag"`
Ctime string `json:"ctime"`

// 数据库访问对象
*Dao
}

func NewLike() *Like {
return &Like{
Dao: &Dao{tablename: "Likes"},
}
}

func (this *Like) Insert() (int64, error) {
this.prepareInsertData()
result, err := this.Dao.Insert()
if err != nil {
return 0, err
}
return result.RowsAffected()
}

func (this *Like) Find(selectCol ...string) error {
return this.Dao.Find(this.colFieldMap(), selectCol...)
}

func (this *Like) FindAll(selectCol ...string) ([]*Like, error) {
if len(selectCol) == 0 {
selectCol = util.MapKeys(this.colFieldMap())
}
rows, err := this.Dao.FindAll(selectCol...)
if err != nil {
return nil, err
}
// TODO:
likeList := make([]*Like, 0, 10)
colNum := len(selectCol)
for rows.Next() {
like := NewLike()
err = this.Scan(rows, colNum, like.colFieldMap(), selectCol...)
if err != nil {
logger.Errorln("Like FindAll Scan Error:", err)
continue
}
likeList = append(likeList, like)
}
return likeList, nil
}

// 为了支持连写
func (this *Like) Where(condition string, args ...interface{}) *Like {
this.Dao.Where(condition, args...)
return this
}

// 为了支持连写
func (this *Like) Set(clause string, args ...interface{}) *Like {
this.Dao.Set(clause, args...)
return this
}

// 为了支持连写
func (this *Like) Limit(limit string) *Like {
this.Dao.Limit(limit)
return this
}

// 为了支持连写
func (this *Like) Order(order string) *Like {
this.Dao.Order(order)
return this
}

func (this *Like) prepareInsertData() {
this.columns = []string{"objid", "objtype", "uid", "flag"}
this.colValues = []interface{}{this.Objid, this.Objtype, this.Uid, this.Flag}
}

func (this *Like) colFieldMap() map[string]interface{} {
return map[string]interface{}{
"uid": &this.Uid,
"objid": &this.Objid,
"objtype": &this.Objtype,
"flag": &this.Flag,
"ctime": &this.Ctime,
}
}
Loading

0 comments on commit c8391a1

Please sign in to comment.