Delete client

This commit is contained in:
Peter Papp
2021-04-28 18:15:33 +02:00
parent b37edd298a
commit 248825f2d1
4 changed files with 52 additions and 2 deletions
@@ -66,4 +66,17 @@ class ClientController extends Controller
new OasisClientResource($client), 201 new OasisClientResource($client), 201
); );
} }
/**
* @param Client $client
* @throws \Exception
*/
public function destroy(Client $client)
{
if ($client->user_id === Auth::id()) {
$client->delete();
return response('Done', 204);
}
}
} }
+8 -2
View File
@@ -78,8 +78,14 @@ class Client extends Model
{ {
parent::boot(); parent::boot();
static::creating(function ($order) { static::creating(function ($client) {
$order->id = (string) Str::uuid(); $client->id = (string) Str::uuid();
});
static::deleting(function ($client) {
if ($client->getRawOriginal('avatar')) {
Storage::delete($client->getRawOriginal('avatar'));
}
}); });
} }
} }
+1
View File
@@ -42,6 +42,7 @@ Route::group(['middleware' => 'api', 'prefix' => '/api/oasis'], function () {
Route::get('/search', [ClientController::class, 'search']); Route::get('/search', [ClientController::class, 'search']);
Route::post('/', [ClientController::class, 'store']); Route::post('/', [ClientController::class, 'store']);
Route::delete('/{client}', [ClientController::class, 'destroy']);
}); });
}); });
+30
View File
@@ -99,6 +99,36 @@ class OasisClientTest extends TestCase
]); ]);
} }
/**
* @test
*/
public function user_delete_client()
{
Storage::fake('local');
$user = User::factory(User::class)
->create(['role' => 'user']);
Sanctum::actingAs($user);
$avatar = UploadedFile::fake()
->image('fake-image.jpg');
Storage::putFileAs('avatar', $avatar, 'fake-image.jpg');
$client = Client::factory(Client::class)
->create([
'avatar' => 'avatar/fake-image.jpg',
'user_id' => $user->id,
]);
$this->deleteJson("/api/oasis/clients/$client->id")
->assertStatus(204);
Storage::disk('local')
->assertMissing('avatar/fake-image.jpg');
}
/** /**
* @test * @test
*/ */