Skip to content

Commit

Permalink
feat: added tags filter
Browse files Browse the repository at this point in the history
  • Loading branch information
SoraAsc committed Jan 3, 2025
1 parent c43ba8d commit 935fc79
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/pescarte/blog/blog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Pescarte.Blog do
Post
|> apply_post_search_filter(filters)
|> apply_post_date_filter(filters)
|> apply_post_tag_filter(filters)
|> apply_pagination(filters)
|> Repo.replica().all()
end
Expand All @@ -34,7 +35,13 @@ defmodule Pescarte.Blog do
end
defp apply_post_date_filter(query, _), do: query

defp apply_pagination(query, %{page, page_size}) do
defp apply_post_tag_filter(query, %{tags: tags}) when is_list(tags) do
from p in query, join: t in assoc(p, :blog_tags), where: t.nome in ^tags,
group_by: p.id, having: count(t.id) == ^length(tags)
end
defp apply_post_tag_filter(query, _), do: query

defp apply_pagination(query, %{page: page, page_size: page_size}) do
offset = (page - 1) * page_size
from p in query, limit: ^page_size, offset: ^offset
end
Expand Down

0 comments on commit 935fc79

Please sign in to comment.