diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php new file mode 100644 index 0000000..f5062de --- /dev/null +++ b/app/Http/Controllers/AdminController.php @@ -0,0 +1,46 @@ +middleware('admin'); + } + + public function index() + { + $users = User::all(); + + return view('admin-dashboard', ['users' => $users]); + } + + public function edit($id) + { + $user = User::find($id); + return view('edit-user', ['user' => $user]); + } + + public function show($id) + { + $user = User::find($id); + return view('show-user', ['user' => $user]); + } + + public function update(Request $request, $id) + { + $user = User::find($id); + + $user->name = $request->name; + $user->isAdmin = $request->isAdmin; + + $user->save(); + + return view('show-user', ['user' => $user]); + } + +} diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index 18a0d08..f325b95 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; +use App\Models\User; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers; @@ -26,6 +27,7 @@ class LoginController extends Controller * * @var string */ + protected $redirectTo = RouteServiceProvider::HOME; /** diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index ed1a5e0..0cc1ac1 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -53,6 +53,7 @@ protected function validator(array $data) 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], + 'isAdmin' => ['required', 'string', 'max:255'], ]); } @@ -68,6 +69,7 @@ protected function create(array $data) 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), + 'isAdmin' => $data['isAdmin'], ]); } } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7cbc2c3..e02bf48 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use App\Models\User; +use App\Models\WorkExperience; class HomeController extends Controller { @@ -23,6 +25,38 @@ public function __construct() */ public function index() { - return view('home'); + $user = User::find(auth()->id()); + + return view('home', ['user' => $user]); + } + + public function show(User $user) + { + $works = $user->WorkExperiences()->get(); + + return view('work-experiences', ['works' => $works]); + + } + + public function create() + { + return view('create-work'); + } + + public function store(Request $request) + { + WorkExperience::create([ + 'position' => $request->position, + 'org_name' => $request->org_name, + 'org_activity' => $request->org_activity, + 'description' => $request->description, + 'month_start' => $request->month_start, + 'year_start' => $request->year_start, + 'month_end' => $request->month_end, + 'year_end' => $request->year_end, + 'user_id' => auth()->id(), + ]); + + return redirect()->route('home'); } } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..4a11c3b --- /dev/null +++ b/app/Http/Controllers/UserController.php @@ -0,0 +1,15 @@ + \Illuminate\Routing\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + 'admin' => \App\Http\Middleware\AdminMiddleware::class, ]; } diff --git a/app/Http/Middleware/AdminMiddleware.php b/app/Http/Middleware/AdminMiddleware.php new file mode 100644 index 0000000..f41df78 --- /dev/null +++ b/app/Http/Middleware/AdminMiddleware.php @@ -0,0 +1,26 @@ +isAdmin==true) { + return $next($request); + } + + return redirect('login'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 804799b..fceca4d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -20,6 +20,7 @@ class User extends Authenticatable 'name', 'email', 'password', + 'isAdmin', ]; /** @@ -40,4 +41,9 @@ class User extends Authenticatable protected $casts = [ 'email_verified_at' => 'datetime', ]; + + public function WorkExperiences() + { + return $this->hasMany(WorkExperience::class); + } } diff --git a/app/Models/WorkExperience.php b/app/Models/WorkExperience.php new file mode 100644 index 0000000..2de42a5 --- /dev/null +++ b/app/Models/WorkExperience.php @@ -0,0 +1,28 @@ +belongsTo(User::class); + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index bdea1a3..1e7cf85 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -25,6 +25,7 @@ public function definition() return [ 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, + 'isAdmin' => 'simple_user', 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), diff --git a/database/factories/WorkExperienceFactory.php b/database/factories/WorkExperienceFactory.php new file mode 100644 index 0000000..2379548 --- /dev/null +++ b/database/factories/WorkExperienceFactory.php @@ -0,0 +1,36 @@ + $this->faker->text(25), + 'org_name' => $this->faker->text(50), + 'org_activity' => $this->faker->text(50), + 'description' => $this->faker->paragraph(5), + 'month_start' => $this->faker->monthName(), + 'year_start' => $this->faker->year(), + 'month_end' => $this->faker->monthName(), + 'year_end' => $this->faker->year(), + 'user_id' => $this->faker->numberBetween(1,10), + ]; + } +} diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 621a24e..180ddb4 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -17,6 +17,7 @@ public function up() $table->id(); $table->string('name'); $table->string('email')->unique(); + $table->string('isAdmin')->default('simple_user'); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); diff --git a/database/migrations/2021_01_24_102001_create_work_experiences_table.php b/database/migrations/2021_01_24_102001_create_work_experiences_table.php new file mode 100644 index 0000000..55557a1 --- /dev/null +++ b/database/migrations/2021_01_24_102001_create_work_experiences_table.php @@ -0,0 +1,43 @@ +id(); + $table->string('position'); + $table->string('org_name'); + $table->string('org_activity'); + $table->text('description'); + $table->string('month_start'); + $table->integer('year_start'); + $table->string('month_end')->nullable(); + $table->integer('year_end')->nullable(); + $table->unsignedBigInteger('user_id'); + $table->timestamps(); + + $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('work_experiences'); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 57b73b5..e5d03f5 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -13,6 +13,7 @@ class DatabaseSeeder extends Seeder */ public function run() { - // \App\Models\User::factory(10)->create(); + \App\Models\User::factory(10)->create(); + \App\Models\WorkExperience::factory(20)->create(); } } diff --git a/database/seeders/WorkExperienceSeeder.php b/database/seeders/WorkExperienceSeeder.php new file mode 100644 index 0000000..23db60b --- /dev/null +++ b/database/seeders/WorkExperienceSeeder.php @@ -0,0 +1,18 @@ + +
Position | +Organization | +Org Activity | +
---|---|---|
{{ $work->position }} | +{{ $work->org_name }} | +{{ $work->org_activity }} | +