Skip to content

Commit

Permalink
Merge pull request UnlockedLabs#29 from chrissantillan/PP-CRUD
Browse files Browse the repository at this point in the history
feat: add ProviderPlaform CRUD functionality UN-183
  • Loading branch information
chrissantillan authored Dec 14, 2023
2 parents 4d1ba72 + 17bbac8 commit 9eb99c5
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 0 deletions.
80 changes: 80 additions & 0 deletions app/Http/Controllers/v1/ProviderPlatformController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Http\Controllers\v1;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreProviderPlatformRequest;
use App\Http\Requests\UpdateProviderPlatformRequest;
use App\Http\Resources\PaginateResource;
use App\Http\Resources\ProviderPlatformResource;
use App\Models\ProviderPlatform;

class ProviderPlatformController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$providerPlatforms = ProviderPlatform::paginate(10);

return PaginateResource::make($providerPlatforms, ProviderPlatformResource::class);
}

/**
* Store a newly created resource in storage.
*/
public function store(StoreProviderPlatformRequest $request)
{
try {
$providerPlatform = $request->validated();
} catch (\Throwable $th) {
return response()->json([
'message' => 'Request Validation failed.',
'errors' => $th->getMessage(),
], 422);
}
$newProviderPlatform = ProviderPlatform::create($providerPlatform);
$newProviderPlatform->hashAccessKey();

return ProviderPlatformResource::make($newProviderPlatform);
}

/**
* Display the specified resource.
*/
public function show(ProviderPlatform $providerPlatform)
{
return ProviderPlatformResource::make($providerPlatform);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateProviderPlatformRequest $request, ProviderPlatform $providerPlatform)
{
try {
$validated = $request->validated();
} catch (\Throwable $th) {
return response()->json([
'message' => 'Request Validation failed.',
'errors' => $th->getMessage(),
], 422);
}
$providerPlatform = ProviderPlatform::findOrFail($providerPlatform->id);
$providerPlatform->update($validated);

return ProviderPlatformResource::make($providerPlatform);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(ProviderPlatform $providerPlatform)
{
$providerPlatform = ProviderPlatform::findOrFail($providerPlatform->id);
$providerPlatform->delete();

return response()->noContent();
}
}
39 changes: 39 additions & 0 deletions app/Http/Requests/StoreProviderPlatformRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Http\Requests;

use App\Enums\ProviderPlatformState;
use App\Enums\ProviderPlatformType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreProviderPlatformRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
//TODO: FIX THIS
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'type' => [Rule::enum(ProviderPlatformType::class)],
'description' => 'nullable|string|max:255',
'icon_url' => 'required|url:http,https',
'account_id' => 'required|unique:provider_platforms,account_id',
'access_key' => 'required|unique:provider_platforms,access_key',
'base_url' => 'required|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
];
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/UpdateProviderPlatformRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Requests;

use App\Enums\ProviderPlatformState;
use App\Enums\ProviderPlatformType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateProviderPlatformRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
//TODO: FIX THIS
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'nullable|string|max:255',
'type' => 'nullable',
'type' => [Rule::enum(ProviderPlatformType::class)],
'description' => 'nullable|string|max:255',
'icon_url' => 'nullable|url:http,https',
'account_id' => 'nullable',
'access_key' => 'nullable',
'base_url' => 'nullable|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Resources/ProviderPlatformResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Resources;

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

class ProviderPlatformResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
// return parent::toArray($request);
return [
'id' => $this->id,
'type' => $this->type,
'name' => $this->name,
'description' => $this->description,
'icon_url' => $this->icon_url,
'account_id' => $this->account_id,
'access_key' => $this->access_key,
'base_url' => $this->base_url,
'state' => $this->state,
];
}
}
10 changes: 10 additions & 0 deletions app/Models/ProviderPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\Enums\ProviderPlatformType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;

class ProviderPlatform extends Model
{
Expand All @@ -31,4 +33,12 @@ class ProviderPlatform extends Model
'type' => ProviderPlatformType::class,
'state' => ProviderPlatformState::class,
];

public function hashAccessKey()
{
$accessKey = $this->access_key;
$hashedAccessKey = Hash::make($accessKey);
$this->access_key = $hashedAccessKey;
$this->save();
}
}
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\v1\UserController;
use App\Http\Controllers\v1\ProviderPlatformController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -24,3 +25,7 @@
Route::post('/v1/users', [UserController::class, 'store']);
Route::patch('/v1/users/{id}', [UserController::class, 'update']);
Route::delete('/v1/users/{id}', [UserController::class, 'destroy']);

Route::prefix('v1')->group(function () {
Route::Resource('provider-platforms', ProviderPlatformController::class);
});
61 changes: 61 additions & 0 deletions tests/Feature/ProviderPlatformControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Tests\Feature;

use App\Models\ProviderPlatform;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ProviderPlatformControllerTest extends TestCase
{
use RefreshDatabase;

public string $uri = '/api/v1/provider-platforms';

public function testGetProviderPlatform()
{
$providerPlatform = ProviderPlatform::factory()->create();

$response = $this->get($this->uri.'/'.$providerPlatform->id);

$response->assertStatus(200);
}

public function testGetProviderPlatforms()
{
$response = $this->get($this->uri);
echo $response->getContent();
$response->assertStatus(200);
}

public function testCreateProviderPlatform()
{
$response = $this->post($this->uri.'/', [
'type' => 'canvas_cloud',
'name' => 'Test',
'description' => 'Test desciption',
'icon_url' => 'https://test.placeholder.com/640x480.png/0066cc?text=qui',
'account_id' => '123456789',
'access_key' => 'testaccesskey123',
'base_url' => 'http://testurl.org/qui-nesciunt-qui-expedita',
'state' => 'enabled',
]);
$response->assertStatus(201);
$response->assertCreated();
}

public function testUpdateProviderPlatform()
{
$providerPlatform = \App\Models\ProviderPlatform::factory()->create();
$response = $this->patch($this->uri.'/'.$providerPlatform->id, ['name' => 'TestUpdate']);
$response->assertStatus(200);
assert($response['data']['name'] == 'TestUpdate');
}

public function testDeleteProviderPlatform()
{
$providerPlatform = \App\Models\ProviderPlatform::factory()->create();
$response = $this->delete($this->uri.'/'.$providerPlatform->id);
$response->assertStatus(204);
}
}

0 comments on commit 9eb99c5

Please sign in to comment.