-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/hngprojects/hng_boilerplate_…
…php_laravel_web into feat/update_job_listing
- Loading branch information
Showing
43 changed files
with
1,320 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/Http/Controllers/Api/V1/Auth/ForgetPasswordRequestController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
use App\Traits\HttpResponses; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\URL; | ||
use Illuminate\Support\Facades\Hash; | ||
use App\Models\User; | ||
|
||
class ForgetPasswordRequestController extends Controller | ||
{ | ||
use HttpResponses; | ||
|
||
/** | ||
* Handle the incoming request. | ||
*/ | ||
public function __invoke(Request $request) | ||
{ | ||
$validator = Validator::make($request->all(), [ | ||
'email' => 'required|email:rfc' | ||
]); | ||
|
||
if ($validator->fails()) { | ||
return $this->apiResponse(status: 'Error', message: $validator->errors(), status_code: 422); | ||
} | ||
|
||
$user = User::where('email', $request->email)->first(); | ||
if (!$user) { | ||
return $this->apiResponse(status: 'Error', message: 'User does not exist', status_code: 400); | ||
} | ||
|
||
// Create a new token | ||
$token_key = Str::random(60); | ||
$token = Hash::make($token_key); | ||
|
||
// Store the token in the password_reset_tokens table | ||
DB::table('password_reset_tokens')->updateOrInsert( | ||
['email' => $request->email], | ||
[ | ||
'token' => $token, | ||
'created_at' => Carbon::now(), | ||
] | ||
); | ||
|
||
// Send the reset password email | ||
$url = URL::temporarySignedRoute( | ||
'password.reset', | ||
Carbon::now()->addMinutes(config('auth.passwords.users.expire')), | ||
['token' => $token, 'email' => $request->email] | ||
); | ||
|
||
$user->sendPasswordResetNotification($url); | ||
|
||
return $this->apiResponse(status: 'Success', message: 'Password reset link sent'); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Blog; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Str; | ||
|
||
class BlogSearchController extends Controller | ||
{ | ||
public function search(Request $request) | ||
{ | ||
try { | ||
$validatedData = $request->validate([ | ||
'author' => 'nullable|string', | ||
'title' => 'nullable|string', | ||
'content' => 'nullable|string', | ||
'tags' => 'nullable|string', | ||
'created_date' => 'nullable|date_format:Y-m-d', | ||
'page' => 'nullable|integer|min:1', | ||
'page_size' => 'nullable|integer|min:1|max:100', | ||
]); | ||
|
||
$page = $validatedData['page'] ?? 1; | ||
$pageSize = $validatedData['page_size'] ?? 20; | ||
|
||
$query = Blog::query(); | ||
|
||
if (!empty($validatedData['author'])) { | ||
$query->where('author', 'like', '%' . $validatedData['author'] . '%'); | ||
} | ||
if (!empty($validatedData['title'])) { | ||
$query->where('title', 'like', '%' . $validatedData['title'] . '%'); | ||
} | ||
if (!empty($validatedData['content'])) { | ||
$query->where('content', 'like', '%' . $validatedData['content'] . '%'); | ||
} | ||
if (!empty($validatedData['tags'])) { | ||
$query->where('tags', 'like', '%' . $validatedData['tags'] . '%'); | ||
} | ||
if (!empty($validatedData['created_date'])) { | ||
$query->whereDate('created_at', '>=', $validatedData['created_date']); | ||
} | ||
|
||
$blogs = $query->paginate($pageSize, ['*'], 'page', $page); | ||
|
||
$response = [ | ||
'current_page' => $blogs->currentPage(), | ||
'total_pages' => $blogs->lastPage(), | ||
'total_results' => $blogs->total(), | ||
'blogs' => $this->formatBlogs($blogs->items()), | ||
'meta' => [ | ||
'has_next' => $blogs->hasMorePages(), | ||
'total' => $blogs->total(), | ||
'next_page' => $blogs->hasMorePages() ? $blogs->currentPage() + 1 : null, | ||
'prev_page' => $blogs->currentPage() > 1 ? $blogs->currentPage() - 1 : null, | ||
], | ||
]; | ||
|
||
// if ($blogs->total() == 0) { | ||
// $response['message'] = 'No blogs found matching the search criteria.'; | ||
// } | ||
|
||
Log::info('Blog search request', ['params' => $validatedData, 'results' => $blogs->total()]); | ||
|
||
return response()->json($response); | ||
} catch (\Illuminate\Validation\ValidationException $e) { | ||
return response()->json([ | ||
'message' => 'Validation error', | ||
'error' => $e->errors(), | ||
'status_code' => 422 | ||
], 422); | ||
} catch (\Exception $e) { | ||
Log::error('Blog search error', ['message' => $e->getMessage(), 'params' => $request->all()]); | ||
|
||
return response()->json([ | ||
'message' => 'An error occurred while searching blogs', | ||
'error' => $e->getMessage(), | ||
'status_code' => 500 | ||
], 500); | ||
} | ||
} | ||
|
||
private function formatBlogs($blogs) | ||
{ | ||
return array_map(function ($blog) { | ||
return [ | ||
'id' => $blog->id, | ||
'title' => $blog->title, | ||
'content' => $blog->content, | ||
'author' => $blog->author, | ||
'created_date' => $blog->created_at->toDateTimeString(), | ||
'tags' => explode(',', $blog->tags), | ||
]; | ||
}, $blogs); | ||
} | ||
} |
Oops, something went wrong.