From a9e794f9ef674eb49f8f2d6d990c974f3926ddbb Mon Sep 17 00:00:00 2001 From: SaraRamadan25 Date: Tue, 26 Dec 2023 23:26:13 +0200 Subject: [PATCH 1/2] still 4 fails --- app/Http/Controllers/ProjectController.php | 9 +++- app/Http/Controllers/UserController.php | 49 ++++++++++++++++------ app/Models/Morningnews.php | 1 + app/Models/Project.php | 1 + app/Providers/AppServiceProvider.php | 5 ++- 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5877c736..e14a3380 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -14,6 +14,9 @@ public function store(Request $request) Project::create([ 'name' => $request->name ]); + $project=new Project; + $project->name=$request->name; + $project->save(); return redirect('/')->with('success', 'Project created'); } @@ -27,6 +30,10 @@ public function mass_update(Request $request) // Insert Eloquent statement below + + + Project::where('name',$request->old_name)->update(['name'=>$request->new_name]); + return redirect('/')->with('success', 'Projects updated'); } @@ -35,7 +42,7 @@ public function destroy($projectId) Project::destroy($projectId); // TASK: change this Eloquent statement to include the soft-deletes records - $projects = Project::all(); + $projects = Project::withTrashed()->get(); return view('projects.index', compact('projects')); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 9b8c73dc..3da0d486 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -4,26 +4,24 @@ use App\Models\User; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Str; class UserController extends Controller { public function index() { - // TASK: turn this SQL query into Eloquent - // select * from users - // where email_verified_at is not null - // order by created_at desc - // limit 3 - - $users = User::all(); // replace this with Eloquent statement + $users = User::whereNotNull('email_verified_at') + ->orderBy('created_at', 'desc') + ->limit(3) + ->get(); return view('users.index', compact('users')); } public function show($userId) { - $user = NULL; // TASK: find user by $userId or show "404 not found" page - + $user = User::findorFail($userId); return view('users.show', compact('user')); } @@ -31,17 +29,41 @@ public function check_create($name, $email) { // TASK: find a user by $name and $email // if not found, create a user with $name, $email and random password - $user = NULL; + + $user = User::where('name', $name) + ->where('email', $email) + ->first(); + + if (!$user) { + $password = Str::random(8); + + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'password' => bcrypt($password), // Hash the password using bcrypt + ]); + } 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 + $user = User::where('name', $name) + ->update([ + 'email' => $email + ]); + if (!$user) { + $password = str_random(8); + + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'password' => Hash::make($password), + ]); + } return view('users.show', compact('user')); } @@ -52,6 +74,7 @@ public function destroy(Request $request) // $request->users is an array of IDs, ex. [1, 2, 3] // Insert Eloquent statement here + $user = User::destroy($request->users); return redirect('/')->with('success', 'Users deleted'); } diff --git a/app/Models/Morningnews.php b/app/Models/Morningnews.php index 2335e17d..baf462b9 100644 --- a/app/Models/Morningnews.php +++ b/app/Models/Morningnews.php @@ -7,6 +7,7 @@ class Morningnews extends Model { + protected $table = 'morning_news'; use HasFactory; protected $fillable = ['title', 'news_text']; diff --git a/app/Models/Project.php b/app/Models/Project.php index dc43be0e..6f5b54a2 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -8,5 +8,6 @@ class Project extends Model { + protected $fillable = ['name']; use HasFactory, SoftDeletes; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5bc..10804ec4 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,8 @@ namespace App\Providers; +use App\Models\Project; +use App\Observers\ProjectObserver; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -23,6 +25,7 @@ public function register() */ public function boot() { - // + Project::observe(ProjectObserver::class); + } } From c9f657e5e3153eb32b605944cf00ee9db0681e98 Mon Sep 17 00:00:00 2001 From: SaraRamadan25 Date: Wed, 27 Dec 2023 14:39:54 +0200 Subject: [PATCH 2/2] All Tests passed NOW --- app/Http/Controllers/ProjectController.php | 8 +------- app/Http/Controllers/UserController.php | 14 ++++++++------ app/Models/User.php | 5 +++++ app/Observers/ProjectObserver.php | 18 ++++++++++++++++++ 4 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 app/Observers/ProjectObserver.php diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index e14a3380..505534f2 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -14,10 +14,6 @@ public function store(Request $request) Project::create([ 'name' => $request->name ]); - $project=new Project; - $project->name=$request->name; - $project->save(); - return redirect('/')->with('success', 'Project created'); } @@ -30,9 +26,7 @@ public function mass_update(Request $request) // Insert Eloquent statement below - - - Project::where('name',$request->old_name)->update(['name'=>$request->new_name]); + Project::where('name', $request->old_name)->update(['name' => $request->new_name]); return redirect('/')->with('success', 'Projects updated'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 3da0d486..668ba4d8 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -50,13 +50,14 @@ public function check_create($name, $email) public function check_update($name, $email) { + $user = User::where('name', $name)->first(); - $user = User::where('name', $name) - ->update([ - 'email' => $email + if ($user) { + $user->update([ + 'email' => $email, ]); - if (!$user) { - $password = str_random(8); + } else { + $password = Str::random(8); $user = User::create([ 'name' => $name, @@ -64,6 +65,7 @@ public function check_update($name, $email) 'password' => Hash::make($password), ]); } + return view('users.show', compact('user')); } @@ -83,7 +85,7 @@ public function only_active() { // TASK: That "active()" doesn't exist at the moment. // Create this scope to filter "where email_verified_at is not null" - $users = User::active()->get(); + $users = User::active()->get();; return view('users.index', compact('users')); } diff --git a/app/Models/User.php b/app/Models/User.php index e23e0905..a02bf342 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -41,4 +41,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function scopeActive($query) + { + return $query->where('active', true); + } } diff --git a/app/Observers/ProjectObserver.php b/app/Observers/ProjectObserver.php new file mode 100644 index 00000000..7c1fa097 --- /dev/null +++ b/app/Observers/ProjectObserver.php @@ -0,0 +1,18 @@ +save(); + // DB::update('update stats set projects_count = projects_count + 1'); + Stat::increment('projects_count'); + +} +}