mirror of https://github.com/Devoalda/LaDo.git
feature(Pomo API):
Added: - API Support for Pomos - Pomo API tests Modified: - Pomo Policy (Still needs to be fixed)
This commit is contained in:
parent
1d9397e738
commit
cff6d01985
|
@ -4,7 +4,12 @@ namespace App\Http\Controllers;
|
|||
|
||||
use App\Http\Requests\StorePomoRequest;
|
||||
use App\Http\Requests\UpdatePomoRequest;
|
||||
use App\Http\Resources\PomoResource;
|
||||
use Illuminate\Auth\Access\AuthorizationException;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Models\{
|
||||
Pomo,
|
||||
Project,
|
||||
|
@ -18,8 +23,15 @@ class PomoController extends Controller
|
|||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
public function index(Request $request): PomoResource|Application|Factory|View
|
||||
{
|
||||
if ($request->expectsJson()){
|
||||
$todo = Todo::find($request->todo_id);
|
||||
$pomos = $todo->pomo()->paginate(4);
|
||||
|
||||
return new PomoResource($pomos);
|
||||
|
||||
}
|
||||
return view('pomo.index');
|
||||
}
|
||||
|
||||
|
@ -42,7 +54,7 @@ class PomoController extends Controller
|
|||
*/
|
||||
public function store(StorePomoRequest $request)
|
||||
{
|
||||
// $this->authorize('create', Pomo::class);
|
||||
$this->authorize('create', Pomo::class);
|
||||
|
||||
// Convert due_start and end to unix timestamp and save
|
||||
$pomo = new Pomo();
|
||||
|
@ -52,6 +64,13 @@ class PomoController extends Controller
|
|||
$pomo->notes = $request->safe()->notes;
|
||||
$pomo->save();
|
||||
|
||||
if ($request->expectsJson()){
|
||||
return response()->json([
|
||||
'message' => 'Pomo created successfully.',
|
||||
'data' => $pomo,
|
||||
], 201);
|
||||
}
|
||||
|
||||
return redirect()->route('pomo.index')
|
||||
->with('success', 'Pomo created successfully.');
|
||||
}
|
||||
|
@ -59,10 +78,17 @@ class PomoController extends Controller
|
|||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Pomo $pomo)
|
||||
public function show(Request $request, Pomo $pomo)
|
||||
{
|
||||
$this->authorize('view', $pomo);
|
||||
|
||||
if ($request->expectsJson()){
|
||||
return response()->json([
|
||||
'message' => 'Pomo retrieved successfully.',
|
||||
'data' => $pomo,
|
||||
], 200);
|
||||
}
|
||||
|
||||
return view('pomo.show', compact('pomo'));
|
||||
}
|
||||
|
||||
|
@ -84,7 +110,7 @@ class PomoController extends Controller
|
|||
*/
|
||||
public function update(UpdatePomoRequest $request, Pomo $pomo)
|
||||
{
|
||||
// $this->authorize('update', $pomo);
|
||||
$this->authorize('update', $pomo);
|
||||
|
||||
// Convert due_start and end to unix timestamp and save
|
||||
$pomo->pomo_start = strtotime($request->pomo_start);
|
||||
|
@ -92,6 +118,13 @@ class PomoController extends Controller
|
|||
$pomo->notes = $request->notes;
|
||||
$pomo->save();
|
||||
|
||||
if ($request->expectsJson()){
|
||||
return response()->json([
|
||||
'message' => 'Pomo updated successfully.',
|
||||
'data' => $pomo,
|
||||
], 200);
|
||||
}
|
||||
|
||||
return redirect()->route('pomo.index')
|
||||
->with('success', 'Pomo updated successfully.');
|
||||
}
|
||||
|
@ -100,13 +133,19 @@ class PomoController extends Controller
|
|||
* Remove the specified resource from storage.
|
||||
* @throws AuthorizationException
|
||||
*/
|
||||
public function destroy(Pomo $pomo)
|
||||
public function destroy(Request $request, Pomo $pomo)
|
||||
{
|
||||
// Validate that the user is authorized to delete the pomo
|
||||
// $this->authorize('delete', $pomo);
|
||||
$this->authorize('delete', [Pomo::class, $pomo]);
|
||||
|
||||
$pomo->delete();
|
||||
|
||||
if ($request->expectsJson()){
|
||||
return response()->json([
|
||||
'message' => 'Pomo deleted successfully.',
|
||||
], 200);
|
||||
}
|
||||
|
||||
return redirect()->route('pomo.index')
|
||||
->with('success', 'Pomo deleted successfully.');
|
||||
|
||||
|
|
|
@ -12,9 +12,9 @@ class StoreProjectRequest extends FormRequest
|
|||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
if ($this->is('api/*')) {
|
||||
return true;
|
||||
}
|
||||
// if ($this->expectsJson()) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
return auth()->check();
|
||||
}
|
||||
|
|
|
@ -13,9 +13,9 @@ class UpdateProjectRequest extends FormRequest
|
|||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
if ($this->is('api/*')) {
|
||||
return true;
|
||||
}
|
||||
// if ($this->is('api/*')) {
|
||||
// return true;
|
||||
// }
|
||||
|
||||
return auth()->check();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\ResourceCollection;
|
||||
|
||||
class PomoResource extends ResourceCollection
|
||||
{
|
||||
/**
|
||||
* Transform the resource collection into an array.
|
||||
*
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'data' => $this->collection,
|
||||
'links' => [
|
||||
'self' => 'link-value',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
|
@ -6,6 +6,9 @@ use App\Traits\UuidTrait;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
|
||||
class Pomo extends Model
|
||||
{
|
||||
|
@ -26,4 +29,19 @@ class Pomo extends Model
|
|||
return $this->belongsTo(Todo::class);
|
||||
}
|
||||
|
||||
|
||||
public function user(): Collection
|
||||
{
|
||||
return DB::table('users')
|
||||
->join('project_user', 'users.id', '=', 'project_user.user_id')
|
||||
->join('projects', 'project_user.project_id', '=', 'projects.id')
|
||||
->join('project_todo', 'projects.id', '=', 'project_todo.project_id')
|
||||
->join('todos', 'project_todo.todo_id', '=', 'todos.id')
|
||||
->join('pomos', 'todos.id', '=', 'pomos.todo_id')
|
||||
->where('pomos.id', '=', $this->id)
|
||||
->select('users.*')
|
||||
->get();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -70,6 +70,11 @@ class User extends Authenticatable
|
|||
->get();
|
||||
}
|
||||
|
||||
public function pomo(): HasManyThrough
|
||||
{
|
||||
return $this->hasManyThrough(Pomo::class, Todo::class);
|
||||
}
|
||||
|
||||
public function pomos(): Collection
|
||||
{
|
||||
return DB::table('pomos')
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Policies;
|
|||
use App\Models\Pomo;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PomoPolicy
|
||||
{
|
||||
|
@ -25,7 +26,7 @@ class PomoPolicy
|
|||
{
|
||||
return false;
|
||||
}
|
||||
return $user->id === $pomo->todo->project->user->id;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -41,7 +42,7 @@ class PomoPolicy
|
|||
*/
|
||||
public function update(User $user, Pomo $pomo): bool
|
||||
{
|
||||
return $user->id === $pomo->todo->project->user->id;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,8 +50,14 @@ class PomoPolicy
|
|||
*/
|
||||
public function delete(User $user, Pomo $pomo): bool
|
||||
{
|
||||
// TODO: Fix this policy
|
||||
// Search for pomo user through pomos->todo->project_todo->project_user->user
|
||||
// $PomoUser = $pomo->todo->project;
|
||||
|
||||
// Check if the user is the owner of the pomo
|
||||
return $user->id === $pomo->todo->project->user->id;
|
||||
// return $user->id === $PomoUser[0]->id;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +65,7 @@ class PomoPolicy
|
|||
*/
|
||||
public function restore(User $user, Pomo $pomo): bool
|
||||
{
|
||||
return $user->id === $pomo->todo->project->user->id;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -66,6 +73,6 @@ class PomoPolicy
|
|||
*/
|
||||
public function forceDelete(User $user, Pomo $pomo): bool
|
||||
{
|
||||
return $user->id === $pomo->todo->project->user->id;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
use App\Http\Controllers\PomoController;
|
||||
use App\Http\Controllers\ProjectController;
|
||||
use App\Http\Controllers\Auth\ApiAuthController;
|
||||
use App\Http\Controllers\ProjectTodoController;
|
||||
|
@ -30,6 +31,8 @@ Route::post('/login', [ApiAuthController::class, 'login']);
|
|||
Route::middleware('auth:sanctum')->group( function () {
|
||||
Route::post('/logout', [ApiAuthController::class, 'logout']);
|
||||
Route::get('/me', [ApiAuthController::class, 'me']);
|
||||
|
||||
Route::apiResource('project', ProjectController::class);
|
||||
Route::apiResource('project.todo', ProjectTodoController::class);
|
||||
Route::apiResource('pomo', PomoController::class);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,194 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Feature\API;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PomoTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected array $user = [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@test.com',
|
||||
'password' => 'password1234'
|
||||
];
|
||||
|
||||
private string $pomo_route = '/api/pomo/';
|
||||
private $access_token;
|
||||
private $user_id;
|
||||
private $project_id;
|
||||
private $todo_id;
|
||||
|
||||
private $pomo_id;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
// Register user
|
||||
$this->registerUser();
|
||||
|
||||
// Authenticate user
|
||||
$this->authenticateUser();
|
||||
|
||||
// Create Project
|
||||
$this->createProject();
|
||||
|
||||
// Create Todo
|
||||
$this->createTodo();
|
||||
|
||||
// Create Pomo
|
||||
$this->createPomo();
|
||||
}
|
||||
|
||||
protected function registerUser(): void
|
||||
{
|
||||
$response = $this->postJson('/api/register', $this->user);
|
||||
$response->assertCreated();
|
||||
}
|
||||
|
||||
protected function authenticateUser(): void
|
||||
{
|
||||
$response = $this->postJson('/api/login', [
|
||||
'email' => $this->user['email'],
|
||||
'password' => $this->user['password'],
|
||||
]);
|
||||
$response->assertOk();
|
||||
|
||||
$this->access_token = $response['access_token'];
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->getJson('/api/me');
|
||||
$response->assertOk();
|
||||
|
||||
$this->user_id = $response['id'];
|
||||
}
|
||||
|
||||
protected function createProject(): void
|
||||
{
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->postJson('/api/project', [
|
||||
'name' => 'Test Project',
|
||||
'description' => 'Test Project Description',
|
||||
]);
|
||||
$response->assertCreated();
|
||||
|
||||
$this->project_id = $response['data']['id'];
|
||||
}
|
||||
|
||||
private function createTodo()
|
||||
{
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->postJson('/api/project/' . $this->project_id . '/todo', [
|
||||
'title' => 'Test Todo',
|
||||
'description' => 'Test Todo Description',
|
||||
]);
|
||||
$response->assertCreated();
|
||||
|
||||
$this->todo_id = $response['data']['id'];
|
||||
}
|
||||
|
||||
private function createPomo()
|
||||
{
|
||||
$start = date('Y-m-d\TH:i');
|
||||
$end = date('Y-m-d\TH:i', strtotime('+25 minutes'));
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->postJson($this->pomo_route, [
|
||||
'todo_id' => $this->todo_id,
|
||||
'pomo_start' => $start,
|
||||
'pomo_end' => $end,
|
||||
'notes' => 'Test Pomo Notes',
|
||||
]);
|
||||
$response->assertCreated();
|
||||
|
||||
$this->pomo_id = $response['data']['id'];
|
||||
}
|
||||
|
||||
public function test_user_can_view_all_pomo(): void
|
||||
{
|
||||
$response = $this->getJson($this->pomo_route . '?todo_id=' . $this->todo_id);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => [
|
||||
'*' => $this->getPomoJsonStructure(),
|
||||
],
|
||||
'links' => [
|
||||
'first',
|
||||
'last',
|
||||
'prev',
|
||||
'next',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getPomoJsonStructure(): array
|
||||
{
|
||||
return [
|
||||
'id',
|
||||
'todo_id',
|
||||
'pomo_start',
|
||||
'pomo_end',
|
||||
'notes',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
}
|
||||
|
||||
public function test_user_can_view_created_pomo(): void
|
||||
{
|
||||
$response = $this->getJson($this->pomo_route . $this->pomo_id);
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => $this->getPomoJsonStructure(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_user_can_edit_pomo(): void
|
||||
{
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->putJson($this->pomo_route . $this->pomo_id, [
|
||||
'todo_id' => $this->todo_id,
|
||||
'pomo_start' => date('Y-m-d\TH:i'),
|
||||
'pomo_end' => date('Y-m-d\TH:i', strtotime('+25 minutes')),
|
||||
'notes' => 'Updated Pomo Notes',
|
||||
]);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJsonStructure([
|
||||
'data' => $this->getPomoJsonStructure(),
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('pomos', [
|
||||
'id' => $this->pomo_id,
|
||||
'notes' => 'Updated Pomo Notes',
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function test_user_can_destroy_pomo(): void
|
||||
{
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
])->deleteJson($this->pomo_route . $this->pomo_id);
|
||||
|
||||
$response->assertOk()
|
||||
->assertJson([
|
||||
'message' => 'Pomo deleted successfully.',
|
||||
]);
|
||||
|
||||
$this->assertDatabaseMissing('pomos', [
|
||||
'id' => $this->pomo_id,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -85,6 +85,8 @@ class PomoCRUDTest extends TestCase
|
|||
public function test_user_can_delete_pomo_with_authorsation(): void
|
||||
{
|
||||
$this->test_user_can_create_pomo();
|
||||
$this->actingAs($this->user);
|
||||
$this->assertAuthenticated();
|
||||
$response = $this->delete(route('pomo.destroy', $this->pomo->id));
|
||||
$response->assertRedirect(route('pomo.index'));
|
||||
$this->assertDatabaseMissing('pomos', [
|
||||
|
|
Loading…
Reference in New Issue