feat(blog): add city_id to blogs for city-specific scoping

- Introduced a new nullable city_id column in the blogs table to allow scoping of blog posts to specific cities.
- Updated Blog entity to include a ManyToOne relationship with the City entity.
- Enhanced BlogController to handle city_id in the request, allowing filtering of posts by city.
- Modified BlogRepository to support querying published posts based on city_id.
- Added tests to ensure correct behavior for city-scoped and nationwide posts, including creation and updating of posts with city associations.
This commit is contained in:
hamed
2026-07-19 08:23:57 +03:30
parent bd66b213c2
commit a4b07c2f80
10 changed files with 535 additions and 178 deletions
+27
View File
@@ -3,6 +3,7 @@
namespace App\Blog\Entity;
use App\Auth\Entity\User;
use App\Location\Entity\City;
use Doctrine\ORM\Mapping as ORM;
use App\Blog\Repository\BlogRepository;
use Symfony\Component\Uid\Uuid;
@@ -10,6 +11,7 @@ use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: BlogRepository::class)]
#[ORM\Table(name: 'blogs')]
#[ORM\Index(columns: ['status', 'created_at'], name: 'idx_blogs_status')]
#[ORM\Index(columns: ['city_id'], name: 'idx_blogs_city')]
class Blog
{
public const STATUS_DRAFT = 'draft';
@@ -46,6 +48,13 @@ class Blog
#[ORM\JoinColumn(name: 'author_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
private User $author;
// شهر پست. NULL معنای دائمی دارد: «پست سراسری» که روی دامنهٔ اصلی canonical
// می‌شود. سایت عمومی چند-دامنه‌ای بر پایهٔ همین تفکیک تصمیم می‌گیرد پست را روی
// دامنهٔ شهر نشان دهد یا روی دامنهٔ اصلی.
#[ORM\ManyToOne(targetEntity: City::class)]
#[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
private ?City $city = null;
#[ORM\Column(type: 'json')]
private array $tags = [];
@@ -80,6 +89,7 @@ class Blog
public function getAuthor(): User { return $this->author; }
public function getTags(): array { return $this->tags; }
public function getStatus(): string { return $this->status; }
public function getCity(): ?City { return $this->city; }
public function setTitle(string $v): self { $this->title = $v; $this->touch(); return $this; }
public function setSlug(string $v): self { $this->slug = $v; $this->touch(); return $this; }
@@ -89,6 +99,8 @@ class Blog
public function setImagePath(?string $v): self { $this->imagePath = $v; $this->touch(); return $this; }
public function setTags(array $v): self { $this->tags = $v; $this->touch(); return $this; }
public function setStatus(string $v): self { $this->status = $v; $this->touch(); return $this; }
/** null = پست سراسری (روی همهٔ دامنه‌ها، canonical روی دامنهٔ اصلی) */
public function setCity(?City $v): self { $this->city = $v; $this->touch(); return $this; }
private function touch(): void { $this->updatedAt = time(); }
@@ -112,11 +124,25 @@ class Blog
'tags' => $this->tags,
'status' => $this->status,
'author' => $this->authorToArray(),
'city' => $this->cityToArray(),
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
];
}
/** null = پست سراسری. مصرف‌کننده روی همین null تصمیم canonical می‌گیرد. */
private function cityToArray(): ?array
{
if ($this->city === null) {
return null;
}
return [
'id' => (string) $this->city->getId(),
'name' => $this->city->getName(),
];
}
private function authorToArray(): ?array
{
if ($this->author === null) {
@@ -144,6 +170,7 @@ class Blog
'image_url' => $this->imageUrl,
'tags' => $this->tags,
'status' => $this->status,
'city' => $this->cityToArray(),
'created_at' => $this->createdAt,
];
}