Skip to content

Commit

Permalink
Events + Org Api
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdankharchenko committed Oct 31, 2023
1 parent 7411bf3 commit d4b996d
Show file tree
Hide file tree
Showing 55 changed files with 3,125 additions and 400 deletions.
24 changes: 24 additions & 0 deletions app-modules/api/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "hack-greenville/api",
"description": "",
"type": "library",
"version": "1.0",
"license": "proprietary",
"require": {},
"autoload": {
"psr-4": {
"HackGreenville\\Api\\": "src/",
"HackGreenville\\Api\\Tests\\": "tests/",
"HackGreenville\\Api\\Database\\Factories\\": "database/factories/",
"HackGreenville\\Api\\Database\\Seeders\\": "database/seeders/"
}
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"HackGreenville\\Api\\Providers\\ApiServiceProvider"
]
}
}
}
10 changes: 10 additions & 0 deletions app-modules/api/routes/api-routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use HackGreenville\Api\Http\Controllers\EventApiV0Controller;
use HackGreenville\Api\Http\Controllers\OrgsApiV0Controller;

Route::prefix('api/')
->group(function () {
Route::get('v0/events', EventApiV0Controller::class)->name('api.v0.events.index');
Route::get('v0/orgs', OrgsApiV0Controller::class)->name('api.v0.orgs.index');
});
16 changes: 16 additions & 0 deletions app-modules/api/src/Http/Controllers/EventApiV0Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace HackGreenville\Api\Http\Controllers;

use App\Models\Event;
use HackGreenville\Api\Resources\Events\V0\EventCollection;

class EventApiV0Controller
{
public function __invoke()
{
return new EventCollection(
resource: Event::query()->latest()->get()
);
}
}
17 changes: 17 additions & 0 deletions app-modules/api/src/Http/Controllers/OrgsApiV0Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace HackGreenville\Api\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Org;
use HackGreenville\Api\Resources\Orgs\V0\OrganizationsCollection;

class OrgsApiV0Controller extends Controller
{
public function __invoke()
{
return new OrganizationsCollection(
resource: Org::all()
);
}
}
16 changes: 16 additions & 0 deletions app-modules/api/src/Providers/ApiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace HackGreenville\Api\Providers;

use Illuminate\Support\ServiceProvider;

class ApiServiceProvider extends ServiceProvider
{
public function register()
{
}

public function boot()
{
}
}
26 changes: 26 additions & 0 deletions app-modules/api/src/Resources/Events/V0/EventCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace HackGreenville\Api\Resources\Events\V0;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

/**
* This exists merely to handle legacy events api.
*/
class EventCollection extends ResourceCollection
{
public static $wrap = null;

public $collects = EventResource::class;

public function toArray(Request $request): array
{
return $this->collection->toArray();
}

public function paginationInformation($request, $paginated, $default)
{
return [];
}
}
35 changes: 35 additions & 0 deletions app-modules/api/src/Resources/Events/V0/EventResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace HackGreenville\Api\Resources\Events\V0;

use App\Models\Event;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class EventResource extends JsonResource
{
/** @var Event $resource */
public $resource;

public function toArray(Request $request): array
{
return [
'event_name' => $this->resource->event_name,
'group_name' => $this->resource->group_name,
'group_url' => 'TBD', // TODO
'url' => $this->resource->url,
'time' => $this->resource->active_at->toISOString(),
'tags' => '1', // TODO,
'nid' => '1', // TODO,
'status' => $this->resource->getStatusAttribute(),
'rsvp_count' => $this->resource->rsvp_count,
'description' => $this->resource->description,
'uuid' => $this->resource->event_uuid,
'data_as_of' => now()->toISOString(),
'service_id' => $this->resource->service_id,
'service' => $this->resource->service,
'venue' => new VenueResource($this->resource->venue),
'created_at' => $this->resource->created_at->toISOString(),
];
}
}
27 changes: 27 additions & 0 deletions app-modules/api/src/Resources/Events/V0/VenueResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace HackGreenville\Api\Resources\Events\V0;

use App\Models\Venue;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class VenueResource extends JsonResource
{
/** @var Venue $resource */
public $resource;

public function toArray(Request $request): array
{
return [
'name' => $this->resource->name,
'address' => $this->resource->address,
'city' => $this->resource->city,
'state' => $this->resource->state->abbr,
'zip' => $this->resource->zipcode,
'country' => $this->resource->country,
'lat' => $this->resource->lat,
'lon' => $this->resource->lon,
];
}
}
33 changes: 33 additions & 0 deletions app-modules/api/src/Resources/Orgs/V0/OrgResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace HackGreenville\Api\Resources\Orgs\V0;

use App\Models\Org;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class OrgResource extends JsonResource
{
/** @var Org $resource */
public $resource;

public function toArray(Request $request): array
{
return [
'title' => $this->resource->title,
'path' => $this->resource->path,
'changed' => $this->resource->updated_at->toISOString(),
'field_city' => $this->resource->city,
'field_event_service' => $this->resource->service,
'field_events_api_key' => $this->resource->service_api_key,
'field_focus_area' => $this->resource->focus_area,
'field_homepage' => $this->resource->home_page,
'field_event_calendar_homepage' => $this->resource->event_calendar_uri,
'field_primary_contact_person' => $this->resource->primary_contact_person,
'field_org_status' => $this->resource->status,
'field_organization_type' => $this->resource->organization_type,
'field_year_established' => $this->resource->established_at->year,
'uuid' => $this->resource->id,
];
}
}
21 changes: 21 additions & 0 deletions app-modules/api/src/Resources/Orgs/V0/OrganizationsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace HackGreenville\Api\Resources\Orgs\V0;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

/**
* This exists merely to handle legacy events api.
*/
class OrganizationsCollection extends ResourceCollection
{
public $collects = OrgResource::class;

public static $wrap = null;

public function toArray(Request $request): array
{
return $this->collection->toArray();
}
}
51 changes: 51 additions & 0 deletions app-modules/api/tests/Feature/EventApiV0Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace HackGreenville\Api\Tests\Feature;

use App\Models\Event;
use Tests\DatabaseTestCase;

class EventApiV0Test extends DatabaseTestCase
{
public function test_event_api_v0_return_correct_data(): void
{
// Lock-in the time
$this->travelTo(now());

$event = Event::factory()->create([
'cancelled_at' => now(),
]);

$this->getJson(route('api.v0.events.index'))
->assertSessionDoesntHaveErrors()
->assertExactJson([
[
'event_name' => $event->event_name,
'group_name' => $event->group_name,
'group_url' => 'TBD', //FIXME
'url' => $event->url,
'time' => $event->active_at->toISOString(),
'tags' => '1', // TODO,
'nid' => '1', // TODO,
'rsvp_count' => $event->rsvp_count,
'created_at' => $event->created_at->toISOString(),
'description' => $event->description,
'uuid' => $event->event_uuid,
'data_as_of' => now()->toISOString(),
'status' => 'cancelled',
'service_id' => $event->service_id,
'service' => $event->service->value,
'venue' => [
'name' => $event->venue->name,
'address' => $event->venue->address,
'city' => $event->venue->city,
'state' => $event->venue->state->abbr,
'zip' => $event->venue->zipcode,
'country' => $event->venue->country,
'lat' => $event->venue->lat,
'lon' => $event->venue->lon,
],
],
]);
}
}
42 changes: 42 additions & 0 deletions app-modules/api/tests/Feature/OrganizationsApiV0Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace HackGreenville\Api\Tests\Feature;

use App\Models\Org;
use Tests\DatabaseTestCase;

class OrganizationsApiV0Test extends DatabaseTestCase
{
public function test_organization_api_v0_return_correct_data(): void
{
// Lock-in the time
$this->travelTo(now());

$org = Org::factory()->create();

$this->getJson(route('api.v0.orgs.index'))
->assertSessionDoesntHaveErrors()
->assertExactJson([
[
'title' => $org->title,
'path' => $org->path,
'changed' => $org->updated_at->toISOString(),
'field_city' => $org->city,
'field_event_service' => $org->service,
'field_events_api_key' => $org->service,
'field_focus_area' => $org->focus_area,
'field_homepage' => $org->home_page,
'field_org_status' => $org->status->value,
'field_primary_contact_person' => $org->primary_contact_person,
'field_organization_type' => $org->organization_type,
'field_event_calendar_homepage' => $org->event_calendar_uri,
// 'field_regular_day_of_the_month' => '', // TBD
// 'field_regular_start_time' => '', // TBD
'field_year_established' => $org->established_at->year,
// 'field_year_inactive' => '', // TBD
'uuid' => $org->id,
// 'field_org_tags' => '', // TBD
],
]);
}
}
24 changes: 24 additions & 0 deletions app-modules/event-importer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "hack-greenville/event-importer",
"description": "",
"type": "library",
"version": "1.0",
"license": "proprietary",
"require": {},
"autoload": {
"psr-4": {
"HackGreenville\\EventImporter\\": "src/",
"HackGreenville\\EventImporter\\Tests\\": "tests/",
"HackGreenville\\EventImporter\\Database\\Factories\\": "database/factories/",
"HackGreenville\\EventImporter\\Database\\Seeders\\": "database/seeders/"
}
},
"minimum-stability": "stable",
"extra": {
"laravel": {
"providers": [
"HackGreenville\\EventImporter\\Providers\\EventImporterServiceProvider"
]
}
}
}
Loading

0 comments on commit d4b996d

Please sign in to comment.