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 - users crud operations #20

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
100 changes: 100 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Interns2024c\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
use Interns2024c\Models\User;

class UserController extends Controller
{
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments don't add value to the code and could be removed to improve readability.

* Display a listing of the users.
*
* @return Response
*/
public function index(): Response
{
$users = User::all();
return Inertia::render('User/Index', ['users' => $users]);
}

/**
* Show the form for creating a new user.
*
* @return Response
*/
public function create(): Response
{
return Inertia::render('User/Create');
}

/**
* Store a newly created user in storage.
*
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8',
]);

User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => bcrypt($request->input('password')),
]);

return redirect()->route('users.index')->with('success', 'User created successfully.');
}

/**
* Show the form for editing the specified user.
*
* @param User $user
* @return Response
*/
public function edit(User $user)
{
return Inertia::render('User/Edit', ['user' => $user]);
}

/**
* Update the specified user in storage.
*
* @param Request $request
* @param User $user
* @return RedirectResponse
*/
public function update(Request $request, User $user): RedirectResponse
{
$validated = $request->validate([
"name" => "required|string|max:255",
"email' => 'required|email|unique:users,email",
]);

$user->update($validated);

return redirect()->route('users.index')->with('success', 'User updated successfully.');
}


/**
* Remove the specified user from storage.
*
* @param User $user
* @return RedirectResponse
*/
public function destroy(User $user): RedirectResponse
{
$user->delete();

return redirect()->back()->with('success', 'User deleted successfully.');
}
}
2 changes: 1 addition & 1 deletion app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProfileUpdateRequest extends FormRequest
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|string>
* @return array<string, ValidationRule|array|string>
*/
public function rules(): array
{
Expand Down
39 changes: 26 additions & 13 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,38 @@
*/
class User extends Authenticatable
{
use HasApiTokens;
use HasFactory;
use Notifiable;
use HasApiTokens, HasFactory, Notifiable;

public mixed $id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

protected $table = 'users';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this?


protected $fillable = [
"name",
"email",
"password",
'name',
'email',
'password',
];

protected $hidden = [
"password",
"remember_token",
'password',
'remember_token',
];

protected function casts(): array
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

/**
* Custom create method to handle user creation
*
* @param array $attributes
* @return static
*/
public static function create(array $attributes): static
{
return [
"email_verified_at" => "datetime",
"password" => "hashed",
];
$attributes['password'] = bcrypt($attributes['password']);
return static::query()->create($attributes);
}


}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"php": "^8.3.4",
"ext-pdo": "*",
"guzzlehttp/guzzle": "^7.9.2",
"inertiajs/inertia-laravel": "^1.0",
"inertiajs/inertia-laravel": "^2.0",
"laravel/framework": "^11.25.0",
"laravel/sanctum": "^4.0",
"laravel/tinker": "^2.10.0",
Expand Down
38 changes: 18 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import blumilkDefault from '@blumilksoftware/eslint-config'
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";


/** @type {import('eslint').Linter.Config[]} */
export default [
...blumilkDefault,
]
{files: ["**/*.{js,mjs,cjs,ts,vue}"]},
{languageOptions: { globals: globals.browser }},
...tseslint.configs.recommended,
...pluginVue.configs["flat/essential"],
{files: ["**/*.vue"], languageOptions: {parserOptions: {parser: tseslint.parser}}},
];
Loading
Loading