Skip to content

Commit

Permalink
adds search functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
underhilllabs committed Dec 9, 2017
1 parent 08c9cc2 commit 35770a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
27 changes: 25 additions & 2 deletions web/controllers/bookmark_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Bookmarks.BookmarkController do
use Bookmarks.Web, :controller
plug :authenticate when action in [:edit, :new, :update, :create, :delete, :bookmarklet]
plug :authenticate when action in [:edit, :new, :update, :create, :delete, :bookmarklet, :search]

alias Bookmarks.Bookmark

Expand Down Expand Up @@ -80,6 +80,29 @@ defmodule Bookmarks.BookmarkController do
end
end

# Search bookmarks
def search(conn, %{"search_term" => st} = params) do
IO.puts("search term: #{st}")
page = from(b in Bookmark,
# TODO: add a private search later
where: [private: false],
where: like(b.title, ^"%#{st}%"),
#or_where: like(b.description, ^"%#{st}%"),
# ecto 2.1
# or_where: [user_id: conn.assigns.current_user.id],
order_by: [desc: b.updated_at])
|> preload(:user)
|> preload(:tags)
|> Repo.paginate(params)
render conn, "index.html",
page: page,
bookmarks: page.entries,
page_number: page.page_number,
page_size: page.page_size,
total_pages: page.total_pages,
total_entries: page.total_entries
end

def archive(conn, %{"id" => id}) do
bookmark = Repo.get!(Bookmark, id)
render(conn, "archive.html", bookmark: bookmark)
Expand Down Expand Up @@ -159,7 +182,7 @@ defmodule Bookmarks.BookmarkController do
conn
else
conn
|> put_flash(:error, "You must be logged in to access that page")
|> put_flash(:error, "You must be logged in to access that page from bookmarklet")
|> redirect(to: session_path(conn, :new, orig_params: conn.params))
|> halt()
end
Expand Down
1 change: 1 addition & 0 deletions web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Bookmarks.Router do
get "/b", BookmarkController, :bookmarklet
get "/goodbye", BookmarkController, :goodbye
get "/reset_api_token", UserController, :reset_api_token
get "/search", BookmarkController, :search

resources "/bookmarks", BookmarkController
#resources "/users", UserController #, only: [:index, :show, :new, :create, :update]
Expand Down

0 comments on commit 35770a4

Please sign in to comment.