Skip to content

Commit

Permalink
Complete tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
juancorax committed Nov 23, 2024
1 parent 1eb80e6 commit ea7aaee
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 18 deletions.
10 changes: 5 additions & 5 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\Stat;
use Illuminate\Http\Request;

class ProjectController extends Controller
Expand All @@ -12,7 +11,7 @@ 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 +25,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 +36,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 @@ -44,11 +45,10 @@ public function store_with_stats(Request $request)
{
// TASK: on creating a new project, create an Observer event to run SQL
// update stats set projects_count = projects_count + 1
$project = new Project();
$project = new Project;
$project->name = $request->name;
$project->save();

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

}
24 changes: 19 additions & 5 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,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')
->orderByDesc('created_at')
->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 +35,10 @@ 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' => bcrypt(Str::random(10))],
);

return view('users.show', compact('user'));
}
Expand All @@ -40,7 +47,13 @@ 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' => bcrypt(Str::random(10)),
],
);

return view('users.show', compact('user'));
}
Expand All @@ -52,6 +65,8 @@ 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 @@ -64,5 +79,4 @@ public function only_active()

return view('users.index', compact('users'));
}

}
7 changes: 7 additions & 0 deletions app/Models/Morningnews.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ class Morningnews extends Model
use HasFactory;

protected $fillable = ['title', 'news_text'];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'morning_news';
}
7 changes: 7 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = ['name'];
}
10 changes: 9 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,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 +41,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* Filter records where email_verified_at is not null.
*/
public function scopeActive(Builder $query): void
{
$query->whereNotNull('email_verified_at');
}
}
19 changes: 19 additions & 0 deletions app/Observers/ProjectObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Observers;

use App\Models\Project;
use App\Models\Stat;

class ProjectObserver
{
/**
* Handle the Project "creating" event.
*/
public function creating(Project $project): void
{
$statsRow = Stat::first();
$statsRow->projects_count += 1;
$statsRow->save();
}
}
12 changes: 5 additions & 7 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

namespace App\Providers;

use App\Models\Project;
use App\Observers\ProjectObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
public function boot(): void
{
//
Project::observe(ProjectObserver::class);
}
}

0 comments on commit ea7aaee

Please sign in to comment.