Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…php_laravel_web into feat/update_job_listing
  • Loading branch information
kunmifab committed Jul 24, 2024
2 parents feb50cf + d3e5c8d commit 053ccde
Show file tree
Hide file tree
Showing 43 changed files with 1,320 additions and 96 deletions.
25 changes: 20 additions & 5 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ on:
jobs:
run-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
Expand Down Expand Up @@ -39,11 +54,11 @@ jobs:
cp .env.example .env
echo "APP_KEY=${{ secrets.APP_KEY }}" >> .env
echo "DB_CONNECTION=pgsql" >> .env
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
echo "DB_DATABASE=${{ secrets.DB_DATABASE }}" >> .env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_DATABASE=postgres" >> .env
echo "DB_USERNAME=postgres" >> .env
echo "DB_PASSWORD=postgres" >> .env
# Uncomment the line below if you prefer to set DATABASE_URL instead of individual DB environment variables
# echo "DATABASE_URL=postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_DATABASE }}?sslmode=disable" >> .env
Expand Down
26 changes: 20 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ on:
jobs:
run-tests:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down Expand Up @@ -38,11 +52,11 @@ jobs:
cp .env.example .env
echo "APP_KEY=${{ secrets.APP_KEY }}" >> .env
echo "DB_CONNECTION=pgsql" >> .env
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
echo "DB_DATABASE=${{ secrets.DB_DATABASE }}" >> .env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_DATABASE=postgres" >> .env
echo "DB_USERNAME=postgres" >> .env
echo "DB_PASSWORD=postgres" >> .env
- name: Run database migrations
run: php artisan migrate --force

Expand Down
25 changes: 20 additions & 5 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ on:
jobs:
run-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
Expand Down Expand Up @@ -38,11 +53,11 @@ jobs:
cp .env.example .env
echo "APP_KEY=${{ secrets.APP_KEY }}" >> .env
echo "DB_CONNECTION=pgsql" >> .env
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
echo "DB_DATABASE=${{ secrets.DB_DATABASE }}" >> .env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_DATABASE=postgres" >> .env
echo "DB_USERNAME=postgres" >> .env
echo "DB_PASSWORD=postgres" >> .env
- name: Run database migrations
run: php artisan migrate --force

Expand Down
29 changes: 21 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@ on:
pull_request_target:
branches:
- dev

jobs:
lint:
runs-on: ubuntu-latest

services:
postgres:
image: postgres:latest
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
ports:
- 5432:5432
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -44,12 +57,12 @@ jobs:
cp .env.example .env
echo "APP_KEY=${{ secrets.APP_KEY }}" >> .env
echo "DB_CONNECTION=pgsql" >> .env
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
echo "DB_DATABASE=${{ secrets.DB_DATABASE }}" >> .env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
echo "DATABASE_URL=postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_DATABASE }}?sslmode=disable" >> .env
echo "DB_HOST=localhost" >> .env
echo "DB_PORT=5432" >> .env
echo "DB_DATABASE=postgres" >> .env
echo "DB_USERNAME=postgres" >> .env
echo "DB_PASSWORD=postgres" >> .env
# echo "DATABASE_URL=postgres://${{ secrets.DB_USERNAME }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.DB_PORT }}/${{ secrets.DB_DATABASE }}?sslmode=disable" >> .env
- name: Run database migrations
run: php artisan migrate --force
Expand Down
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');

}
}
99 changes: 99 additions & 0 deletions app/Http/Controllers/Api/V1/BlogSearchController.php
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);
}
}
Loading

0 comments on commit 053ccde

Please sign in to comment.