Skip to content

Commit

Permalink
complete task
Browse files Browse the repository at this point in the history
  • Loading branch information
adel14524 committed Oct 30, 2024
1 parent 1eb80e6 commit a2e8197
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use App\Models\Project;
use App\Models\Stat;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProjectController extends Controller
{
public function store(Request $request)
{
// TASK: Currently this statement fails. Fix the underlying issue.
Project::create([
'name' => $request->name
'name' => $request->name,
]);

return redirect('/')->with('success', 'Project created');
Expand All @@ -26,6 +27,7 @@ public function mass_update(Request $request)
// where name = $request->old_name

// Insert Eloquent statement below
Project::where('name', $request->old_name)->update(['name' => $request->new_name]);

return redirect('/')->with('success', 'Projects updated');
}
Expand All @@ -35,7 +37,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'));
}
Expand Down
26 changes: 21 additions & 5 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserController extends Controller
{
Expand All @@ -15,14 +17,17 @@ public function index()
// 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(); // replace this with Eloquent statement

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); // TASK: find user by $userId or show "404 not found" page

return view('users.show', compact('user'));
}
Expand All @@ -31,7 +36,12 @@ 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::firstOrCreate([
'name' => $name,
'email' => $email,
],[
'password' => Hash::make(Str::random(10)),
]);

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +50,12 @@ 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::updateOrCreate([
'name' => $name,
],[
'email' => $email,
'password' => Hash::make(Str::random(10)),
]);

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +67,7 @@ public function destroy(Request $request)
// $request->users is an array of IDs, ex. [1, 2, 3]

// Insert Eloquent statement here
User::destroy($request->users);

return redirect('/')->with('success', 'Users deleted');
}
Expand All @@ -60,7 +76,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::whereNotNull('email_verified_at')->get();

return view('users.index', compact('users'));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Morningnews extends Model
class MorningNews extends Model
{
use HasFactory;

Expand Down
4 changes: 4 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'name',
];
}
49 changes: 49 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Observers;

use App\Models\Project;
use Illuminate\Support\Facades\DB;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{
DB::table('stats')->increment('projects_count');
}

/**
* Handle the Project "updated" event.
*/
public function updated(Project $project): void
{
//
}

/**
* Handle the Project "deleted" event.
*/
public function deleted(Project $project): void
{
//
}

/**
* Handle the Project "restored" event.
*/
public function restored(Project $project): void
{
//
}

/**
* Handle the Project "force deleted" event.
*/
public function forceDeleted(Project $project): void
{
//
}
}
4 changes: 3 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Providers;

use App\Models\Project;
use App\Observers\ProjectObserver;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
Expand All @@ -27,6 +29,6 @@ class EventServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('news');
Schema::dropIfExists('morning_news');
}
}
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// \App\Models\User::factory(10)->create();
\App\Models\User::factory(10)->create();
}
}

0 comments on commit a2e8197

Please sign in to comment.