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

im done #309

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
08d6017
task 1
pouria-azad Aug 9, 2023
0fdcb87
Update UserController.php
pouria-azad Aug 9, 2023
989408a
Update UserController.php
pouria-azad Aug 9, 2023
c55a7c2
Update ProjectController.php
pouria-azad Aug 9, 2023
e0a638d
Update ProjectController.php
pouria-azad Aug 9, 2023
d7d25e1
Update UserController.php
pouria-azad Aug 9, 2023
5623e80
Update UserController.php
pouria-azad Aug 9, 2023
f58775a
Update UserController.php
pouria-azad Aug 9, 2023
65ea9e4
Update UserController.php
pouria-azad Aug 9, 2023
370a0c9
Update UserController.php
pouria-azad Aug 9, 2023
4f7db2d
Update UserController.php
pouria-azad Aug 9, 2023
953eba4
Update UserController.php
pouria-azad Aug 9, 2023
13c4891
Update UserController.php
pouria-azad Aug 9, 2023
93b0a81
Update UserController.php
pouria-azad Aug 9, 2023
edc4277
Update ProjectController.php
pouria-azad Aug 9, 2023
91ff34a
Update ProjectController.php
pouria-azad Aug 9, 2023
1fc23c2
Update ProjectController.php
pouria-azad Aug 9, 2023
c5f79fa
Update ProjectController.php
pouria-azad Aug 9, 2023
df5737a
Update ProjectController.php
pouria-azad Aug 11, 2023
1097378
Update UserController.ph
pouria-azad Aug 11, 2023
3bff064
Update UserController.php
pouria-azad Aug 11, 2023
ef3fad3
Update User.php
pouria-azad Aug 11, 2023
84c379f
Update User.php
pouria-azad Aug 11, 2023
ea5e0b7
Update User.php
pouria-azad Aug 11, 2023
4053818
Update User.php
pouria-azad Aug 11, 2023
ee05244
Update ProjectController.php
pouria-azad Aug 12, 2023
677b40c
Create ProjectObservers.php
pouria-azad Aug 12, 2023
109d087
Update ProjectObservers.php
pouria-azad Aug 12, 2023
cf0ee9c
Update ProjectObservers.php
pouria-azad Aug 12, 2023
256835f
Update ProjectObservers.php
pouria-azad Aug 12, 2023
22b437e
Update ProjectObservers.php
pouria-azad Aug 12, 2023
5e1f554
Update ProjectObservers.php
pouria-azad Aug 12, 2023
7bc7558
Update Project.php
pouria-azad Aug 12, 2023
3a84c90
Update Project.php
pouria-azad Aug 12, 2023
74c4940
Rename ProjectObservers.php to ProjectObserver.php
pouria-azad Aug 12, 2023
f30df1b
Update ProjectObserver.php
pouria-azad Aug 12, 2023
f2bfb4e
Update EventServiceProvider.php
pouria-azad Aug 12, 2023
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
12 changes: 9 additions & 3 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

class ProjectController extends Controller
{

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

Expand All @@ -24,8 +25,9 @@ public function mass_update(Request $request)
// update projects
// set name = $request->new_name
// 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 All @@ -48,6 +50,10 @@ public function store_with_stats(Request $request)
$project->name = $request->name;
$project->save();

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

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

Expand Down
33 changes: 24 additions & 9 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 @@ -13,34 +15,47 @@ public function index()
// 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
// limit 3
$users = User::whereNotNull('email_verified_at')
->orderByDesc('created_at') //latest('created_at')
->limit(3)
->get();
// $users = User::all(); // 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 = NULL; // TASK: find user by $userId or show "404 not found" page
$user = User::findOrFail($userId);
return view('users.show', compact('user'));
}

public function check_create($name, $email)
{
// TASK: find a user by $name and $email
$user = User::firstOrCreate(
['name' => $name , 'email' => $email] ,
['password' => Hash::make(str::password())] ,
);
// if not found, create a user with $name, $email and random password
$user = NULL;


return view('users.show', compact('user'));
}

public function check_update($name, $email)
{
// TASK: find a user by $name and update it with $email
$user = User::firstOrNew(
['name' => $name] ,
['email' => $email , 'password' => Hash::make(str::random())] ,
);
$user->email = $email;
$user->save();
// if not found, create a user with $name, $email and random password
$user = NULL; // updated or created user
// $user = NULL; // updated or created user

return view('users.show', compact('user'));
}
Expand All @@ -52,7 +67,8 @@ 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 @@ -61,7 +77,6 @@ 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();

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 @@ -10,4 +10,5 @@ class Morningnews extends Model
use HasFactory;

protected $fillable = ['title', 'news_text'];
protected $table = 'morning_news';
}
9 changes: 9 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
class Project extends Model
{
use HasFactory, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable= [ 'name' ,
];

}
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -12,6 +13,7 @@ class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;


/**
* The attributes that are mass assignable.
*
Expand Down Expand Up @@ -41,4 +43,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* Scope a query.
*/
public function scopeActive(Builder $query): void
{
$query->whereNotNull('email_verified_at');
}
}
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 App\Models\Stat;

class ProjectObserver
{
/**
* Handle the Project "created" event.
*/
public function created(Project $project): void
{
(Stat::first())->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 "forceDeleted" event.
*/
public function forceDeleted(Project $project): void
{
// ...
}
}
5 changes: 4 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

use App\Models\Project;
use App\Observers\ProjectObserver;

class EventServiceProvider extends ServiceProvider
{
/**
Expand All @@ -27,6 +30,6 @@ class EventServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Project::observe(ProjectObserver::class);
}
}
Loading