add Comment persistence: model, migration, eloquent + fake repo

This commit is contained in:
Yisroel Baum 2026-05-06 22:14:11 +03:00
parent 0d589340d9
commit 93da08756c
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
4 changed files with 219 additions and 0 deletions

View file

@ -0,0 +1,43 @@
<?php
namespace App\Comment;
use DateTimeImmutable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property int $post_id
* @property int $user_id
* @property string $body
* @property DateTimeImmutable $created_at
*
* @method static Builder<static>|CommentModel newModelQuery()
* @method static Builder<static>|CommentModel newQuery()
* @method static Builder<static>|CommentModel query()
* @method static Builder<static>|CommentModel whereId($value)
* @method static Builder<static>|CommentModel wherePostId($value)
* @method static Builder<static>|CommentModel whereUserId($value)
* @method static Builder<static>|CommentModel whereBody($value)
* @method static Builder<static>|CommentModel whereCreatedAt($value)
*
* @mixin \Eloquent
*/
class CommentModel extends Model
{
protected $table = 'comments';
public $timestamps = false;
protected $fillable = [
'post_id',
'user_id',
'body',
'created_at',
];
protected $casts = [
'created_at' => 'immutable_datetime',
];
}

View file

@ -0,0 +1,66 @@
<?php
namespace App\Comment;
use DateTimeImmutable;
use DateTimeZone;
class EloquentCommentRepository implements CommentRepository
{
public function create(CreateCommentDto $dto): Comment
{
$model = CommentModel::create([
'post_id' => $dto->postId,
'user_id' => $dto->userId,
'body' => $dto->body,
'created_at' => $dto->createdAt,
]);
return $this->toDomain($model);
}
public function find(int $id): ?Comment
{
$model = CommentModel::find($id);
return $model === null ? null : $this->toDomain($model);
}
/**
* @return Comment[]
*/
public function findByPostId(int $postId): array
{
$models = CommentModel::query()
->where('post_id', $postId)
->orderBy('created_at', 'asc')
->get();
return $models->map(
function (CommentModel $model) {
return $this->toDomain($model);
},
)->all();
}
public function delete(int $id): void
{
CommentModel::query()->where('id', $id)->delete();
}
private function toDomain(CommentModel $model): Comment
{
$utc = new DateTimeZone('UTC');
return new Comment(
id: $model->id,
postId: $model->post_id,
userId: $model->user_id,
body: $model->body,
createdAt: new DateTimeImmutable(
$model->created_at->toDateTimeString(),
$utc,
),
);
}
}

View file

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')
->constrained('posts')
->cascadeOnDelete();
$table->foreignId('user_id')
->constrained('users')
->cascadeOnDelete();
$table->text('body');
$table->dateTime('created_at')->index();
});
}
public function down(): void
{
Schema::dropIfExists('comments');
}
};

View file

@ -0,0 +1,82 @@
<?php
namespace Tests\Fakes;
use App\Comment\Comment;
use App\Comment\CommentRepository;
use App\Comment\CreateCommentDto;
class FakeCommentRepository implements CommentRepository
{
/**
* @var Comment[]
*/
private array $existingComments = [];
public function create(CreateCommentDto $dto): Comment
{
$id = $this->getNextId();
$comment = new Comment(
id: $id,
postId: $dto->postId,
userId: $dto->userId,
body: $dto->body,
createdAt: $dto->createdAt,
);
$this->existingComments[$id] = $comment;
return $this->copy($comment);
}
public function find(int $id): ?Comment
{
$comment = $this->existingComments[$id] ?? null;
if ($comment === null) {
return null;
}
return $this->copy($comment);
}
/**
* @return Comment[]
*/
public function findByPostId(int $postId): array
{
$matching = [];
foreach ($this->existingComments as $comment) {
if ($comment->getPostId() === $postId) {
$matching[] = $this->copy($comment);
}
}
usort(
$matching,
function (Comment $left, Comment $right) {
return $left->getCreatedAt() <=> $right->getCreatedAt();
},
);
return $matching;
}
public function delete(int $id): void
{
unset($this->existingComments[$id]);
}
private function copy(Comment $comment): Comment
{
return new Comment(
id: $comment->getId(),
postId: $comment->getPostId(),
userId: $comment->getUserId(),
body: $comment->getBody(),
createdAt: $comment->getCreatedAt(),
);
}
private function getNextId(): int
{
return count($this->existingComments) + 1;
}
}