Skip to content

Commit

Permalink
Added client and organization endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Mar 4, 2024
1 parent b3f5507 commit 161da5b
Show file tree
Hide file tree
Showing 18 changed files with 679 additions and 17 deletions.
88 changes: 88 additions & 0 deletions app/Http/Controllers/Api/V1/ClientController.php
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);
}
}
40 changes: 40 additions & 0 deletions app/Http/Controllers/Api/V1/OrganizationController.php
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);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/V1/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function store(Organization $organization, ProjectStoreRequest $request):
$project = new Project();
$project->name = $request->input('name');
$project->color = $request->input('color');
$project->client_id = $request->input('client_id');
$project->organization()->associate($organization);
$project->save();

Expand Down
31 changes: 31 additions & 0 deletions app/Http/Requests/V1/Organization/OrganizationUpdateRequest.php
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',
],
];
}
}
14 changes: 14 additions & 0 deletions app/Http/Requests/V1/Project/ProjectStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace App\Http\Requests\V1\Project;

use App\Models\Client;
use App\Models\Organization;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;

/**
* @property Organization $organization Organization from model binding
*/
class ProjectStoreRequest extends FormRequest
{
/**
Expand All @@ -28,6 +35,13 @@ public function rules(): array
'string',
'max:255',
],
'client_id' => [
'nullable',
new ExistsEloquent(Client::class, null, function (Builder $builder): Builder {
/** @var Builder<Client> $builder */
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
];
}
}
14 changes: 14 additions & 0 deletions app/Http/Requests/V1/Project/ProjectUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

namespace App\Http\Requests\V1\Project;

use App\Models\Client;
use App\Models\Organization;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;

/**
* @property Organization $organization Organization from model binding
*/
class ProjectUpdateRequest extends FormRequest
{
/**
Expand All @@ -27,6 +34,13 @@ public function rules(): array
'string',
'max:255',
],
'client_id' => [
'nullable',
new ExistsEloquent(Client::class, null, function (Builder $builder): Builder {
/** @var Builder<Client> $builder */
return $builder->whereBelongsTo($this->organization, 'organization');
}),
],
];
}
}
17 changes: 17 additions & 0 deletions app/Http/Resources/V1/Client/ClientCollection.php
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;
}
34 changes: 34 additions & 0 deletions app/Http/Resources/V1/Client/ClientResource.php
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 app/Http/Resources/V1/Organization/OrganizationResource.php
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,
];
}
}
14 changes: 12 additions & 2 deletions app/Models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;

/**
* @property string $id
* @property string $name
* @property string $organization_id
* @property string $created_at
* @property string $updated_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Organization $organization
*
* @method static ClientFactory factory()
Expand All @@ -41,4 +43,12 @@ public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class, 'organization_id');
}

/**
* @return HasMany<Project>
*/
public function projects(): HasMany
{
return $this->hasMany(Project::class, 'client_id');
}
}
3 changes: 3 additions & 0 deletions app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

/**
* @property string $id
* @property string $name
* @property bool $personal_team
* @property User $owner
*
* @method HasMany<OrganizationInvitation> teamInvitations()
Expand All @@ -31,6 +33,7 @@ class Organization extends JetstreamTeam
* @var array<string, string>
*/
protected $casts = [
'name' => 'string',
'personal_team' => 'boolean',
];

Expand Down
8 changes: 8 additions & 0 deletions app/Providers/JetstreamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ protected function configurePermissions(): void
'tags:create',
'tags:update',
'tags:delete',
'clients:view',
'clients:create',
'clients:update',
'clients:delete',
'organizations:view',
'organizations:update',
])->description('Administrator users can perform any action.');

Jetstream::role('manager', 'Manager', [
Expand All @@ -87,6 +93,7 @@ protected function configurePermissions(): void
'tags:create',
'tags:update',
'tags:delete',
'organizations:view',
])->description('Editor users have the ability to read, create, and update.');

Jetstream::role('employee', 'Employee', [
Expand All @@ -96,6 +103,7 @@ protected function configurePermissions(): void
'time-entries:create:own',
'time-entries:update:own',
'time-entries:delete:own',
'organizations:view',
])->description('Editor users have the ability to read, create, and update.');
}
}
Loading

0 comments on commit 161da5b

Please sign in to comment.