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
25 changes: 3 additions & 22 deletions src/app/Common/Domain/Enum/PostVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,10 @@

namespace App\Common\Domain\Enum;

use InvalidArgumentException;

enum PostVisibility: string
enum PostVisibility: int
{
case PUBLIC = 'public';
case PRIVATE = 'private';

public static function fromString(string $value): self
{
return match ($value) {
self::PUBLIC->value => self::PUBLIC,
self::PRIVATE->value => self::PRIVATE,
default => throw new InvalidArgumentException("Invalid PostVisibility: {$value}"),
};
}

public function toInt(): int
{
return match ($this) {
self::PUBLIC => 1,
self::PRIVATE => 2,
};
}
case PUBLIC = 0;
case PRIVATE = 1;

public function toLabel(): string
{
Expand Down
11 changes: 6 additions & 5 deletions src/app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public function user(): BelongsTo
return $this->belongsTo(User::class, 'user_id');
}

protected function visibility(): Attribute
protected $casts = [
'visibility' => PostVisibility::class,
];

public function getVisibilityLabelAttribute(): string
{
return Attribute::make(
get: fn ($value) => $value === 0 ? 'public' : 'private',
set: fn ($value) => $value === 'public' ? 0 : 1,
);
return $this->visibility->toLabel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ public function getEachUserPost(
UserId $userId,
PostId $postId
): ?PostEntity;

public function getOthersAllPosts(
int $userId,
int $perPage,
int $currentPage
): ?PaginationDto;

// public function getOneAllPosts(
// int $userId,
// int $targetUserId,
// int $perPage,
// int $currentPage
// ): PaginationDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function build(array $request): PostEntity
'content' => $request['content'],
'mediaPath' => $request['media_path'] ?? null,
'visibility' => new PostVisibility(
PostVisibilityEnum::fromString($request['visibility'])
PostVisibilityEnum::from($request['visibility'] ?? PostVisibilityEnum::PUBLIC)
Copy link

Copilot AI Jul 5, 2025

Choose a reason for hiding this comment

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

Default PostVisibilityEnum::PUBLIC is an enum instance, not its backing value; use PostVisibilityEnum::PUBLIC->value or ensure only an int is passed to from().

Suggested change
PostVisibilityEnum::from($request['visibility'] ?? PostVisibilityEnum::PUBLIC)
PostVisibilityEnum::from($request['visibility'] ?? PostVisibilityEnum::PUBLIC->value)

Copilot uses AI. Check for mistakes.

),
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace App\Post\Infrastructure\InfrastructureTest;

use App\Post\Domain\Entity\PostEntity;
use App\Post\Infrastructure\QueryService\GetPostQueryService;
use Tests\TestCase;
use App\Post\Application\QueryServiceInterface\GetPostQueryServiceInterface;
use App\Common\Application\DtoFactory\PaginationFactory;
use App\Common\Application\Dto\Pagination as PaginationDto;
use Mockery;
use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\Post;

class GetOthersAllPostsQueryServiceTest extends TestCase
{

private $user;
private int $currentPage = 1;
private int $perPage = 10;
protected function setUp(): void
{
parent::setUp();
$this->refresh();
$this->user = $this->createUsersWithPosts();
}

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 createUsersWithPosts(): User
{
$users = [];

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

$visibility = $i % 2 === 0 ? 0 : 1;
for ($j = 1; $j <= 20; $j++) {
Post::create([
'user_id' => $user->id,
'content' => "Post {$j} by User{$i}",
'media_path' => null,
'visibility' => $visibility,
'created_at' => now()->subMinutes(rand(0, 1000)),
'updated_at' => now(),
]);
}

$users[] = $user;
}

return $users[0];
}

public function test_check_query_service_return_type(): void
{
$queryService = new GetPostQueryService(
new Post(),
new User(),
);

$result = $queryService->getOthersAllPosts(
$this->user->id,
$this->perPage,
$this->currentPage,
);

$this->assertInstanceOf(
PaginationDto::class,
$result
);
}

public function test_check_query_service_return_value(): void
{
$queryService = new GetPostQueryService(
new Post(),
new User(),
);

$result = $queryService->getOthersAllPosts(
$this->user->id,
$this->perPage,
$this->currentPage,
);

$this->assertEquals($this->currentPage, $result->getCurrentPage());
$this->assertEquals($this->perPage, $result->getPerPage());
foreach ($result->getData() as $post) {
$this->assertInstanceOf(
PostEntity::class,
$post
);
}
}
}
24 changes: 24 additions & 0 deletions src/app/Post/Infrastructure/QueryService/GetPostQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Common\Application\Dto\Pagination as PaginationDto;
use App\Models\User;
use ErrorException;
use App\Common\Domain\Enum\PostVisibility as PostVisibilityEnum;

class GetPostQueryService implements GetPostQueryServiceInterface
{
Expand Down Expand Up @@ -84,4 +85,27 @@ private function paginationDto(
links: $data['links'] ?? null
);
}

public function getOthersAllPosts(
int $userId,
int $perPage,
int $currentPage
): ?PaginationDto {

$allPosts = $this->post
->where('user_id', '!=', $userId)
->where('visibility', PostVisibilityEnum::PUBLIC->value)
->paginate(
$perPage,
['*'],
'page',
$currentPage
);

if (empty($allPosts)) {
Copy link

Copilot AI Jul 5, 2025

Choose a reason for hiding this comment

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

Using empty() on a LengthAwarePaginator will always return false; use $allPosts->isEmpty() to detect when no records are returned.

Suggested change
if (empty($allPosts)) {
if ($allPosts->isEmpty()) {

Copilot uses AI. Check for mistakes.

return null;
}

return $this->paginationDto($allPosts->toArray());
}
}