Skip to content

Commit

Permalink
Merge pull request #611 from hngprojects/dev
Browse files Browse the repository at this point in the history
Merge from dev branch
  • Loading branch information
timiajayi authored Aug 25, 2024
2 parents 94d0a08 + d1af577 commit c101a19
Show file tree
Hide file tree
Showing 49 changed files with 1,377 additions and 747 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ jobs:
- name: Run database migrations
run: php artisan migrate --force

- name: Clear all cache
run: php artisan optimize

- name: Clear configuration cache
run: php artisan config:clear


- name: Run Laravel tests
run: |
Expand Down
27 changes: 14 additions & 13 deletions .github/workflows/postman-api-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Scheduled Postman API Tests
name: Scheduled API Tests

on:
schedule:
Expand All @@ -23,17 +23,18 @@ jobs:
- name: Run Postman Collection
run: newman run postman-api-tests.json -r cli,json --reporter-json-export result.json
continue-on-error: true



- name: Transfer test results via SCP
uses: appleboy/[email protected]
- name: Set up PHP
uses: nanasess/setup-php@v4
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
password: ${{ secrets.SSH_PASSWORD }}
source: "result.json"
target: "/var/www/boilerplate-be"

- name: Clean up
run: rm -f result.json
php-version: '8.2'

- name: Send parsed result to Hosted Application
run: |
cp .env.example .env
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "APP_URL=${{ secrets.APP_URL }}" >> .env
php artisan store:api-status

6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Code Quality

on:
push:
branches:
- dev
pull_request_target:

jobs:
lint:
Expand Down Expand Up @@ -31,6 +29,8 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Set up PHP
uses: nanasess/setup-php@v4
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ docker-compose.yml
/public/uploads
ResponseHTTP_OK,
can
result.json
102 changes: 0 additions & 102 deletions app/Console/Commands/ApiStatus.php

This file was deleted.

97 changes: 73 additions & 24 deletions app/Http/Controllers/Api/V1/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,30 @@ public function latest(Request $request)
public function index()
{
try{
$blogPosts = Blog::orderBy('created_at', 'desc')
->select('id', 'title', 'content', 'author', 'created_at', 'category', 'image_url')
->get();
$blogPosts = Blog::with('comments')
->orderBy('created_at', 'desc')
->select('id', 'title', 'content', 'author', 'created_at', 'category', 'image_url')
->get();

$blogPosts = $blogPosts->map(function ($blog) {
return [
'id' => $blog->id,
'title' => $blog->title,
'content' => $blog->content,
'published_date' => $blog->created_at?->toIso8601String(),
'updated_date' => $blog->updated_at?->toIso8601String(),
'author_id' => $blog->author_id,
'category' => $blog->category,
'image_url' => $blog->image_url,
'comments' => $blog->comments->map(function ($comment) {
return [
'id' => $comment->id,
'content' => $comment->content,
'created_at' => $comment->created_at?->toIso8601String(),
];
}),
];
});

return response()->json([
'data' => $blogPosts,
Expand All @@ -93,17 +114,17 @@ public function create()
*/
public function store(Request $request)
{
$author = User::where('id', Auth::id())->first();
if(!$author){
return response()->json(['error' => 'User Not found.'], 404);
$author = User::find(Auth::id());

if (!$author) {
return response()->json(['error' => 'User not found.'], 404);
}

Log::error('Error creating blog post: ' . Auth::id());
$validator = Validator::make($request->all(), [
'title' => ['required', 'string', 'max:255'],
'content' => ['required', 'string'],
'image_url' => ['required', 'mimes:jpeg,png,jpg,gif,svg'],
'category' => ['required', 'string', 'max:255'],
'title' => 'required|string|max:255',
'content' => 'required|string',
'category' => 'required|string|max:255',
'image_url' => 'required|mimes:jpeg,png,jpg,gif,svg',
]);

if ($validator->fails()) {
Expand All @@ -112,27 +133,47 @@ public function store(Request $request)
'status_code' => 422,
], 422);
}

try {
DB::beginTransaction();
$saved = Storage::disk('public')->put('images', $request->file('image_url'));

$savedPath = Storage::disk('public')->put('images', $request->file('image_url'));
$imageUrl = 'storage/' . $savedPath;

$blog = Blog::create([
'title' => $request->get('title'),
'content' => (string)$request->get('content'),
'author' => $author->name ? $author->name : "",
'image_url' => 'storage/'.$saved,
'content' => $request->get('content'),
'author' => $author->name ?? '',
'image_url' => $imageUrl,
'category' => $request->get('category'),
'author_id' => $author->id
'author_id' => $author->id,
]);

DB::commit();

return response()->json([
'data' => $blog,
'message' => 'Blog post created successfully.',
'status_code' => Response::HTTP_CREATED,
'id' => $blog->id,
'title' => $blog->title,
'image_url' => $blog->image_url,
'content' => $blog->content,
'published_date' => $blog->created_at->toIso8601String(),
'updated_date' => $blog->updated_at->toIso8601String(),
'author_id' => $blog->author_id,
'category' => $blog->category,
'comments' => $blog->comments->map(function ($comment) {
return [
'id' => $comment->id,
'content' => $comment->content,
'created_at' => $comment->created_at->toIso8601String(),
];
}),
], Response::HTTP_CREATED);


} catch (\Exception $exception) {
Log::error('Error creating blog post: ' . $exception->getMessage());
DB::rollBack();

return response()->json(['error' => 'Internal server error.'], 500);
}
}
Expand All @@ -143,11 +184,12 @@ public function store(Request $request)
public function show(Request $request, string $id)
{
try {
$blog = Blog::find($id);
$blog = Blog::with('comments')->find($id);

if(!$blog){
return response()->json([
'error' => 'Blog not found.',
'message' => 'Blog not found.',
'status_code' => Response::HTTP_NOT_FOUND,
], 404);
}
Expand All @@ -158,14 +200,21 @@ public function show(Request $request, string $id)
'category' => $blog->category,
'content' => $blog->content,
'image_url' => $blog->image_url,
'created_at' => $blog->created_at,
'published_date' => $blog->created_at?->toIso8601String(),
'updated_date' => $blog->updated_at?->toIso8601String(),
'author_id' => $blog->author_id,
'comments' => $blog->comments->map(function ($comment) {
return [
'id' => $comment->id,
'content' => $comment->content,
'created_at' => $comment->created_at?->toIso8601String(),
];
}),
],
'message' => 'Blog post fetched sucessfully.',
'message' => 'Blog post details fetched sucessfully.',
'status_code' => Response::HTTP_OK,
], Response::HTTP_OK);
} catch (Exception $exception) {
// Log::error('Error creating blog post: ' . $exception->getMessage());
DB::rollBack();
return response()->json(['error' => 'Internal server error.'], 500);
}
}
Expand Down
18 changes: 14 additions & 4 deletions app/Http/Controllers/Api/V1/Admin/EmailTemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function index(Request $request)
'limit' => $templates->perPage(),
], 200);
}

public function update(Request $request, $id)
{
// Validate the request data
Expand Down Expand Up @@ -97,7 +98,6 @@ public function update(Request $request, $id)

public function store(Request $request)
{

// Validate request data
$validatedData = $request->validate([
'title' => 'required|string|max:255|unique:email_templates,title',
Expand All @@ -108,13 +108,23 @@ public function store(Request $request)
// Create email template
$emailTemplate = EmailTemplate::create($validatedData);

// Format the response to match the API schema in the screenshot
return response()->json([
'status_code' => 200,
'data' => [
'id' => $emailTemplate->id,
'name' => $emailTemplate->title,
'subject' => '', // Add subject if applicable
'template_body' => $emailTemplate->template,
'placeholders' => [
// Add placeholders if available
],
],
'message' => 'Email template created successfully',
'data' => $emailTemplate
], 200);
'status_code' => 201
], 201);
}


public function destroy($id)
{
// Find the email template by ID
Expand Down
Loading

0 comments on commit c101a19

Please sign in to comment.