Task 6 - mass update

This commit is contained in:
PovilasKorop
2021-11-16 09:58:36 +02:00
parent ef8cf9f247
commit b39af5b844
3 changed files with 32 additions and 0 deletions
@@ -16,4 +16,16 @@ class ProjectController extends Controller
return redirect('/')->with('success', 'Project created');
}
public function mass_update(Request $request)
{
// TASK: Transform this SQL query into Eloquent
// update projects
// set name = $request->new_name
// where name = $request->old_name
// Insert Eloquent statement below
return redirect('/')->with('success', 'Projects updated');
}
}
+4
View File
@@ -19,8 +19,12 @@ Route::get('/', function () {
return view('welcome');
});
// NOTICE: Not all the routes are logical and would exist in a real Laravel project
// Some routes are just for the purpose of replicating some testing scenario
Route::get('users', [UserController::class, 'index']);
Route::get('users/{userId}', [UserController::class, 'show']);
Route::get('users/check/{name}/{email}', [UserController::class, 'check_create']);
Route::post('projects', [ProjectController::class, 'store']);
Route::post('projects/mass_update', [ProjectController::class, 'mass_update']);
+16
View File
@@ -4,6 +4,7 @@ namespace Tests\Feature;
use App\Models\Morningnews;
use App\Models\News;
use App\Models\Project;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -74,4 +75,19 @@ class EloquentTest extends TestCase
$response->assertRedirect();
}
public function test_mass_update_projects() {
$project = new Project();
$project->name = 'Old name';
$project->save();
$this->assertDatabaseHas('projects', ['name' => 'Old name']);
$response = $this->post('projects/mass_update', [
'old_name' => 'Old name',
'new_name' => 'New name'
]);
$response->assertRedirect();
$this->assertDatabaseMissing('projects', ['name' => 'Old name']);
$this->assertDatabaseHas('projects', ['name' => 'New name']);
}
}