コード例 #1
0
ファイル: users.go プロジェクト: wakuworks/retwitter
func (us *Users) GET(c *kocha.Context) error {
	var n int
	var users []model.User

	num, _ := GetPerItem(c)
	currentPage, _ := GetPageNum(c)
	page := currentPage
	if page > 0 {
		page--
	}

	dbInst := db.Get("default")
	condition := dbInst.OrderBy("id", genmai.DESC).Limit(num).Offset(page * num)
	err := dbInst.Select(&users, condition)
	if err == nil {
		if page > 0 && len(users) == 0 {
			c.Redirect("/users", false)
		}
		err = dbInst.Select(&n, dbInst.Count(), dbInst.From(&model.User{}))
	}

	if err != nil {
		c.RenderError(500, err, nil)
	}

	pagination, _ := simpagin.New(currentPage, n, num, DEFAULT_PER_PAGE)
	return c.Render(map[string]interface{}{
		"ControllerName": "Users",
		"users":          users,
		"totalPage":      int(math.Ceil(float64(n) / float64(num))),
		"pagination":     pagination,
	})
}
コード例 #2
0
ファイル: queues.go プロジェクト: wakuworks/retwitter
func (qu *Queues) GET(c *kocha.Context) error {
	var n int
	var queues []Queue

	num, _ := GetPerItem(c)
	currentPage, _ := GetPageNum(c)
	page := currentPage
	if page > 0 {
		page--
	}

	dbInst := db.Get("default")
	rows, err := dbInst.DB().Query(`
		SELECT queues.id, status, screen_name, tweet_id,
		action, execute_time, fail_count, queues.created_at, queues.updated_at
		FROM queues JOIN users ON user_id = users.id
		ORDER BY id DESC LIMIT ? OFFSET ?
	`, num, page*num)

	if err == nil {
		defer rows.Close()
		for rows.Next() {
			var queue Queue
			if err := rows.Scan(
				&queue.Id,
				&queue.Status,
				&queue.ScreenName,
				&queue.TweetId,
				&queue.Action,
				&queue.ExecuteTime,
				&queue.FailCount,
				&queue.CreatedAt,
				&queue.UpdatedAt,
			); err != nil {
				panic(err)
			}
			queues = append(queues, queue)
		}

		if page > 0 && len(queues) == 0 {
			c.Redirect("/queues", false)
		}
		err = dbInst.Select(&n, dbInst.Count(), dbInst.From(&model.Queue{}))
	}

	if err != nil {
		c.RenderError(500, err, nil)
	}

	pagination, _ := simpagin.New(currentPage, n, num, DEFAULT_PER_PAGE)
	return c.Render(map[string]interface{}{
		"ControllerName": "Queues",
		"queues":         queues,
		"totalPage":      int(math.Ceil(float64(n) / float64(num))),
		"pagination":     pagination,
	})
}