forked from UnlockedLabs/UnlockEdv2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request UnlockedLabs#29 from chrissantillan/PP-CRUD
feat: add ProviderPlaform CRUD functionality UN-183
- Loading branch information
Showing
7 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |