mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Tasks until no.8 - mass delete users
This commit is contained in:
@@ -68,3 +68,31 @@ In `app/Http/Controllers/ProjectController.php` file method `store()`, creating
|
||||
Test method `test_create_project()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 6. Mass Update.
|
||||
|
||||
In `app/Http/Controllers/ProjectController.php` file method `mass_update()`, write the update SQL query as Eloquent statement.
|
||||
|
||||
```
|
||||
update projects set name = $request->new_name where name = $request->old_name
|
||||
```
|
||||
|
||||
Test method `test_mass_update_projects()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 7. Update or New Record.
|
||||
|
||||
In `app/Http/Controllers/UserController.php` file method `check_update()`, find a user by $name and update it with $email. If not found, create a user with $name, $email and random password
|
||||
|
||||
Test method `test_check_or_update_user()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 8. Mass Delete Users.
|
||||
|
||||
In `app/Http/Controllers/UserController.php` file method `destroy()`, delete all users by the array of `$request->users`
|
||||
|
||||
Test method `test_mass_delete_users()`.
|
||||
|
||||
---
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
@@ -34,4 +35,24 @@ class UserController extends Controller
|
||||
|
||||
return view('users.show', compact('user'));
|
||||
}
|
||||
|
||||
public function check_update($name, $email)
|
||||
{
|
||||
// TASK: find a user by $name and update it with $email
|
||||
// if not found, create a user with $name, $email and random password
|
||||
$user = NULL; // updated or created user
|
||||
|
||||
return view('users.show', compact('user'));
|
||||
}
|
||||
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
// TASK: delete multiple users by their IDs
|
||||
// SQL: delete from users where id in ($request->users)
|
||||
// $request->users is an array of IDs, ex. [1, 2, 3]
|
||||
|
||||
// Insert Eloquent statement here
|
||||
|
||||
return redirect('/')->with('success', 'Users deleted');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ Route::get('/', function () {
|
||||
Route::get('users', [UserController::class, 'index']);
|
||||
Route::get('users/{userId}', [UserController::class, 'show']);
|
||||
Route::get('users/check/{name}/{email}', [UserController::class, 'check_create']);
|
||||
Route::get('users/check_update/{name}/{email}', [UserController::class, 'check_update']);
|
||||
Route::delete('users', [UserController::class, 'destroy']);
|
||||
|
||||
Route::post('projects', [ProjectController::class, 'store']);
|
||||
Route::post('projects/mass_update', [ProjectController::class, 'mass_update']);
|
||||
|
||||
@@ -90,4 +90,33 @@ class EloquentTest extends TestCase
|
||||
$this->assertDatabaseMissing('projects', ['name' => 'Old name']);
|
||||
$this->assertDatabaseHas('projects', ['name' => 'New name']);
|
||||
}
|
||||
|
||||
public function test_check_or_update_user() {
|
||||
$response = $this->get('users/check_update/john/john@john.com');
|
||||
$response->assertStatus(200);
|
||||
$this->assertDatabaseHas('users', [
|
||||
'name' => 'john',
|
||||
'email' => 'john@john.com'
|
||||
]);
|
||||
|
||||
// Same parameters - should NOT create a new user
|
||||
$this->get('users/check_update/john/john2@john.com');
|
||||
$this->assertDatabaseHas('users', [
|
||||
'name' => 'john',
|
||||
'email' => 'john2@john.com'
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_mass_delete_users()
|
||||
{
|
||||
User::factory(4)->create();
|
||||
$this->assertDatabaseCount('users', 4);
|
||||
|
||||
$response = $this->delete('users', [
|
||||
'users' => [1, 2, 3]
|
||||
]);
|
||||
$response->assertRedirect();
|
||||
$this->assertDatabaseCount('users', 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user