Skip to content

Commit b9e36a1

Browse files
committed
Rename "finder" package to "query" (for CQRS)
1 parent 132032b commit b9e36a1

12 files changed

Lines changed: 63 additions & 63 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package finder
1+
package query
22

33
import (
44
"context"
@@ -22,7 +22,7 @@ type Article struct {
2222
Tags []string `db:"tags"`
2323
}
2424

25-
func (f *Finder) ListArticles(limit, offset int) ([]Article, error) {
25+
func (f *Query) ListArticles(limit, offset int) ([]Article, error) {
2626
stmt := `
2727
WITH latest AS (
2828
SELECT
@@ -64,7 +64,7 @@ func (f *Finder) ListArticles(limit, offset int) ([]Article, error) {
6464
return articles, nil
6565
}
6666

67-
func (f *Finder) ListArticlesByAccount(account *model.Account, limit, offset int) ([]Article, error) {
67+
func (f *Query) ListArticlesByAccount(account *model.Account, limit, offset int) ([]Article, error) {
6868
stmt := `
6969
WITH latest AS (
7070
SELECT
@@ -111,7 +111,7 @@ func (f *Finder) ListArticlesByAccount(account *model.Account, limit, offset int
111111
return articles, nil
112112
}
113113

114-
func (f *Finder) SearchArticles(search string, limit, offset int) ([]Article, error) {
114+
func (f *Query) SearchArticles(search string, limit, offset int) ([]Article, error) {
115115
stmt := `
116116
WITH relevant AS (
117117
SELECT
@@ -154,7 +154,7 @@ func (f *Finder) SearchArticles(search string, limit, offset int) ([]Article, er
154154
return articles, nil
155155
}
156156

157-
func (f *Finder) SearchArticlesByAccount(account *model.Account, search string, limit, offset int) ([]Article, error) {
157+
func (f *Query) SearchArticlesByAccount(account *model.Account, search string, limit, offset int) ([]Article, error) {
158158
stmt := `
159159
WITH relevant AS (
160160
SELECT
@@ -202,7 +202,7 @@ func (f *Finder) SearchArticlesByAccount(account *model.Account, search string,
202202
return articles, nil
203203
}
204204

205-
func (f *Finder) CountArticles() (int, error) {
205+
func (f *Query) CountArticles() (int, error) {
206206
stmt := `
207207
SELECT count(*)
208208
FROM post`
@@ -223,7 +223,7 @@ func (f *Finder) CountArticles() (int, error) {
223223
return count, nil
224224
}
225225

226-
func (f *Finder) CountArticlesByAccount(account *model.Account) (int, error) {
226+
func (f *Query) CountArticlesByAccount(account *model.Account) (int, error) {
227227
stmt := `
228228
SELECT count(*)
229229
FROM post
@@ -249,7 +249,7 @@ func (f *Finder) CountArticlesByAccount(account *model.Account) (int, error) {
249249
return count, nil
250250
}
251251

252-
func (f *Finder) CountSearchArticles(search string) (int, error) {
252+
func (f *Query) CountSearchArticles(search string) (int, error) {
253253
stmt := `
254254
SELECT count(*)
255255
FROM post
@@ -271,7 +271,7 @@ func (f *Finder) CountSearchArticles(search string) (int, error) {
271271
return count, nil
272272
}
273273

274-
func (f *Finder) CountSearchArticlesByAccount(account *model.Account, search string) (int, error) {
274+
func (f *Query) CountSearchArticlesByAccount(account *model.Account, search string) (int, error) {
275275
stmt := `
276276
SELECT count(*)
277277
FROM post
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package finder_test
1+
package query_test
22

33
import (
44
"testing"
@@ -14,7 +14,7 @@ func TestListArticles(t *testing.T) {
1414
repo, repoCloser := test.NewRepository(t)
1515
defer repoCloser()
1616

17-
find, findCloser := test.NewFinder(t)
17+
find, findCloser := test.NewQuery(t)
1818
defer findCloser()
1919

2020
blog := test.CreateBlog(t, repo)
@@ -32,7 +32,7 @@ func TestListArticlesByAccount(t *testing.T) {
3232
repo, repoCloser := test.NewRepository(t)
3333
defer repoCloser()
3434

35-
find, findCloser := test.NewFinder(t)
35+
find, findCloser := test.NewQuery(t)
3636
defer findCloser()
3737

3838
followedBlog := test.CreateBlog(t, repo)
@@ -62,7 +62,7 @@ func TestSearchArticles(t *testing.T) {
6262
repo, repoCloser := test.NewRepository(t)
6363
defer repoCloser()
6464

65-
find, findCloser := test.NewFinder(t)
65+
find, findCloser := test.NewQuery(t)
6666
defer findCloser()
6767

6868
blog := test.NewBlog(t)
@@ -109,7 +109,7 @@ func TestSearchArticlesByAccount(t *testing.T) {
109109
repo, repoCloser := test.NewRepository(t)
110110
defer repoCloser()
111111

112-
find, findCloser := test.NewFinder(t)
112+
find, findCloser := test.NewQuery(t)
113113
defer findCloser()
114114

115115
// Create some followed posts about python.
@@ -161,7 +161,7 @@ func TestCountArticles(t *testing.T) {
161161
repo, repoCloser := test.NewRepository(t)
162162
defer repoCloser()
163163

164-
find, findCloser := test.NewFinder(t)
164+
find, findCloser := test.NewQuery(t)
165165
defer findCloser()
166166

167167
blog := test.CreateBlog(t, repo)
@@ -181,7 +181,7 @@ func TestCountArticlesByAccount(t *testing.T) {
181181
repo, repoCloser := test.NewRepository(t)
182182
defer repoCloser()
183183

184-
find, findCloser := test.NewFinder(t)
184+
find, findCloser := test.NewQuery(t)
185185
defer findCloser()
186186

187187
followedBlog := test.CreateBlog(t, repo)
@@ -209,7 +209,7 @@ func TestCountSearchArticles(t *testing.T) {
209209
repo, repoCloser := test.NewRepository(t)
210210
defer repoCloser()
211211

212-
find, findCloser := test.NewFinder(t)
212+
find, findCloser := test.NewQuery(t)
213213
defer findCloser()
214214

215215
blog := test.CreateBlog(t, repo)
@@ -254,7 +254,7 @@ func TestCountSearchArticlesByAccount(t *testing.T) {
254254
repo, repoCloser := test.NewRepository(t)
255255
defer repoCloser()
256256

257-
find, findCloser := test.NewFinder(t)
257+
find, findCloser := test.NewQuery(t)
258258
defer findCloser()
259259

260260
// Create some followed posts about python.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package finder
1+
package query
22

33
import (
44
"context"
@@ -17,7 +17,7 @@ type BlogForAccount struct {
1717
}
1818

1919
// TODO: Paginate this (will need to add a CountBlogsForAccount method).
20-
func (f *Finder) ListBlogsForAccount(account *model.Account) ([]BlogForAccount, error) {
20+
func (f *Query) ListBlogsForAccount(account *model.Account) ([]BlogForAccount, error) {
2121
stmt := `
2222
SELECT
2323
blog.id,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package finder_test
1+
package query_test
22

33
import (
44
"testing"
@@ -12,7 +12,7 @@ func TestListBlogsForAccount(t *testing.T) {
1212
repo, repoCloser := test.NewRepository(t)
1313
defer repoCloser()
1414

15-
find, findCloser := test.NewFinder(t)
15+
find, findCloser := test.NewQuery(t)
1616
defer findCloser()
1717

1818
account := test.CreateAccount(t, repo)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// This package contains read-only queries that require more data than just the
22
// normalized domain models (like articles or blogs+isFollowing). They are not
33
// grouped by types and instead exist as top-level methods of the Finder struct.
4-
package finder
4+
package query
55

66
import "github.com/theandrew168/bloggulus/backend/postgres"
77

8-
type Finder struct {
8+
type Query struct {
99
conn postgres.Conn
1010
}
1111

12-
func New(conn postgres.Conn) *Finder {
13-
f := Finder{
12+
func New(conn postgres.Conn) *Query {
13+
f := Query{
1414
conn: conn,
1515
}
1616
return &f

backend/test/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"github.com/theandrew168/bloggulus/backend/config"
77
"github.com/theandrew168/bloggulus/backend/fetch"
88
fetchMock "github.com/theandrew168/bloggulus/backend/fetch/mock"
9-
"github.com/theandrew168/bloggulus/backend/finder"
109
"github.com/theandrew168/bloggulus/backend/job"
1110
"github.com/theandrew168/bloggulus/backend/postgres"
11+
"github.com/theandrew168/bloggulus/backend/query"
1212
"github.com/theandrew168/bloggulus/backend/repository"
1313
)
1414

@@ -42,12 +42,12 @@ func NewRepository(t *testing.T) (*repository.Repository, CloserFunc) {
4242
return repo, closer
4343
}
4444

45-
func NewFinder(t *testing.T) (*finder.Finder, CloserFunc) {
45+
func NewQuery(t *testing.T) (*query.Query, CloserFunc) {
4646
t.Helper()
4747

4848
db, closer := NewDatabase(t)
49-
find := finder.New(db)
50-
return find, closer
49+
q := query.New(db)
50+
return q, closer
5151
}
5252

5353
func NewSyncService(

backend/web/blogs.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import (
99

1010
"github.com/google/uuid"
1111

12-
"github.com/theandrew168/bloggulus/backend/finder"
1312
"github.com/theandrew168/bloggulus/backend/job"
1413
"github.com/theandrew168/bloggulus/backend/postgres"
14+
"github.com/theandrew168/bloggulus/backend/query"
1515
"github.com/theandrew168/bloggulus/backend/repository"
1616
"github.com/theandrew168/bloggulus/backend/web/page"
1717
"github.com/theandrew168/bloggulus/backend/web/util"
1818
)
1919

20-
func HandleBlogList(find *finder.Finder) http.Handler {
20+
func HandleBlogList(qry *query.Query) http.Handler {
2121
tmpl := page.NewBlogs()
2222
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2323
account, isLoggedIn := util.GetContextAccount(r)
@@ -26,7 +26,7 @@ func HandleBlogList(find *finder.Finder) http.Handler {
2626
return
2727
}
2828

29-
blogs, err := find.ListBlogsForAccount(account)
29+
blogs, err := qry.ListBlogsForAccount(account)
3030
if err != nil {
3131
util.ListErrorResponse(w, r, err)
3232
return
@@ -49,7 +49,7 @@ func HandleBlogList(find *finder.Finder) http.Handler {
4949
})
5050
}
5151

52-
func HandleBlogCreateForm(repo *repository.Repository, find *finder.Finder, syncService *job.SyncService) http.Handler {
52+
func HandleBlogCreateForm(repo *repository.Repository, syncService *job.SyncService) http.Handler {
5353
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5454
account, isLoggedIn := util.GetContextAccount(r)
5555
if !isLoggedIn {
@@ -153,7 +153,7 @@ func HandleBlogCreateForm(repo *repository.Repository, find *finder.Finder, sync
153153
})
154154
}
155155

156-
func HandleBlogFollowForm(repo *repository.Repository, find *finder.Finder) http.Handler {
156+
func HandleBlogFollowForm(repo *repository.Repository) http.Handler {
157157
tmpl := page.NewBlogs()
158158
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
159159
account, isLoggedIn := util.GetContextAccount(r)
@@ -204,7 +204,7 @@ func HandleBlogFollowForm(repo *repository.Repository, find *finder.Finder) http
204204
data := page.BlogsBlogData{
205205
BaseData: util.GetTemplateBaseData(r, w),
206206

207-
BlogForAccount: finder.BlogForAccount{
207+
BlogForAccount: query.BlogForAccount{
208208
ID: blog.ID(),
209209
Title: blog.Title(),
210210
SiteURL: blog.SiteURL(),
@@ -221,7 +221,7 @@ func HandleBlogFollowForm(repo *repository.Repository, find *finder.Finder) http
221221
})
222222
}
223223

224-
func HandleBlogUnfollowForm(repo *repository.Repository, find *finder.Finder) http.Handler {
224+
func HandleBlogUnfollowForm(repo *repository.Repository) http.Handler {
225225
tmpl := page.NewBlogs()
226226
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
227227
account, isLoggedIn := util.GetContextAccount(r)
@@ -272,7 +272,7 @@ func HandleBlogUnfollowForm(repo *repository.Repository, find *finder.Finder) ht
272272
data := page.BlogsBlogData{
273273
BaseData: util.GetTemplateBaseData(r, w),
274274

275-
BlogForAccount: finder.BlogForAccount{
275+
BlogForAccount: query.BlogForAccount{
276276
ID: blog.ID(),
277277
Title: blog.Title(),
278278
SiteURL: blog.SiteURL(),

backend/web/handler.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"golang.org/x/oauth2/google"
1212

1313
"github.com/theandrew168/bloggulus/backend/config"
14-
"github.com/theandrew168/bloggulus/backend/finder"
1514
"github.com/theandrew168/bloggulus/backend/job"
15+
"github.com/theandrew168/bloggulus/backend/query"
1616
"github.com/theandrew168/bloggulus/backend/repository"
1717
"github.com/theandrew168/bloggulus/backend/web/middleware"
1818
"github.com/theandrew168/bloggulus/backend/web/util"
@@ -44,7 +44,7 @@ func Handler(
4444
public fs.FS,
4545
conf config.Config,
4646
repo *repository.Repository,
47-
find *finder.Finder,
47+
qry *query.Query,
4848
syncService *job.SyncService,
4949
) http.Handler {
5050
mux := http.NewServeMux()
@@ -89,7 +89,7 @@ func Handler(
8989
mux.Handle("/js/", publicFilesHandler)
9090

9191
// The main application routes start here.
92-
mux.Handle("GET /{$}", HandleIndexPage(find))
92+
mux.Handle("GET /{$}", HandleIndexPage(qry))
9393

9494
// Check if the debug auth method should be enabled.
9595
enableDebugAuth := os.Getenv("ENABLE_DEBUG_AUTH") != ""
@@ -106,10 +106,10 @@ func Handler(
106106
mux.Handle("POST /signout", HandleSignOutForm(repo))
107107

108108
// Public blog routes.
109-
mux.Handle("GET /blogs", requireAccount(HandleBlogList(find)))
110-
mux.Handle("POST /blogs/create", requireAccount(HandleBlogCreateForm(repo, find, syncService)))
111-
mux.Handle("POST /blogs/{blogID}/follow", requireAccount(HandleBlogFollowForm(repo, find)))
112-
mux.Handle("POST /blogs/{blogID}/unfollow", requireAccount(HandleBlogUnfollowForm(repo, find)))
109+
mux.Handle("GET /blogs", requireAccount(HandleBlogList(qry)))
110+
mux.Handle("POST /blogs/create", requireAccount(HandleBlogCreateForm(repo, syncService)))
111+
mux.Handle("POST /blogs/{blogID}/follow", requireAccount(HandleBlogFollowForm(repo)))
112+
mux.Handle("POST /blogs/{blogID}/unfollow", requireAccount(HandleBlogUnfollowForm(repo)))
113113

114114
// Private (admin only) blog + post routes.
115115
mux.Handle("GET /blogs/{blogID}", requireAdmin(HandleBlogRead(repo)))

0 commit comments

Comments
 (0)