-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added client and organization endpoints
- Loading branch information
Showing
18 changed files
with
679 additions
and
17 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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Requests\V1\Tag\TagStoreRequest; | ||
use App\Http\Requests\V1\Tag\TagUpdateRequest; | ||
use App\Http\Resources\V1\Client\ClientCollection; | ||
use App\Http\Resources\V1\Client\ClientResource; | ||
use App\Models\Client; | ||
use App\Models\Organization; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ClientController extends Controller | ||
{ | ||
protected function checkPermission(Organization $organization, string $permission, ?Client $client = null): void | ||
{ | ||
parent::checkPermission($organization, $permission); | ||
if ($client !== null && $client->organization_id !== $organization->getKey()) { | ||
throw new AuthorizationException('Tag does not belong to organization'); | ||
} | ||
} | ||
|
||
/** | ||
* Get clients | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function index(Organization $organization): ClientCollection | ||
{ | ||
$this->checkPermission($organization, 'clients:view'); | ||
|
||
$clients = Client::query() | ||
->whereBelongsTo($organization, 'organization') | ||
->orderBy('created_at', 'desc') | ||
->get(); | ||
|
||
return new ClientCollection($clients); | ||
} | ||
|
||
/** | ||
* Create client | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function store(Organization $organization, TagStoreRequest $request): ClientResource | ||
{ | ||
$this->checkPermission($organization, 'clients:create'); | ||
|
||
$client = new Client(); | ||
$client->name = $request->input('name'); | ||
$client->organization()->associate($organization); | ||
$client->save(); | ||
|
||
return new ClientResource($client); | ||
} | ||
|
||
/** | ||
* Update client | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function update(Organization $organization, Client $client, TagUpdateRequest $request): ClientResource | ||
{ | ||
$this->checkPermission($organization, 'clients:update', $client); | ||
|
||
$client->name = $request->input('name'); | ||
$client->save(); | ||
|
||
return new ClientResource($client); | ||
} | ||
|
||
/** | ||
* Delete client | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function destroy(Organization $organization, Client $client): JsonResponse | ||
{ | ||
$this->checkPermission($organization, 'clients:delete', $client); | ||
|
||
$client->delete(); | ||
|
||
return response()->json(null, 204); | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Requests\V1\Organization\OrganizationUpdateRequest; | ||
use App\Http\Resources\V1\Organization\OrganizationResource; | ||
use App\Models\Organization; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
|
||
class OrganizationController extends Controller | ||
{ | ||
/** | ||
* Get organization | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function show(Organization $organization): OrganizationResource | ||
{ | ||
$this->checkPermission($organization, 'organizations:view'); | ||
|
||
return new OrganizationResource($organization); | ||
} | ||
|
||
/** | ||
* Update organization | ||
* | ||
* @throws AuthorizationException | ||
*/ | ||
public function update(Organization $organization, OrganizationUpdateRequest $request): OrganizationResource | ||
{ | ||
$this->checkPermission($organization, 'organizations:update'); | ||
|
||
$organization->name = $request->input('name'); | ||
$organization->save(); | ||
|
||
return new OrganizationResource($organization); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
app/Http/Requests/V1/Organization/OrganizationUpdateRequest.php
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests\V1\Organization; | ||
|
||
use App\Models\Organization; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
/** | ||
* @property Organization $organization Organization from model binding | ||
*/ | ||
class OrganizationUpdateRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, array<string|ValidationRule>> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'name' => [ | ||
'required', | ||
'string', | ||
'max:255', | ||
], | ||
]; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\Client; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class ClientCollection extends ResourceCollection | ||
{ | ||
/** | ||
* The resource that this resource collects. | ||
* | ||
* @var string | ||
*/ | ||
public $collects = ClientResource::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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\Client; | ||
|
||
use App\Http\Resources\V1\BaseResource; | ||
use App\Models\Client; | ||
use Illuminate\Http\Request; | ||
|
||
/** | ||
* @property Client $resource | ||
*/ | ||
class ClientResource extends BaseResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, string|bool|int|null> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
/** @var string $id ID */ | ||
'id' => $this->resource->id, | ||
/** @var string $name Name */ | ||
'name' => $this->resource->name, | ||
/** @var string $created_at When the tag was created */ | ||
'created_at' => $this->formatDateTime($this->resource->created_at), | ||
/** @var string $updated_at When the tag was last updated */ | ||
'updated_at' => $this->formatDateTime($this->resource->updated_at), | ||
]; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/Http/Resources/V1/Organization/OrganizationResource.php
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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Resources\V1\Organization; | ||
|
||
use App\Http\Resources\V1\BaseResource; | ||
use App\Models\Organization; | ||
use Illuminate\Http\Request; | ||
|
||
/** | ||
* @property Organization $resource | ||
*/ | ||
class OrganizationResource extends BaseResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, string|bool|int|null> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
/** @var string $id ID */ | ||
'id' => $this->resource->id, | ||
/** @var string $name Name */ | ||
'name' => $this->resource->name, | ||
/** @var string $color Personal organizations automatically created after registration */ | ||
'is_personal' => $this->resource->personal_team, | ||
]; | ||
} | ||
} |
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
Oops, something went wrong.