From c72be05cea3528c5f1787adf09389e2d095f402d Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 6 Dec 2021 07:58:56 +0200 Subject: [PATCH] Task 3 - update and delete old file --- README.md | 8 +++++ app/Http/Controllers/HouseController.php | 36 +++++++++++++++++++ app/Models/House.php | 13 +++++++ .../2021_12_06_054450_create_houses_table.php | 33 +++++++++++++++++ routes/web.php | 1 + tests/Feature/FileUploadTest.php | 22 ++++++++++-- 6 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/HouseController.php create mode 100644 app/Models/House.php create mode 100644 database/migrations/2021_12_06_054450_create_houses_table.php diff --git a/README.md b/README.md index 3391007..bac1d85 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,11 @@ In `app/Http/Controllers/ProjectController.php` file, in the `store()` method, p Test method `test_file_size_validation()`. --- + +## Task 3. Update: Delete Old File + +In `app/Http/Controllers/HouseController.php` file, in the `update()` method, we upload the new file but don't delete the old one. Help to clean up the disk and delete the old file. + +Test method `test_update_file_remove_old_one()`. + +--- diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php new file mode 100644 index 0000000..1ce61f8 --- /dev/null +++ b/app/Http/Controllers/HouseController.php @@ -0,0 +1,36 @@ +file('photo')->store('houses'); + + House::create([ + 'name' => $request->name, + 'photo' => $filename, + ]); + + return 'Success'; + } + + public function update(Request $request, House $house) + { + $filename = $request->file('photo')->store('houses'); + + // TASK: Delete the old file from the storage + + $house->update([ + 'name' => $request->name, + 'photo' => $filename, + ]); + + return 'Success'; + } +} diff --git a/app/Models/House.php b/app/Models/House.php new file mode 100644 index 0000000..b1aca59 --- /dev/null +++ b/app/Models/House.php @@ -0,0 +1,13 @@ +id(); + $table->string('name'); + $table->string('photo'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('houses'); + } +} diff --git a/routes/web.php b/routes/web.php index 7e82fc6..df14a28 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,3 +18,4 @@ Route::get('/', function () { }); Route::post('projects', [\App\Http\Controllers\ProjectController::class, 'store']); +Route::resource('houses', \App\Http\Controllers\HouseController::class); diff --git a/tests/Feature/FileUploadTest.php b/tests/Feature/FileUploadTest.php index e5fa34f..c460583 100644 --- a/tests/Feature/FileUploadTest.php +++ b/tests/Feature/FileUploadTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use App\Models\House; use App\Models\Project; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; @@ -14,7 +15,6 @@ class FileUploadTest extends TestCase public function test_original_filename_upload() { - Storage::fake('logos'); $filename = 'logo.jpg'; $response = $this->post('projects', [ @@ -31,8 +31,6 @@ class FileUploadTest extends TestCase public function test_file_size_validation() { - Storage::fake('logos'); - $response = $this->post('projects', [ 'name' => 'Some name', 'logo' => UploadedFile::fake()->create('logo.jpg', 2000) @@ -45,4 +43,22 @@ class FileUploadTest extends TestCase ]); $response->assertValid(); } + + public function test_update_file_remove_old_one() + { + $response = $this->post('houses', [ + 'name' => 'Some name', + 'photo' => UploadedFile::fake()->image('photo.jpg') + ]); + $response->assertStatus(200); + $house = House::first(); + $this->assertTrue(Storage::exists($house->photo)); + + $response = $this->put('houses/1', [ + 'name' => 'Some name', + 'photo' => UploadedFile::fake()->image('photo2.jpg') + ]); + $response->assertStatus(200); + $this->assertFalse(Storage::exists($house->photo)); + } }