Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/code_corps/project/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ defmodule CodeCorps.Project.Query do
Contains queries for retrieving projects
"""
alias CodeCorps.{
Helpers.Query,
Project,
SluggedRoute,
Repo
}

import Ecto.Query

@doc ~S"""
Returns a list of `Project` records based on the provided filter.

If the filter contains a `slug` key, returns all projects for the specified
`Organization.`

If the filter does not contain a `slug` key, returns all `approved` projects.
If the filter does not contain a `slug` key, filters by optional params.
"""
@spec list(map) :: list(Project.t)
def list(%{"slug" => slug}) do
Expand All @@ -26,10 +25,10 @@ defmodule CodeCorps.Project.Query do
|> Map.get(:organization)
|> Map.get(:projects)
end
def list(%{}) do
def list(%{} = params) do
Project
|> where(approved: true)
|> Repo.all
|> Query.optional_filters(params, ~w(approved)a)
|> Repo.all()
end

@doc ~S"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ defmodule CodeCorpsWeb.ProjectControllerTest do
@invalid_attrs %{title: ""}

describe "index" do
test "lists all approved entries on index", %{conn: conn} do
test "filters on index", %{conn: conn} do
[project_1, project_2] = insert_pair(:project, approved: true)
project_3 = insert(:project, approved: false)

path = "/projects?approved=true"

returned_ids =
conn
|> request_index
|> get(path)
|> json_response(200)
|> ids_from_response

Expand Down