mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-14 11:12:16 +01:00
Added tag endpoints
This commit is contained in:
88
app/Http/Controllers/Api/V1/TagController.php
Normal file
88
app/Http/Controllers/Api/V1/TagController.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ class ProjectStoreRequest extends FormRequest
|
||||
'name' => [
|
||||
'required',
|
||||
'string',
|
||||
'min:1',
|
||||
'max:255',
|
||||
],
|
||||
'color' => [
|
||||
|
||||
28
app/Http/Requests/V1/Tag/TagStoreRequest.php
Normal file
28
app/Http/Requests/V1/Tag/TagStoreRequest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\V1\Tag;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class TagStoreRequest 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',
|
||||
'min:1',
|
||||
'max:255',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
28
app/Http/Requests/V1/Tag/TagUpdateRequest.php
Normal file
28
app/Http/Requests/V1/Tag/TagUpdateRequest.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Requests\V1\Tag;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class TagUpdateRequest 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',
|
||||
'min:1',
|
||||
'max:255',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -52,13 +52,12 @@ class TimeEntryStoreRequest extends FormRequest
|
||||
// Start of time entry (ISO 8601 format, UTC timezone)
|
||||
'start' => [
|
||||
'required',
|
||||
'date', // TODO
|
||||
'date_format:Y-m-d\TH:i:s\Z',
|
||||
],
|
||||
// End of time entry (ISO 8601 format, UTC timezone)
|
||||
'end' => [
|
||||
'required',
|
||||
'nullable',
|
||||
'date', // TODO
|
||||
'date_format:Y-m-d\TH:i:s\Z',
|
||||
'after:start',
|
||||
],
|
||||
// Description of time entry
|
||||
|
||||
17
app/Http/Resources/V1/Tag/TagCollection.php
Normal file
17
app/Http/Resources/V1/Tag/TagCollection.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\V1\Tag;
|
||||
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class TagCollection extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* The resource that this resource collects.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $collects = TagResource::class;
|
||||
}
|
||||
34
app/Http/Resources/V1/Tag/TagResource.php
Normal file
34
app/Http/Resources/V1/Tag/TagResource.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Resources\V1\Tag;
|
||||
|
||||
use App\Http\Resources\V1\BaseResource;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* @property Tag $resource
|
||||
*/
|
||||
class TagResource 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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user