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
4 changes: 2 additions & 2 deletions src/app/Common/Domain/Enum/PostVisibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enum PostVisibility: int
public function toLabel(): string
{
return match ($this) {
self::PUBLIC => 'Public',
self::PRIVATE => 'Private',
self::PUBLIC => 'public',
self::PRIVATE => 'private',
};
}
}
1 change: 1 addition & 0 deletions src/app/Post/Application/UseCase/EditUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(
public function handle(
EditPostUseCommand $command
): EditPostDto {

$entity = PostEntity::build(
$command->toArray()
);
Expand Down
11 changes: 6 additions & 5 deletions src/app/Post/Domain/Entity/PostEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public static function build(array $request): self
visibility: $request['visibility'] instanceof PostVisibility
? $request['visibility']
: new PostVisibility( PostVisibilityEnum::from(
is_int($request['visibility'])
? $request['visibility']
: match (strtoupper((string)$request['visibility'])) {
'PUBLIC' => PostVisibilityEnum::PRIVATE->value,
default => PostVisibilityEnum::PUBLIC->value,
is_numeric($request['visibility'])
? (int)$request['visibility']
: match(strtoupper((string)$request['visibility'])) {
'PUBLIC' => PostVisibilityEnum::PUBLIC,
'PRIVATE' => PostVisibilityEnum::PRIVATE,
default => PostVisibilityEnum::PUBLIC,
}
))
);
Expand Down
1 change: 1 addition & 0 deletions src/app/Post/Infrastructure/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function editById(PostEntity $entity): PostEntity

$targetPost->fill([
'content' => $entity->getContent(),
'user_id' => $entity->getUserId()->getValue(),
'media_path' => $entity->getMediaPath(),
'visibility' => $entity->getPostVisibility()->getValue(),
])->save();
Expand Down
12 changes: 7 additions & 5 deletions src/app/Post/Presentation/Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use App\Common\Presentation\ViewModelFactory\PaginationFactory as PaginationViewModelFactory;
use App\Post\Application\Dto\GetUserEachPostDto;
use App\Post\Presentation\ViewModel\GetAllUserPostViewModel;
use App\Post\Application\UseCase\EditUseCase;
use App\Post\Application\UseCommand\EditPostUseCommand;
use App\Post\Presentation\ViewModel\EditPostViewModel;

class PostController extends Controller
{
Expand Down Expand Up @@ -121,18 +124,17 @@ public function getEachPost(

public function edit(
Request $request,
int $user_id,
int $post_id,
int $userId,
int $postId,
EditUseCase $useCase
): JsonResponse {
DB::connection('mysql')->beginTransaction();
try {
Comment on lines +127 to 132
Copy link

Copilot AI Jul 8, 2025

Choose a reason for hiding this comment

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

The controller's edit method expects both $userId and $postId parameters, but the route only supplies {postId}. Update the signature to accept just $postId or adjust the route to provide both parameters.

Suggested change
int $userId,
int $postId,
EditUseCase $useCase
): JsonResponse {
DB::connection('mysql')->beginTransaction();
try {
int $postId,
EditUseCase $useCase
): JsonResponse {
DB::connection('mysql')->beginTransaction();
try {
$userId = auth()->id(); // Retrieve the authenticated user's ID

Copilot uses AI. Check for mistakes.


$command = EditPostUseCommand::build(
array_merge(
$request->toArray(),
['userId' => $user_id],
['id' => $post_id]
['userId' => $userId],
['id' => $postId]
)
);

Expand Down
3 changes: 2 additions & 1 deletion src/app/Post/Tests/Post_CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Post\Tests;

use App\Common\Domain\Enum\PostVisibility as PostVisibilityEnum;
use App\Models\Post;
use App\Models\User;
use Tests\TestCase;
Expand Down Expand Up @@ -50,7 +51,7 @@ public function test_create_post(): void
'title' => 'Portugal Wins Nations League in 2025',
'content' => 'Vamos Portugal! The team has shown incredible skill and determination.',
'media_path' => 'https://example.com/media.jpg',
'visibility' => 'public',
'visibility' => PostVisibilityEnum::PUBLIC->value,
];

$response = $this->post(
Expand Down
16 changes: 8 additions & 8 deletions src/app/Post/Tests/Post_EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,35 @@ public function test_feature_test(): void
'profile_image' => null,
];

$user_id = User::create($request)->id;
$userId = User::create($request)->id;

$postRequest = [
'content' => 'Vamos Portugal! The team has shown incredible skill and determination.',
'media_path' => 'https://example.com/media.jpg',
'visibility' => 'public',
'visibility' => PostVisibilityEnum::PUBLIC->value,
];

$post_id = Post::create(array_merge($postRequest, ['user_id' => $user_id]))->id;
$postId = Post::create(array_merge($postRequest, ['user_id' => $userId]))->id;

$editRequest = [
'content' => 'Vamos Portugal! The team has shown incredible skill and determination. Updated content.',
'media_path' => 'https://example.com/media_updated.jpg',
'visibility' => 'private',
'visibility' => PostVisibilityEnum::PRIVATE->value,
];

$response = $this->putJson(
"api/users/{$user_id}/posts/{$post_id}",
"api/users/{$userId}/posts/{$postId}",
$editRequest
);

$response->assertJson([
'status' => 'success',
'data' => [
'id' => $post_id,
'user_id' => $user_id,
'id' => $postId,
'user_id' => $userId,
'content' => $editRequest['content'],
'media_path' => $editRequest['media_path'],
'visibility' => PostVisibilityEnum::PRIVATE->value,
'visibility' => PostVisibilityEnum::PRIVATE->toLabel(),
]
]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
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');
Route::put('{postId}', [PostController::class, 'edit'])->name('edit');
});
});