// 获取由用户发布的评论 // @page: 从1开始 func Comment_ByUser(userId int64, page, pagesize int) []Comment { if page < 1 { page = 1 } page = page - 1 if pagesize == 0 { pagesize = 20 } var db *goku.MysqlDB = GetDB() defer db.Close() qi := goku.SqlQueryInfo{} qi.Limit = pagesize qi.Offset = page * pagesize qi.Where = "`user_id`=?" qi.Params = []interface{}{userId} qi.Order = "id desc" var comments []Comment err := db.GetStructs(&comments, qi) if err != nil { goku.Logger().Errorln(err.Error()) return nil } return comments }
// 获取用户列表. // @page: 从1开始的页数 // @return: users, total-count, err func User_GetList(page, pagesize int, order string) ([]User, int64, error) { var db *goku.MysqlDB = GetDB() defer db.Close() page, pagesize = utils.PageCheck(page, pagesize) qi := goku.SqlQueryInfo{} qi.Limit = pagesize qi.Offset = pagesize * page if order == "" { qi.Order = "id desc" } else { qi.Order = order } var users []User err := db.GetStructs(&users, qi) if err != nil { goku.Logger().Errorln(err.Error()) return nil, 0, err } total, err := db.Count("user", "") if err != nil { goku.Logger().Errorln(err.Error()) } return users, total, nil }
// @page: 从1开始 // @return: comments, total-count, err func Comment_GetByPage(page, pagesize int, order string) ([]Comment, int64, error) { var db *goku.MysqlDB = GetDB() defer db.Close() page, pagesize = utils.PageCheck(page, pagesize) qi := goku.SqlQueryInfo{} qi.Limit = pagesize qi.Offset = page * pagesize if order == "" { qi.Order = "id desc" } else { qi.Order = order } var comments []Comment err := db.GetStructs(&comments, qi) if err != nil { goku.Logger().Errorln(err.Error()) return nil, 0, err } total, err := db.Count("comment", "") if err != nil { goku.Logger().Errorln(err.Error()) } return comments, total, nil }
func GetTodoLists() (*[]Todo, error) { var db *goku.MysqlDB = GetDB() defer db.Close() qi := goku.SqlQueryInfo{} qi.Order = "finished asc, id desc" var todos []Todo err := db.GetStructs(&todos, qi) return &todos, err }
func Topic_GetTops(page, pagesize int) ([]Topic, error) { var db *goku.MysqlDB = GetDB() defer db.Close() qi := goku.SqlQueryInfo{} qi.Order = "link_count desc" var topics []Topic err := db.GetStructs(&topics, qi) if err != nil { goku.Logger().Errorln(err.Error()) } return topics, err }
// 获取link的评论 func Comment_ForLink(linkId int64) []Comment { var db *goku.MysqlDB = GetDB() defer db.Close() qi := goku.SqlQueryInfo{} qi.Where = "`link_id`=?" qi.Params = []interface{}{linkId} qi.Order = "id asc" var comments []Comment err := db.GetStructs(&comments, qi) if err != nil { goku.Logger().Errorln(err.Error()) return nil } return comments }
func User_GetByIds(ids []int64) ([]User, error) { var db *goku.MysqlDB = GetDB() defer db.Close() users := []User{} qi := goku.SqlQueryInfo{} sids := "" for _, id := range ids { sids += "," + strconv.FormatInt(id, 10) } qi.Where = "id in (" + sids[1:] + ")" // qi.Params = []interface{}{ids} err := db.GetStructs(&users, qi) if err != nil { goku.Logger().Errorln("User_GetByIds error:", err.Error()) return nil, err } return users, nil }
// 获取由用户发布的link // @page: 从1开始 func Link_ByUser(userId int64, page, pagesize int) []Link { var db *goku.MysqlDB = GetDB() defer db.Close() page, pagesize = utils.PageCheck(page, pagesize) qi := goku.SqlQueryInfo{} qi.Limit = pagesize qi.Offset = page * pagesize qi.Where = "`user_id`=? and `status`=0" qi.Params = []interface{}{userId} qi.Order = "id desc" var links []Link err := db.GetStructs(&links, qi) if err != nil { goku.Logger().Errorln(err.Error()) return nil } return links }
// 转换为用于view显示用的实例 func Link_ToVLink(links []Link, ctx *goku.HttpContext) []VLink { if links == nil || len(links) < 1 { return nil } var userId int64 if user, ok := ctx.Data["user"].(*User); ok && user != nil { userId = user.Id } l := len(links) vlinks := make([]VLink, l, l) uids := make([]string, l, l) lids := make([]string, l, l) lindex := make(map[int64]*VLink) for i, link := range links { uids[i] = strconv.FormatInt(link.UserId, 10) lids[i] = strconv.FormatInt(link.Id, 10) vlinks[i] = VLink{Link: link} lindex[link.Id] = &(vlinks[i]) } var db *goku.MysqlDB = GetDB() defer db.Close() // 添加用户信息 userIndex := make(map[int64]*User) qi := goku.SqlQueryInfo{} qi.Where = fmt.Sprintf("`id` in (%v)", strings.Join(uids, ",")) var users []User err := db.GetStructs(&users, qi) if err != nil { goku.Logger().Errorln(err.Error()) } else if users != nil { for i, _ := range users { user := &users[i] userIndex[user.Id] = user } } for i, _ := range vlinks { link := &vlinks[i] if user, ok := userIndex[link.UserId]; ok { link.user = user if user.Id == userId { link.SharedByMe = true } } } // 添加投票信息 if userId > 0 { qi = goku.SqlQueryInfo{} qi.Where = fmt.Sprintf("`user_id`=%v AND `link_id` in (%v)", userId, strings.Join(lids, ",")) var srs []LinkSupportRecord err = db.GetStructs(&srs, qi) if err != nil { goku.Logger().Errorln(err.Error()) } else if srs != nil { for _, sr := range srs { if sr.Score == 1 { lindex[sr.LinkId].VoteUped = true } else if sr.Score == -1 { lindex[sr.LinkId].VoteDowned = true } } } // 添加收藏信息 qi.Fields = "link_id" rows, err := db.Select("user_favorite_link", qi) if err != nil { goku.Logger().Errorln(err.Error()) } else { var linkId int64 for rows.Next() { err = rows.Scan(&linkId) if err != nil { goku.Logger().Errorln(err.Error()) continue } lindex[linkId].Favorited = true } } } return vlinks }