Added tag endpoints

This commit is contained in:
Constantin Graf
2024-02-27 22:12:48 +01:00
parent 524dc0727d
commit a86e72f655
19 changed files with 824 additions and 138 deletions

View File

@@ -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\Tag\TagCollection;
use App\Http\Resources\V1\Tag\TagResource;
use App\Models\Organization;
use App\Models\Tag;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
class TagController extends Controller
{
protected function checkPermission(Organization $organization, string $permission, ?Tag $tag = null): void
{
parent::checkPermission($organization, $permission);
if ($tag !== null && $tag->organization_id !== $organization->getKey()) {
throw new AuthorizationException('Tag does not belong to organization');
}
}
/**
* Get tags
*
* @throws AuthorizationException
*/
public function index(Organization $organization): TagCollection
{
$this->checkPermission($organization, 'tags:view');
$tags = Tag::query()
->whereBelongsTo($organization, 'organization')
->orderBy('created_at', 'desc')
->get();
return new TagCollection($tags);
}
/**
* Create tag
*
* @throws AuthorizationException
*/
public function store(Organization $organization, TagStoreRequest $request): TagResource
{
$this->checkPermission($organization, 'tags:create');
$tag = new Tag();
$tag->name = $request->input('name');
$tag->organization()->associate($organization);
$tag->save();
return new TagResource($tag);
}
/**
* Update tag
*
* @throws AuthorizationException
*/
public function update(Organization $organization, Tag $tag, TagUpdateRequest $request): TagResource
{
$this->checkPermission($organization, 'tags:update', $tag);
$tag->name = $request->input('name');
$tag->save();
return new TagResource($tag);
}
/**
* Delete tag
*
* @throws AuthorizationException
*/
public function destroy(Organization $organization, Tag $tag): JsonResponse
{
$this->checkPermission($organization, 'tags:delete', $tag);
$tag->delete();
return response()->json(null, 204);
}
}

View File

@@ -114,11 +114,13 @@ class TimeEntryController extends Controller
if ($request->get('end') === null && TimeEntry::query()->where('user_id', $request->get('user_id'))->where('end', null)->exists()) {
// TODO: API documentation
// TODO: Create concept for api exceptions
throw new TimeEntryStillRunning('User already has an active time entry');
}
$timeEntry = new TimeEntry();
$timeEntry->fill($request->validated());
$timeEntry->description = $request->get('description', '');
$timeEntry->organization()->associate($organization);
$timeEntry->save();