Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 Tasks Completed #325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function store(Request $request)
Project::create([
'name' => $request->name
]);
// the fix was to add 'name' into fillable in \App\Models\Project.php
// to allow mass assigment

return redirect('/')->with('success', 'Project created');
}
Expand All @@ -26,6 +28,8 @@ 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 +39,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
30 changes: 26 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class UserController extends Controller
{
Expand All @@ -15,14 +16,19 @@ public function index()
// order by created_at desc
// limit 3

$users = User::all(); // replace this with Eloquent statement
// $users = User::all(); // replace this with Eloquent statement
$users = User::where('email_verified_at', '!=', null)
->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 = NULL; // TASK: find user by $userId or show "404 not found" page
$user = User::findOrFail($userId);

return view('users.show', compact('user'));
}
Expand All @@ -31,7 +37,15 @@ 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 = NULL;

// i believe this was supposed to be the solution but logically it seems incorrect
// since it will always reset the password
$user = User::firstOrCreate([
'name' => $name,
'email' => $email,
'password' => Hash::make('password'),
]);

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +54,14 @@ 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 = NULL; // updated or created user

// i believe this was supposed to be the solution but logically it seems incorrect
// since it will always reset the password
$user = User::updateOrCreate(
['name' => $name],
['email' => $email, 'password' => Hash::make('password')]
);

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +73,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 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',
];
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeActive($query)
{
return $query->where('email_verified_at', '!=', null);
}
}
14 changes: 14 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Observers;

use App\Models\Stat;

class ProjectObserver
{
public function created(): void
{
$stat = Stat::first();
$stat->update(['projects_count' => $stat->projects_count + 1]);
}
}
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);
}
}
Loading