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

Task 1+2 #401

Open
wants to merge 18 commits 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
20 changes: 19 additions & 1 deletion app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class ProjectController extends Controller
public function store(Request $request)
{
// TASK: Currently this statement fails. Fix the underlying issue.
// $validated = $request->validated();
// $validated = $request->safe()->only(['name']);

Project::create([
'name' => $request->name
// 'name' => $validated['name']
]);

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

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

Project::where('name', $request->old_name)->update(['name' => $request->new_name]);
return redirect('/')->with('success', 'Projects updated');
}

Expand All @@ -35,7 +43,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 All @@ -48,6 +56,16 @@ public function store_with_stats(Request $request)
$project->name = $request->name;
$project->save();

// $stats = new Stat;
// $stats->increment(['projects_count']);
// $stats->save();

$stat = Stat::first();
// $stat->increment(['projects_count']);
$count = $stat->projects_count + 1;
$stat->projects_count = $count;
$stat->save();

return redirect('/')->with('success', 'Project created');
}

Expand Down
20 changes: 16 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\Str;

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

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

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

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

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

// Insert Eloquent statement here
User::whereIn('id', $request->users)->delete();

return redirect('/')->with('success', 'Users deleted');
}
Expand All @@ -60,6 +69,9 @@ 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"
// ->filter(function (User $user) {
// return $user->email_verified_at != null;
// })
$users = User::active()->get();

return view('users.index', compact('users'));
Expand Down
1 change: 1 addition & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class Morningnews extends Model
{
use HasFactory;

protected $table = "morning_news";
protected $fillable = ['title', 'news_text'];
}
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',
];
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand Down Expand Up @@ -41,4 +42,9 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function scopeActive(Builder $query): void
{
$query->whereNotNull('email_verified_at');
}
}
Loading
Loading