first commit

This commit is contained in:
2024-05-21 14:17:55 +08:00
commit b29ea777f9
167 changed files with 14750 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ConfirmsPasswords;
class ConfirmPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Confirm Password Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password confirmations and
| uses a simple trait to include the behavior. You're free to explore
| this trait and override any functions that require customization.
|
*/
use ConfirmsPasswords;
/**
* Where to redirect users when the intended url fails.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
class ForgotPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/
use SendsPasswordResetEmails;
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = null;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request) {
$credentials = $request->only('email', 'password');
if (auth()->attempt($credentials)) {
return response()->json(['message' => 'User logged in successfully', 'user' => auth()->user()], 200);
} else {
return response()->json(['message' => 'User not found'], 404);
}
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
//protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}
event(new Registered($user = $this->create($request->all())));
return response()->json(['message' => 'User registered successfully', 'user' => $user], 201);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/
use ResetsPasswords;
/**
* Where to redirect users after resetting their password.
*
* @var string
*/
protected $redirectTo = '/home';
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}

View File

@@ -0,0 +1,137 @@
<?php
namespace App\Http\Controllers;
use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class CommentController extends Controller
{
/**
* Retrieve all comments with their associated user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response containing all comments.
*/
public function get(Request $request)
{
$comments = Comment::with('user')->get();
return response()->json($comments, 200);
}
/**
* Create a new comment on a specific post.
*
* @param Request $request The HTTP request object.
* @param int $postId The ID of the post to comment on.
* @return \Illuminate\Http\JsonResponse A JSON response containing the created comment.
*/
//OLD-------------------
// public function post(Request $request, $postId)
// {
// $data = $request->all();
// $comment = new Comment($data);
// $comment->post_id = $postId;
// $comment->user_id = auth()->id();
// $comment->save();
// return response()->json($comment, 201);
// }
//NEW--------------------
public function post(Request $request)
{
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
// return response()->json("$request->post_id", 401);
$comment = Comment::create([
'content' => $request->content,
'user_id' => auth()->id(),
'post_id' => $request->post_id
]);
return response()->json($comment, 201);
}
/**
* Retrieve all comments for a specific post.
*
* @param Request $request The HTTP request object.
* @param int $postId The ID of the post to retrieve comments for.
* @return \Illuminate\Http\JsonResponse A JSON response containing the comments for the specified post.
*/
public function getPostComments(Request $request, $postId)
{
$comments = Comment::where('post_id', $postId)->with('user')->get();
return response()->json($comments, 200);
}
/**
* Retrieve a specific comment by its ID.
*
* @param Request $request The HTTP request object.
* @param int $commentId The ID of the comment to retrieve.
* @return \Illuminate\Http\JsonResponse A JSON response containing the specified comment.
*/
public function getComment(Request $request, $commentId)
{
$comment = Comment::with('user')->find($commentId);
error_log($comment);
return response()->json($comment, 200);
}
/**
* Update an existing comment.
*
* @param Request $request The HTTP request object.
* @param int $commentId The ID of the comment to update.
* @return \Illuminate\Http\JsonResponse A JSON response containing the updated comment or an error message.
*/
//OLD--------------------------------
// public function put(Request $request, $commentId)
// {
// $comment = Comment::find($commentId);
// if ($comment && $comment->user_id === auth()->id()) {
// $data = $request->all();
// $comment->update($data);
// return response()->json($comment, 200);
// } else {
// return response()->json(['error' => 'You are not authorized to update this comment'], 401);
// }
// }
//NEW---------------------------
public function put(Request $request)
{
// below section is REQUIRED
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$data = [
'content' => $request->content
];
// above section is REQUIRED
//only owner can update the
$comment = Comment::where([
'id' => $request->id,
'user_id' => auth()->id(),
])->first();
//return empty post
if (!$comment) {
return response()->json(['error' => 'You are not authorized to update this comment'], 401);
}
$comment->update($data);
$comment->load('user');
//send updated data of comment
return response()->json($comment, 200);
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
// public function __construct()
// {
// $this->middleware('auth');
// }
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Retrieve all posts with their associated user.
*
* @return \Illuminate\Http\JsonResponse A JSON response containing all posts.
*/
public function get()
{
$posts = Post::with('user')->get();
return response()->json($posts, 200);
}
/**
* Create a new post.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response containing the created post.
*/
public function post(Request $request)
{
//OLD-------------
// $data = $request->all();
// $post = new Post($data);
// $post->user_id = auth()->id();
// $post->save();
// return response()->json($post, 201);
//NEW-----------------
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$post = Post::create([
'content' => $request->content,
'user_id' => auth()->id()
]);
//you can use the $post value for future "push" function of the JavaScript instead of fetching data every time with new posts
return response()->json($post, 201);
}
/**
* Update an existing post.
*
* @param Request $request The HTTP request object.
* @param int $postId The ID of the post to update.
* @return \Illuminate\Http\JsonResponse A JSON response containing the updated post or an error message.
*/
//OLD
// public function put(Request $request, $postId)
// {
// $post = Post::find($postId);
// if ($post && $post->user_id === auth()->id()) {
// $data = $request->all();
// $post->update($data);
// return response()->json($post, 200);
// } else {
// return response()->json(['error' => 'You are not authorized to update this post'], 401);
// }
// }
// }
//NEW
public function put(Request $request)
{
// below section is REQUIRED
$validator = Validator::make($request->all(), [
'content' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
$data = [
'content' => $request->content
];
// above section is REQUIRED
//only owner can update the
$post = Post::where([
'id' => $request->id,
'user_id' => auth()->id()
])->first();
//return empty post
if (!$post) {
return response()->json(['error' => "You are not authorized to update this post."], 401);
}
$post->update($data);
return response()->json($post, 200);
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class UserController extends Controller
{
/**
* Show the user details for the given ID.
*
* @param string $id The ID of the user to retrieve.
* @return \Illuminate\Http\JsonResponse A JSON response containing the user details.
*/
public function show(string $id)
{
$user = User::findOrFail($id);
return response()->json($user, 200);
}
/**
* Log in a user with the provided credentials.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating success or failure of the login attempt.
*/
public function login(Request $request)
{
$credentials = $request->only('email', 'password');
if (auth()->attempt($credentials)) {
error_log('User logged in');
return response()->json(['message' => 'User logged in', 'user' => auth()->user()], 200);
} else {
error_log('User not found');
return response()->json(['message' => 'User not found'], 401);
}
}
/**
* Log out the currently authenticated user.
*
* @return \Illuminate\Http\JsonResponse A JSON response indicating the user has been logged out.
*/
public function logout()
{
auth()->logout();
return response()->json(['message' => 'User logged out'], 200);
}
/**
* Validate the given user data.
*
* @param array $data The user data to validate.
* @return \Illuminate\Contracts\Validation\Validator The validator instance.
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8'],
]);
}
/**
* Create a new user with the given data.
*
* @param array $data The user data to use for creation.
* @return \App\Models\User The created user instance.
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
/**
* Register a new user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating success or failure of the registration attempt.
*/
public function register(Request $request)
{
$validator = $this->validator($request->all());
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 422);
}
event(new Registered($user = $this->create($request->all())));
return response()->json(['message' => 'User registered successfully', 'user' => $user], 201);
}
/**
* Edit the profile of the currently authenticated user.
*
* @param Request $request The HTTP request object.
* @return \Illuminate\Http\JsonResponse A JSON response indicating the profile update status.
*/
public function editProfile(Request $request)
{
// $validator = Validator::make($request->all(), [
// 'email' => 'required',
// 'name' => 'required',
// ]);
// if ($validator->fails()) {
// return response()->json($validator->errors(), 400);
// }
$user = User::find(auth()->user()->id);
$data = $request->only('name', 'email');
$user->fill($data)->save();
return response()->json(['message' => 'Profile updated successfully', 'user' => $user], 200);
}
}