Skip to content

Commit

Permalink
Merge pull request #581 from TheRestartProject/RES-1808_more_api_info
Browse files Browse the repository at this point in the history
RES-1808 Add includeDetails parameter to network API
  • Loading branch information
ngm committed Nov 14, 2022
2 parents 581f92d + 6803bbc commit 91ae0a6
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 42 deletions.
75 changes: 61 additions & 14 deletions app/Http/Controllers/API/NetworkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,37 @@ public function getNetworkv2($id)
* type="boolean"
* )
* ),
* @OA\Parameter(
* name="includeDetails",
* description="Include the details for each group. This makes the call significantly slower. Default false.",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* )
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(
* property="data",
* title="data",
* description="An array of groups",
* type="array",
* @OA\Items(
* ref="#/components/schemas/GroupSummary"
* )
* property="data",
* title="data",
* description="An array of groups",
* oneOf={
* @OA\Schema(
* type="array",
* @OA\Items(
* ref="#/components/schemas/GroupSummary"
* )
* ),
* @OA\Schema(
* type="array",
* @OA\Items(
* ref="#/components/schemas/Group"
* )
* ),
* },
* )
* )
* ),
Expand All @@ -160,15 +179,20 @@ public function getNetworkGroupsv2(Request $request, $id)
->where('groups.updated_at', '>=', $start)
->where('groups.updated_at', '<=', $end)->get();

return \App\Http\Resources\GroupSummaryCollection::make($groups);

if ($request->get('includeDetails', false)) {
return \App\Http\Resources\GroupCollection::make($groups);
} else {
return \App\Http\Resources\GroupSummaryCollection::make($groups);
}
}

/**
* @OA\Get(
* path="/api/v2/networks/{id}/events",
* operationId="getNetworkEvents",
* tags={"Networks"},
* summary="Get Network Groups",
* summary="Get Network Events",
* description="Returns list of events for a network.",
* @OA\Parameter(
* name="id",
Expand Down Expand Up @@ -199,6 +223,15 @@ public function getNetworkGroupsv2(Request $request, $id)
* example="2022-09-18T12:30:00+00:00"
* )
* ),
* @OA\Parameter(
* name="includeDetails",
* description="Include the details for each event. This makes the call significantly slower. Default false.",
* required=false,
* in="query",
* @OA\Schema(
* type="boolean"
* )
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
Expand All @@ -207,10 +240,20 @@ public function getNetworkGroupsv2(Request $request, $id)
* property="data",
* title="data",
* description="An array of events",
* type="array",
* @OA\Items(
* ref="#/components/schemas/EventSummary"
* )
* oneOf={
* @OA\Schema(
* type="array",
* @OA\Items(
* ref="#/components/schemas/EventSummary"
* )
* ),
* @OA\Schema(
* type="array",
* @OA\Items(
* ref="#/components/schemas/Event"
* )
* ),
* },
* )
* )
* ),
Expand Down Expand Up @@ -242,6 +285,10 @@ public function getNetworkEventsv2(Request $request, $id)
->select('events.*')
->get();

return \App\Http\Resources\PartySummaryCollection::make($events);
if ($request->get('includeDetails', false)) {
return \App\Http\Resources\PartyCollection::make($events);
} else {
return \App\Http\Resources\PartySummaryCollection::make($events);
}
}
}
20 changes: 20 additions & 0 deletions app/Http/Resources/PartyCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

/**
* @OA\Schema(
* title="EventCollection",
* schema="EventCollection",
* description="A collection of events.",
* @OA\Xml(
* name="EventCollection"
* ),
* )
*/

class PartyCollection extends ResourceCollection
{
}
2 changes: 1 addition & 1 deletion app/Network.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function eventsRequiringModeration()
return $events->flatten(1);
}

public function logo($size)
public function sizedLogo($size)
{
$logo = preg_replace('/\\.([^.\\s]{3,4})$/', "-$size.$1", $this->logo);
return $logo;
Expand Down
4 changes: 2 additions & 2 deletions resources/views/networks/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@foreach($yourNetworks as $network)
<tr>
<td>
@php( $logo = $network->logo('_x100') )
@php( $logo = $network->sizedLogo('_x100') )
@if( $logo )
<img style="width: auto; height:50px" src="{{ asset("/uploads/$logo") }}" alt="{{{ $network->name }}} logo">
@else
Expand Down Expand Up @@ -79,7 +79,7 @@
@foreach($allNetworks as $network)
<tr>
<td>
@php( $logo = $network->logo('_x100') )
@php( $logo = $network->sizedLogo('_x100') )
@if( $logo )
<img style="width: auto; height:50px" src="{{ asset("/uploads/$logo") }}" alt="{{{ $network->name }}} logo">
@else
Expand Down
2 changes: 1 addition & 1 deletion resources/views/networks/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="row">
<div class="col-lg-3 align-self-center" style="text-align:center">
<div class="network-icon">
@php( $logo = $network->logo('_x100') )
@php( $logo = $network->sizedLogo('_x100') )
@if( $logo )
<img style="max-width: 100%; max-height:50px" src="{{ asset("/uploads/$logo") }}" alt="{{{ $network->name }}} logo">
@else
Expand Down
75 changes: 51 additions & 24 deletions tests/Feature/Networks/APIv2NetworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function testGet() {
}

/**
* @dataProvider providerTrueFalse
* @dataProvider providerGroupsParameters
* @param $value
*/
public function testListGroups($getNextEvent) {
public function testListGroups($getNextEvent, $getDetails) {
$network = factory(Network::class)->create([
'name' => 'Restart',
'events_push_to_wordpress' => true,
Expand All @@ -82,29 +82,35 @@ public function testListGroups($getNextEvent) {
]);

// List networks.
$response = $this->get("/api/v2/networks/{$network->id}/groups?" . ($getNextEvent ? 'includeNextEvent=true' : ''));

try {
$response->assertSuccessful();
$json = json_decode($response->getContent(), true)['data'];
$this->assertEquals(1, count($json));
$this->assertEquals($group->idgroups, $json[0]['id']);
$this->assertEquals($group->name, $json[0]['name']);
$this->assertTrue(array_key_exists('location', $json[0]));
$location = $json[0]['location'];
$this->assertEquals($group->location, $location['location']);
$this->assertEquals($group->country, $location['country']);
$this->assertEquals($group->area, $location['area']);
$this->assertEquals($group->latitude, $location['lat']);
$this->assertEquals($group->longitude, $location['lng']);
$url = "/api/v2/networks/{$network->id}/groups?" .
($getNextEvent ? '&includeNextEvent=true' : '') .
($getDetails ? '&includeDetails=true' : '');
$response = $this->get($url);

$response->assertSuccessful();
$json = json_decode($response->getContent(), true)['data'];
$this->assertEquals(1, count($json));
$this->assertEquals($group->idgroups, $json[0]['id']);
$this->assertEquals($group->name, $json[0]['name']);
$this->assertTrue(array_key_exists('location', $json[0]));
$location = $json[0]['location'];
$this->assertEquals($group->location, $location['location']);
$this->assertEquals($group->country, $location['country']);
$this->assertEquals($group->area, $location['area']);
$this->assertEquals($group->latitude, $location['lat']);
$this->assertEquals($group->longitude, $location['lng']);

if ($getDetails) {
$this->assertNotNull($json[0]['description']);
$this->assertEquals($event->idevents, $json[0]['next_event']['id']);
} else {
$this->assertFalse(array_key_exists('description', $json[0]));

if ($getNextEvent) {
$this->assertEquals($event->idevents, $json[0]['next_event']['id']);
} else {
$this->assertFalse(array_key_exists('next_event', $json[0]));
}
} catch (\Exception $e) {
error_log($e->getMessage());
}

// Test updated_at filters.
Expand All @@ -123,14 +129,20 @@ public function testListGroups($getNextEvent) {
$this->assertEquals(0, count($json));
}

public function providerTrueFalse() {
public function providerGroupsParameters() {
return [
[false],
[true],
[false, false],
[false, true],
[true, false],
[true, true],
];
}

public function testListEvents() {
/**
* @dataProvider providerEventsParameters
* @param $value
*/
public function testListEvents($getDetails) {
$network = factory(Network::class)->create([
'name' => 'Restart',
'events_push_to_wordpress' => true,
Expand All @@ -151,14 +163,22 @@ public function testListEvents() {
DB::statement(DB::raw("UPDATE events SET updated_at = '2011-01-01 12:34'"));
DB::statement(DB::raw("UPDATE `groups` SET updated_at = '2011-01-02 12:34'"));

$response = $this->get("/api/v2/networks/{$network->id}/events");
$url = "/api/v2/networks/{$network->id}/events" .
($getDetails ? '?includeDetails=true' : '');
$response = $this->get($url);
$response->assertSuccessful();
$json = json_decode($response->getContent(), true)['data'];
$this->assertEquals(1, count($json));
$this->assertEquals($event->idevents, $json[0]['id']);
$this->assertEquals('2011-01-01T12:34:00+00:00', $json[0]['updated_at']);
$this->assertEquals('2011-01-02T12:34:00+00:00', $json[0]['group']['updated_at']);

if ($getDetails) {
$this->assertNotNull($json[0]['description']);
} else {
$this->assertFalse(array_key_exists('description', $json[0]));
}

# Test updated filters.
$start = '2011-01-01T10:34:00+00:00';
$end = '2011-01-01T14:34:00+00:00';
Expand All @@ -174,4 +194,11 @@ public function testListEvents() {
$json = json_decode($response->getContent(), true)['data'];
$this->assertEquals(0, count($json));
}

public function providerEventsParameters() {
return [
[false],
[true],
];
}
}

0 comments on commit 91ae0a6

Please sign in to comment.