extend Post entity with feature slot

Adds nullable feature_slot column (unique) plus repo
findByFeatureSlot/findFeatured/update methods so admins can
pin a post into one of two slots.
This commit is contained in:
Yisroel Baum 2026-05-06 22:28:45 +03:00
parent 64a334c63e
commit f73e5a1af5
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
6 changed files with 160 additions and 4 deletions

View file

@ -4,6 +4,7 @@ namespace App\Post;
use DateTimeImmutable;
use DateTimeZone;
use RuntimeException;
class EloquentPostRepository implements PostRepository
{
@ -65,6 +66,53 @@ class EloquentPostRepository implements PostRepository
PostModel::query()->where('id', $id)->delete();
}
/**
* @throws RuntimeException
*/
public function update(Post $post): Post
{
$model = PostModel::find($post->getId());
if ($model === null) {
throw new RuntimeException(
"Post with id: {$post->getId()} does not exist"
);
}
$model->user_id = $post->getUserId();
$model->title = $post->getTitle();
$model->body = $post->getBody();
$model->created_at = $post->getCreatedAt();
$model->feature_slot = $post->getFeatureSlot();
$model->save();
return $this->toDomain($model);
}
public function findByFeatureSlot(int $slot): ?Post
{
$model = PostModel::query()
->where('feature_slot', $slot)
->first();
return $model === null ? null : $this->toDomain($model);
}
/**
* @return Post[]
*/
public function findFeatured(): array
{
$models = PostModel::query()
->whereNotNull('feature_slot')
->orderBy('feature_slot', 'asc')
->get();
return $models->map(
function (PostModel $model) {
return $this->toDomain($model);
},
)->all();
}
private function toDomain(PostModel $model): Post
{
$utc = new DateTimeZone('UTC');
@ -78,6 +126,7 @@ class EloquentPostRepository implements PostRepository
$model->created_at->toDateTimeString(),
$utc,
),
featureSlot: $model->feature_slot,
);
}
}