mirror of https://github.com/jlelse/GoBlog
Improve post counting
This commit is contained in:
parent
92e508d5cf
commit
1a3178eea7
34
posts.go
34
posts.go
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -240,10 +239,8 @@ type postsRequestConfig struct {
|
||||||
parameterValue string
|
parameterValue string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPosts(context context.Context, config *postsRequestConfig) (posts []*post, err error) {
|
func buildQuery(config *postsRequestConfig) (query string, params []interface{}) {
|
||||||
paths := make(map[string]int)
|
defaultSelection := "select p.path as path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(blog, ''), coalesce(section, ''), coalesce(parameter, ''), coalesce(value, '') "
|
||||||
var rows *sql.Rows
|
|
||||||
defaultSelection := "select p.path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(blog, ''), coalesce(section, ''), coalesce(parameter, ''), coalesce(value, '') "
|
|
||||||
postsTable := "posts"
|
postsTable := "posts"
|
||||||
if config.blog != "" {
|
if config.blog != "" {
|
||||||
postsTable = "(select * from " + postsTable + " where blog = '" + config.blog + "')"
|
postsTable = "(select * from " + postsTable + " where blog = '" + config.blog + "')"
|
||||||
|
@ -271,21 +268,27 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*pos
|
||||||
defaultTables := " from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path "
|
defaultTables := " from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path "
|
||||||
defaultSorting := " order by p.published desc "
|
defaultSorting := " order by p.published desc "
|
||||||
if config.path != "" {
|
if config.path != "" {
|
||||||
query := defaultSelection + defaultTables + " where p.path=?" + defaultSorting
|
query = defaultSelection + defaultTables + " where p.path=?" + defaultSorting
|
||||||
rows, err = appDb.QueryContext(context, query, config.path)
|
params = []interface{}{config.path}
|
||||||
} else if config.limit != 0 || config.offset != 0 {
|
} else if config.limit != 0 || config.offset != 0 {
|
||||||
query := defaultSelection + " from (select * from " + postsTable + " p " + defaultSorting + " limit ? offset ?) p left outer join post_parameters pp on p.path = pp.path "
|
query = defaultSelection + " from (select * from " + postsTable + " p " + defaultSorting + " limit ? offset ?) p left outer join post_parameters pp on p.path = pp.path "
|
||||||
rows, err = appDb.QueryContext(context, query, config.limit, config.offset)
|
params = []interface{}{config.limit, config.offset}
|
||||||
} else {
|
} else {
|
||||||
query := defaultSelection + defaultTables + defaultSorting
|
query = defaultSelection + defaultTables + defaultSorting
|
||||||
rows, err = appDb.QueryContext(context, query)
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPosts(context context.Context, config *postsRequestConfig) (posts []*post, err error) {
|
||||||
|
query, queryParams := buildQuery(config)
|
||||||
|
rows, err := appDb.QueryContext(context, query, queryParams...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
_ = rows.Close()
|
_ = rows.Close()
|
||||||
}()
|
}()
|
||||||
|
paths := make(map[string]int)
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
p := &post{}
|
p := &post{}
|
||||||
var parameterName, parameterValue string
|
var parameterName, parameterValue string
|
||||||
|
@ -306,9 +309,12 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*pos
|
||||||
return posts, nil
|
return posts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func countPosts(context context.Context, config *postsRequestConfig) (int, error) {
|
func countPosts(context context.Context, config *postsRequestConfig) (count int, err error) {
|
||||||
posts, err := getPosts(context, config)
|
query, params := buildQuery(config)
|
||||||
return len(posts), err
|
query = "select count(distinct path) from (" + query + ")"
|
||||||
|
row := appDb.QueryRowContext(context, query, params...)
|
||||||
|
err = row.Scan(&count)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func allPostPaths() ([]string, error) {
|
func allPostPaths() ([]string, error) {
|
||||||
|
|
Loading…
Reference in New Issue