Skip to content

Commit

Permalink
Allow searching for multiple values within a post attributes
Browse files Browse the repository at this point in the history
Allow queries like `?values[key][]=value1&values[key][]=value2`
rather than just `?values[key]=value1`
  • Loading branch information
rjmackay committed Jul 7, 2017
1 parent a3bf331 commit f45fc00
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions application/classes/Ushahidi/Repository/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ protected function setSearchConditions(SearchData $search)
{
$attribute = $this->form_attribute_repo->getByKey($key);

if (!is_array($value)) {
$value = explode(',', $value);
}

$sub = $this->post_value_factory
->getRepo($attribute->type)
->getValueQuery($attribute->id, $value);
Expand Down
14 changes: 11 additions & 3 deletions application/classes/Ushahidi/Repository/Post/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,18 @@ protected function selectQuery(Array $where = [])
}

// PostValueRepository
public function getValueQuery($form_attribute_id, $match)
public function getValueQuery($form_attribute_id, array $matches)
{
return $this->selectQuery(compact('form_attribute_id'))
->where('tag_id', 'LIKE', "%$match%");
$query = $this->selectQuery(compact('form_attribute_id'))
->and_where_open();

foreach ($matches as $match) {
$query->or_where('tag_id', 'LIKE', "%$match%");
}

$query->and_where_close();

return $query;
}

// UpdatePostValueRepository
Expand Down
14 changes: 11 additions & 3 deletions application/classes/Ushahidi/Repository/Post/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,18 @@ public function deleteAllForPost($post_id)
}

// PostValueRepository
public function getValueQuery($form_attribute_id, $match)
public function getValueQuery($form_attribute_id, array $matches)
{
return $this->selectQuery(compact('form_attribute_id'))
->where('value', 'LIKE', "%$match%");
$query = $this->selectQuery(compact('form_attribute_id'))
->and_where_open();

foreach ($matches as $match) {
$query->or_where('value', 'LIKE', "%$match%");
}

$query->and_where_close();

return $query;
}

// PostValueRepository
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Entity/PostValueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get($id, $post_id = null, $form_attribute_id = null);
* @param string $match
* @return Database_Query
*/
public function getValueQuery($form_attribute_id, $match);
public function getValueQuery($form_attribute_id, array $matches);

/**
* Get the table name for use in joins
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/posts.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,20 @@ Feature: Testing the Posts API
And the "count" property equals "1"
Then the guzzle status code should be 200

@resetFixture @search
Scenario: Search All Posts by attribute
Given that I want to get all "Posts"
And that the request "query string" is:
"""
values[test_varchar][]=special&values[test_varchar][]=things
"""
When I request "/posts"
Then the response is JSON
And the response has a "count" property
And the type of the "count" property is "numeric"
And the "count" property equals "1"
Then the guzzle status code should be 200

@resetFixture @search
Scenario: Search All Posts by single tag
Given that I want to get all "Posts"
Expand Down

0 comments on commit f45fc00

Please sign in to comment.