-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1092ee4
commit 7d6b6f2
Showing
17 changed files
with
1,052 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package profile_search | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Southclaws/dt" | ||
"github.com/Southclaws/fault" | ||
"github.com/Southclaws/fault/fctx" | ||
"github.com/Southclaws/opt" | ||
|
||
"github.com/Southclaws/storyden/app/resources/profile" | ||
"github.com/Southclaws/storyden/internal/ent" | ||
"github.com/Southclaws/storyden/internal/ent/account" | ||
) | ||
|
||
type Filter func(*ent.AccountQuery) | ||
|
||
type Result struct { | ||
PageSize int | ||
Results int | ||
TotalPages int | ||
CurrentPage int | ||
NextPage opt.Optional[int] | ||
Profiles []*profile.Profile | ||
} | ||
|
||
type Repository interface { | ||
Search(ctx context.Context, page int, pageSize int, opts ...Filter) (*Result, error) | ||
} | ||
|
||
func WithDisplayNameContains(q string) Filter { | ||
return func(pq *ent.AccountQuery) { | ||
pq.Where(account.And( | ||
account.NameContainsFold(q), | ||
)) | ||
} | ||
} | ||
|
||
func WithHandleContains(q string) Filter { | ||
return func(pq *ent.AccountQuery) { | ||
pq.Where(account.And( | ||
account.HandleContainsFold(q), | ||
)) | ||
} | ||
} | ||
|
||
type database struct { | ||
db *ent.Client | ||
} | ||
|
||
func New(db *ent.Client) Repository { | ||
return &database{db} | ||
} | ||
|
||
func (d *database) Search(ctx context.Context, page int, size int, filters ...Filter) (*Result, error) { | ||
total, err := d.db.Account.Query().Count(ctx) | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
q := d.db.Account.Query(). | ||
Limit(size + 1). | ||
Offset(page * size). | ||
Order(ent.Asc(account.FieldCreatedAt)) | ||
|
||
for _, fn := range filters { | ||
fn(q) | ||
} | ||
|
||
r, err := q.All(ctx) | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
nextPage := opt.NewSafe(page+1, len(r) >= size) | ||
|
||
profiles, err := dt.MapErr(r, profile.FromModel) | ||
if err != nil { | ||
return nil, fault.Wrap(err, fctx.With(ctx)) | ||
} | ||
|
||
return &Result{ | ||
PageSize: size, | ||
Results: len(profiles), | ||
TotalPages: total / size, | ||
CurrentPage: page, | ||
NextPage: nextPage, | ||
Profiles: profiles, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7d6b6f2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
storyden-homepage – ./home
storyden-homepage-southclaws.vercel.app
storyden.org
storyden-homepage-git-main-southclaws.vercel.app
storyden-homepage.vercel.app
www.storyden.org