Skip to content
Merged
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
16 changes: 4 additions & 12 deletions src/app/Post/Application/Dto/GetAllUserPostDtoCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,24 @@

namespace App\Post\Application\Dto;

use App\Post\Application\Dto\GetUserEachPostDto as PostDto;
use App\Post\Application\Dto\GetUserEachPostDto;

class GetAllUserPostDtoCollection
{
/**
* @param PostDto[] $posts
*/
public function __construct(
public readonly array $posts
) {}

public static function build(array $items): self
public static function build(array $collection): self
{
$postDtos = array_map(
fn($item) => PostDto::build($item),
$items
fn($entity) => GetUserEachPostDto::buildFromEntity($entity),
$collection
);

return new self($postDtos);
}

/**
* Convert the collection to an array of DTOs.
*
* @return PostDto[]
*/
public function getPosts(): array
{
return $this->posts;
Expand Down
13 changes: 13 additions & 0 deletions src/app/Post/Application/Dto/GetUserEachPostDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Post\Application\Dto;

use App\Post\Domain\Entity\PostEntity;

class GetUserEachPostDto
{
public function __construct(
Expand All @@ -23,6 +25,17 @@ public static function build(array $data): self
);
}

public static function buildFromEntity(PostEntity $entity): self
{
return new self(
id: $entity->getId()->getValue(),
userId: $entity->getUserId()->getValue(),
content: $entity->getContent(),
mediaPath: $entity->getMediaPath(),
visibility: $entity->getPostVisibility()->getStringValue()
);
}

public function toArray(): array
{
return [
Expand Down
10 changes: 9 additions & 1 deletion src/app/Post/Application/UseCase/GetOthersAllPostsUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ public function handle(
total: $allPosts->getTotal(),
perPage: $allPosts->getPerPage(),
currentPage: $allPosts->getCurrentPage(),
lastPage: $allPosts->getLastPage()
lastPage: $allPosts->getLastPage(),
from: $allPosts->getFrom(),
to: $allPosts->getTo(),
path: $allPosts->getPath(),
firstPageUrl: $allPosts->getFirstPageUrl(),
lastPageUrl: $allPosts->getLastPageUrl(),
nextPageUrl: $allPosts->getNextPageUrl(),
prevPageUrl: $allPosts->getPrevPageUrl(),
links: $allPosts->getLinks()
);
}
}
11 changes: 5 additions & 6 deletions src/app/Post/Infrastructure/QueryService/GetPostQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Common\Domain\ValueObject\PostId;
use App\Common\Domain\ValueObject\UserId;
use App\Models\Post;
use App\Post\Application\Dto\GetUserEachPostDto;
use App\Post\Application\QueryServiceInterface\GetPostQueryServiceInterface;
use App\Post\Domain\Entity\PostEntity;
use App\Post\Domain\EntityFactory\PostFromModelEntityFactory;
Expand Down Expand Up @@ -67,7 +68,9 @@ private function paginationDto(
): PaginationDto
{
$dtos = array_map(
fn($item) => PostFromModelEntityFactory::build($item),
fn($item) => GetUserEachPostDto::buildFromEntity(
PostFromModelEntityFactory::build($item)
),
$data['data']
);

Expand Down Expand Up @@ -108,10 +111,6 @@ public function getOthersAllPosts(
return null;
}

$allPosts = $getPosts->map(function ($post) {
return PostFromModelEntityFactory::build($post->toArray());
});

return $this->paginationDto($allPosts->toArray());
return $this->paginationDto($getPosts->toArray());
}
}
95 changes: 95 additions & 0 deletions src/app/Post/Tests/GetOthersUserPostsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Post\Tests;

use App\Models\Post;
use App\Models\User;
use Tests\TestCase;
use Illuminate\Support\Facades\DB;

class GetOthersUserPostsTest extends TestCase
{
protected $endpoint = "/api/users/{userId}/posts/public";
private $userId;
protected function setUp(): void
{
parent::setUp();
$this->refresh();

$user = User::create([
'first_name' => 'Cristiano',
'last_name' => 'Ronaldo',
'email' => '[email protected]',
'password' => 'test1234',
Comment on lines +19 to +23
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using Laravel model factories (e.g. User::factory()) to create test users and posts—this reduces boilerplate and follows testing best practices.

Suggested change
$user = User::create([
'first_name' => 'Cristiano',
'last_name' => 'Ronaldo',
'email' => '[email protected]',
'password' => 'test1234',
$user = User::factory()->create([
'first_name' => 'Cristiano',
'last_name' => 'Ronaldo',
'email' => '[email protected]',
'password' => bcrypt('test1234'),

Copilot uses AI. Check for mistakes.

'bio' => null,
'location' => null,
'skills' => ['Laravel', 'React'],
'profile_image' => null,
]);
$this->userId = $user->id;

$this->createDummyPosts($user->id);
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

The method createDummyPosts is defined without parameters but called with $user->id, causing an argument mismatch. Update the method signature to accept the excluded userId and pass it through.

Copilot uses AI. Check for mistakes.

}

protected function tearDown(): void
{
$this->refresh();
parent::tearDown();
}

private function refresh(): void
{
if (env('APP_ENV') === 'testing') {
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;');
User::truncate();
Post::truncate();
DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;');
}
}

private function createDummyPosts(): void
{
for ($userIndex = 1; $userIndex <= 3; $userIndex++) {
if ($userIndex === 1) {
continue;
}

Comment on lines +50 to +56
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

Hardcoding userIndex === 1 won’t reliably skip the requesting user's posts when their ID isn’t 1. Use the passed excludedUserId to conditionally skip based on actual user IDs.

Suggested change
private function createDummyPosts(): void
{
for ($userIndex = 1; $userIndex <= 3; $userIndex++) {
if ($userIndex === 1) {
continue;
}
private function createDummyPosts(int $excludedUserId): void
{
for ($userIndex = 1; $userIndex <= 3; $userIndex++) {

Copilot uses AI. Check for mistakes.

$user = User::create([
'first_name' => "User{$userIndex}",
'last_name' => "Test{$userIndex}",
'email' => "user{$userIndex}@example.com",
'password' => bcrypt('password123'),
'bio' => "This is user {$userIndex}",
'location' => "City{$userIndex}",
'skills' => ['Laravel', 'Vue'],
'profile_image' => 'https://example.com/user.jpg',
]);

for ($i = 1; $i <= 20; $i++) {
Post::create([
'user_id' => $user->id,
'content' => "Post {$i} by User{$userIndex}",
'media_path' => $i % 2 === 0 ? null : "https://example.com/image{$i}.jpg",
'visibility' => $i % 2 === 0 ? 0 : 1,
'created_at' => now()->subMinutes(rand(0, 500)),
'updated_at' => now(),
]);
}
}
}
public function test_feature_api(): void
{
$newEndpoint = str_replace('{userId}', intval($this->userId), $this->endpoint);

$response = $this->getJson($newEndpoint);

$data = $response->json('data');
$meta = $response->json('meta');
$this->assertEquals(200, $response->status());
$this->assertCount(15, $data);
$this->assertEquals(20, $meta['total']);
$this->assertEquals(1, $meta['currentPage']);
$this->assertEquals(2, $meta['lastPage']);
$this->assertEquals(15, $meta['perPage']);
}
}
1 change: 1 addition & 0 deletions src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Route::prefix('{userId}/posts')->name('posts.')->group(function () {
Route::post('/', [PostController::class, 'create'])->name('create');
Route::get('/', [PostController::class, 'getAllPosts'])->name('getAllPosts');
Route::get('public', [PostController::class, 'getOthersPosts'])->name('getOthersPosts');
Route::get('{postId}', [PostController::class, 'getEachPost'])->name('getEachPost');
Route::put('{userId}/posts/{postId}', [PostController::class, 'edit'])->name('posts.edit');
});
Expand Down