Skip to content

Commit

Permalink
✨ refactor: Change filter parameter to accept array of strings
Browse files Browse the repository at this point in the history
- Modified `parseListFilter` to accept `[]string` instead of `string`
- Updated `List` method in `Service` to use array of filters
- Changed `listBookmarksRequest` struct to use `[]string` for filter
- Updated frontend API calls to handle array of filters
- Added URLSearchParams for proper URL encoding of multiple filters
  • Loading branch information
vaayne committed Jan 5, 2025
1 parent a2da9ca commit ddd57c2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
20 changes: 14 additions & 6 deletions internal/core/bookmarks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func (s *Service) Get(ctx context.Context, tx db.DBTX, id, userID uuid.UUID) (*C
return &dto, nil
}

func parseListFilter(filter string) (domains, contentTypes, tags []string) {
if filter == "" {
func parseListFilter(filters []string) (domains, contentTypes, tags []string) {
if len(filters) == 0 {
return
}

Expand All @@ -93,8 +93,7 @@ func parseListFilter(filter string) (domains, contentTypes, tags []string) {
tags = make([]string, 0)

// Parse filter=category:article;type:rss
parts := strings.Split(filter, ";")
for _, part := range parts {
for _, part := range filters {
kv := strings.Split(part, ":")
if len(kv) != 2 {
continue
Expand All @@ -108,19 +107,28 @@ func parseListFilter(filter string) (domains, contentTypes, tags []string) {
tags = append(tags, kv[1])
}
}
if len(domains) == 0 {
domains = nil
}
if len(contentTypes) == 0 {
contentTypes = nil
}
if len(tags) == 0 {
tags = nil
}
return
}

// ListBookmarks retrieves a paginated list of bookmarks for a user
func (s *Service) List(ctx context.Context, tx db.DBTX, userID uuid.UUID, filter, query string, limit, offset int32) ([]*ContentDTO, int64, error) {
func (s *Service) List(ctx context.Context, tx db.DBTX, userID uuid.UUID, filters []string, query string, limit, offset int32) ([]*ContentDTO, int64, error) {
if limit <= 0 || limit > 100 {
limit = 50 // Default limit
}
if offset < 0 {
offset = 0
}

domains, contentTypes, tags := parseListFilter(filter)
domains, contentTypes, tags := parseListFilter(filters)
totalCount := int64(0)
cs, err := s.dao.ListContents(ctx, tx, db.ListContentsParams{
UserID: userID,
Expand Down
10 changes: 5 additions & 5 deletions internal/port/httpserver/handler_bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
type BookmarkService interface {
Create(ctx context.Context, tx db.DBTX, dto *bookmarks.ContentDTO) (*bookmarks.ContentDTO, error)
Get(ctx context.Context, tx db.DBTX, id, userID uuid.UUID) (*bookmarks.ContentDTO, error)
List(ctx context.Context, tx db.DBTX, userID uuid.UUID, filter, query string, limit, offset int32) ([]*bookmarks.ContentDTO, int64, error)
List(ctx context.Context, tx db.DBTX, userID uuid.UUID, filter []string, query string, limit, offset int32) ([]*bookmarks.ContentDTO, int64, error)
ListTags(ctx context.Context, tx db.DBTX, userID uuid.UUID) ([]bookmarks.TagDTO, error)
ListDomains(ctx context.Context, tx db.DBTX, userID uuid.UUID) ([]bookmarks.DomainDTO, error)
Update(ctx context.Context, tx db.DBTX, id, userID uuid.UUID, dto *bookmarks.ContentDTO) (*bookmarks.ContentDTO, error)
Expand Down Expand Up @@ -53,10 +53,10 @@ func registerBookmarkHandlers(e *echo.Group, s *Service) {
}

type listBookmarksRequest struct {
Limit int32 `query:"limit" validate:"min=1,max=100"`
Offset int32 `query:"offset" validate:"min=0"`
Filter string `query:"filter"` // filter=category:article;type:rss
Query string `query:"query"` // query=keyword
Limit int32 `query:"limit" validate:"min=1,max=100"`
Offset int32 `query:"offset" validate:"min=0"`
Filter []string `query:"filter"` // filter=category:article;type:rss
Query string `query:"query"` // query=keyword
}

type listBookmarksResponse struct {
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/bookmarks/bookmarks-list-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function BookmarksListView({
const { data, isLoading } = useBookmarks(
limit,
offset,
search.filters.join(";"),
search.filters,
search.query,
);
const bookmarks = data?.bookmarks ?? [];
Expand Down
31 changes: 23 additions & 8 deletions web/src/lib/apis/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ interface BookmarkRefreshInput {

// API Functions
const api = {
list: (filter = "", query = "", limit = 20, offset = 0) =>
// filter=category:article;type:rss
fetcher<ListBookmarksResponse>(
`/api/v1/bookmarks?limit=${limit}&offset=${offset}&query=${query}&filter=${filter}`,
),
list: (filters: string[] = [], query = "", limit = 20, offset = 0) => {
const params = new URLSearchParams();
params.set("limit", limit.toString());
params.set("offset", offset.toString());
params.set("query", query);
// Append each filter separately
for (const filter of filters) {
params.append("filter", filter);
}

const url = `/api/v1/bookmarks?${params.toString()}`;
console.log("api.list", url);
return fetcher<ListBookmarksResponse>(url);
},

create: (input: BookmarkCreateInput) =>
fetcher<Bookmark>("/api/v1/bookmarks", {
Expand Down Expand Up @@ -120,10 +129,16 @@ const api = {
};

// SWR Hooks
export function useBookmarks(limit = 20, offset = 0, filter = "", query = "") {
export function useBookmarks(
limit = 20,
offset = 0,
filters: string[] = [],
query = "",
) {
console.log("useBookmarks", filters, query, limit, offset);
return useSWR<ListBookmarksResponse>(
["bookmarks", filter, query, limit, offset],
() => api.list(filter, query, limit, offset),
["bookmarks", filters, query, limit, offset],
() => api.list(filters, query, limit, offset),
);
}

Expand Down

0 comments on commit ddd57c2

Please sign in to comment.