Skip to content

Commit

Permalink
Add bounding box parameter to /api/groups/network.
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Aug 17, 2021
1 parent 801dfa1 commit 06b330d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 67 deletions.
151 changes: 85 additions & 66 deletions app/Http/Controllers/API/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ public static function getGroupsByUsersNetworks(Request $request)
{
$authenticatedUser = Auth::user();

$bbox = $minLat = $minLng = $maxLat = $maxLng = null;

if ($request->has('bbox')) {
$bbox = $request->get('bbox');
if (preg_match('/(.*?),(.*?),(.*?),(.*)/', $bbox, $matches)) {
$minLat = floatval($matches[1]);
$minLng = floatval($matches[2]);
$maxLat = floatval($matches[3]);
$maxLng = floatval($matches[4]);
}
}

$groups = [];

foreach ($authenticatedUser->networks as $network) {
Expand All @@ -72,72 +84,79 @@ public static function getGroupsByUsersNetworks(Request $request)
$collection = collect([]);

foreach ($groups as $group) {
$groupStats = $group->getGroupStats();
$collection->push([
'id' => $group->idgroups,
'name' => $group->name,
'location' => [
'value' => $group->location,
'country' => $group->country,
'latitude' => $group->latitude,
'longitude' => $group->longitude,
'area' => $group->area,
'postcode' => $group->postcode,
],
'website' => $group->website,
'facebook' => $group->facebook,
'description' => $group->free_text,
'image_url' => $group->groupImagePath(),
'upcoming_parties' => $upcoming_parties_collection = collect([]),
'past_parties' => $past_parties_collection = collect([]),
'impact' => [
'volunteers' => $groupStats['pax'],
'hours_volunteered' => $groupStats['hours'],
'parties_thrown' => $groupStats['parties'],
'waste_prevented' => $groupStats['waste'],
'co2_emissions_prevented' => $groupStats['co2'],
],
'widgets' => [
'headline_stats' => url("/group/stats/{$group->idgroups}"),
'co2_equivalence_visualisation' => url("/outbound/info/group/{$group->idgroups}/manufacture"),
],
'created_at' => new \Carbon\Carbon($group->created_at),
'updated_at' => new \Carbon\Carbon($group->max_updated_at_devices_updated_at),

]);

foreach ($group->upcomingParties() as $event) {
$upcoming_parties_collection->push([
'event_id' => $event->idevents,
'event_date' => $event->event_date,
'start_time' => $event->start,
'end_time' => $event->end,
'name' => $event->venue,
'location' => [
'value' => $event->location,
'latitude' => $event->latitude,
'longitude' => $event->longitude,
],
'created_at' => $event->created_at,
'updated_at' => $event->updated_at,
]);
}

foreach ($group->pastParties() as $key => $event) {
$past_parties_collection->push([
'event_id' => $event->idevents,
'event_date' => $event->event_date,
'start_time' => $event->start,
'end_time' => $event->end,
'name' => $event->venue,
'location' => [
'value' => $event->location,
'latitude' => $event->latitude,
'longitude' => $event->longitude,
],
'created_at' => $event->created_at,
'updated_at' => $event->updated_at,
]);
// If we have a bounding box, check that the group is within it.
if (!$bbox || (
$group->latitude !== null && $group->longitude !== null &&
$group->latitude >= $minLat && $group->latitude <= $maxLat &&
$group->longitude >= $minLng && $group->longitude <= $maxLng
)) {
$groupStats = $group->getGroupStats();
$collection->push([
'id' => $group->idgroups,
'name' => $group->name,
'location' => [
'value' => $group->location,
'country' => $group->country,
'latitude' => $group->latitude,
'longitude' => $group->longitude,
'area' => $group->area,
'postcode' => $group->postcode,
],
'website' => $group->website,
'facebook' => $group->facebook,
'description' => $group->free_text,
'image_url' => $group->groupImagePath(),
'upcoming_parties' => $upcoming_parties_collection = collect([]),
'past_parties' => $past_parties_collection = collect([]),
'impact' => [
'volunteers' => $groupStats['pax'],
'hours_volunteered' => $groupStats['hours'],
'parties_thrown' => $groupStats['parties'],
'waste_prevented' => $groupStats['waste'],
'co2_emissions_prevented' => $groupStats['co2'],
],
'widgets' => [
'headline_stats' => url("/group/stats/{$group->idgroups}"),
'co2_equivalence_visualisation' => url("/outbound/info/group/{$group->idgroups}/manufacture"),
],
'created_at' => new \Carbon\Carbon($group->created_at),
'updated_at' => new \Carbon\Carbon($group->max_updated_at_devices_updated_at),

]);

foreach ($group->upcomingParties() as $event) {
$upcoming_parties_collection->push([
'event_id' => $event->idevents,
'event_date' => $event->event_date,
'start_time' => $event->start,
'end_time' => $event->end,
'name' => $event->venue,
'location' => [
'value' => $event->location,
'latitude' => $event->latitude,
'longitude' => $event->longitude,
],
'created_at' => $event->created_at,
'updated_at' => $event->updated_at,
]);
}

foreach ($group->pastParties() as $key => $event) {
$past_parties_collection->push([
'event_id' => $event->idevents,
'event_date' => $event->event_date,
'start_time' => $event->start,
'end_time' => $event->end,
'name' => $event->venue,
'location' => [
'value' => $event->location,
'latitude' => $event->latitude,
'longitude' => $event->longitude,
],
'created_at' => $event->created_at,
'updated_at' => $event->updated_at,
]);
}
}
}

Expand Down
30 changes: 29 additions & 1 deletion tests/Feature/Networks/NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,41 @@ public function admins_can_associate_group_to_network()
$admin = factory(User::class)->states('Administrator')->create();
$this->actingAs($admin);

$group = factory(Group::class)->create();
$group = factory(Group::class)->create([
'latitude' => 51.5074,
'longitude' => -0.1278
]);
$network = factory(Network::class)->create();

$this->networkService->addGroupToNetwork($admin, $group, $network);

$this->assertTrue($network->containsGroup($group));
$this->assertTrue($group->isMemberOf($network));

// Check the group shows up in the list of groups for this network.
$coordinator = factory(User::class)->states('NetworkCoordinator')->create([
'api_token' => '1234',
]);
$network->addCoordinator($coordinator);
$this->actingAs($coordinator);

$response = $this->get('/api/groups/network?api_token=1234');
$groups = json_decode($response->getContent(), TRUE);
$this->assertEquals(1, count($groups));
$this->assertEquals($group->idgroups, $groups[0]['id']);
$this->assertEquals($group->name, $groups[0]['name']);

// Get again with a bounding box which the group is inside.
$response = $this->get('/api/groups/network?api_token=1234&bbox=' . urlencode('51.5,-0.13,51.51,-0.12'));
$groups = json_decode($response->getContent(), TRUE);
$this->assertEquals(1, count($groups));
$this->assertEquals($group->idgroups, $groups[0]['id']);
$this->assertEquals($group->name, $groups[0]['name']);

// Get again with a bounding box which the group is outside.
$response = $this->get('/api/groups/network?api_token=1234&bbox=' . urlencode('51.5,-0.13,51.505,-0.12'));
$groups = json_decode($response->getContent(), TRUE);
$this->assertEquals(0, count($groups));
}

/** @test */
Expand Down

0 comments on commit 06b330d

Please sign in to comment.