Skip to content

Commit a74ced2

Browse files
committed
feat: update category management to use 'label' instead of 'title'
1 parent 0b35cee commit a74ced2

23 files changed

+188
-383
lines changed

contexts/CategoryManagement/Application/Coordinators/CategoryManagementCoordinator.php

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,20 @@ class CategoryManagementCoordinator extends BaseCoordinator
1919
{
2020
public function __construct(
2121
private CategoryRepository $repository
22-
) {}
23-
24-
public function create(CreateCategoryDTO $data): Category
25-
{
26-
$category = match ($data->status) {
27-
'draft' => $this->createDraft($data),
28-
'published' => $this->createPublished($data),
29-
default => throw new \InvalidArgumentException('Invalid category status'),
30-
};
31-
32-
$this->dispatchDomainEvents($category);
33-
34-
return $category;
35-
}
36-
37-
private function createDraft(CreateCategoryDTO $data): Category
38-
{
39-
$category = Category::createDraft(
40-
new CategoryId(0),
41-
$data->title,
42-
$data->body,
43-
$data->created_at ? CarbonImmutable::parse($data->created_at) : null
44-
);
45-
46-
return $this->repository->create($category);
22+
) {
4723
}
4824

49-
private function createPublished(CreateCategoryDTO $data): Category
25+
public function create(CreateCategoryDTO $data): Category
5026
{
51-
$category = Category::createPublished(
27+
$category = Category::create(
5228
new CategoryId(0),
53-
$data->title,
54-
$data->body,
29+
$data->label,
5530
$data->created_at ? CarbonImmutable::parse($data->created_at) : null
5631
);
5732

5833
return $this->repository->create($category);
5934
}
6035

61-
public function publishDraft(int $id): void
62-
{
63-
$category = $this->repository->getById(new CategoryId($id));
64-
$category->publish();
65-
66-
$this->repository->update($category);
67-
68-
$this->dispatchDomainEvents($category);
69-
}
70-
7136
public function getCategory(int $id): Category
7237
{
7338
return $this->repository->getById(new CategoryId($id));
@@ -81,9 +46,8 @@ public function getCategoryList(GetCategoryListDTO $data): LengthAwarePaginator
8146
public function updateCategory(int $id, UpdateCategoryDTO $data): Category
8247
{
8348
$category = $this->repository->getById(new CategoryId($id));
84-
$category->revise(
85-
$data->title,
86-
$data->body,
49+
$category->modify(
50+
$data->label,
8751
$data->status ? CategoryStatus::fromString($data->status) : null,
8852
$data->created_at ? CarbonImmutable::parse($data->created_at) : null
8953
);
@@ -95,10 +59,10 @@ public function updateCategory(int $id, UpdateCategoryDTO $data): Category
9559
return $category;
9660
}
9761

98-
public function archiveCategory(int $id)
62+
public function subspendCategory(int $id)
9963
{
10064
$category = $this->repository->getById(new CategoryId($id));
101-
$category->archive();
65+
$category->subspend();
10266

10367
$this->repository->update($category);
10468

contexts/CategoryManagement/Application/DTOs/CreateCategoryDTO.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
class CreateCategoryDTO
88
{
99
public function __construct(
10-
public readonly string $title,
11-
public readonly string $body,
12-
public readonly string $status,
10+
public readonly string $label,
1311
public readonly ?string $created_at
14-
) {}
12+
) {
13+
}
1514

1615
public static function fromRequest(array $data): self
1716
{
1817
return new self(
19-
$data['title'],
20-
$data['body'] ?? '',
21-
$data['status'] ?? 'draft',
18+
$data['label'],
2219
$data['created_at'] ?? null,
2320
);
2421
}

contexts/CategoryManagement/Application/DTOs/GetCategoryListDTO.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ class GetCategoryListDTO
88
{
99
public function __construct(
1010
public readonly ?string $id,
11-
public readonly ?string $title,
11+
public readonly ?string $label,
1212
public readonly ?string $status,
1313
public readonly ?array $createdAtRange,
1414
public readonly int $page,
1515
public readonly int $perPage,
16-
) {}
16+
) {
17+
}
1718

1819
public static function fromRequest(array $data): self
1920
{
2021
return new self(
2122
$data['id'] ?? null,
22-
$data['title'] ?? null,
23+
$data['label'] ?? null,
2324
$data['status'] ?? null,
2425
$data['created_at_range'] ?? null,
2526
$data['page'] ?? 1,
@@ -31,7 +32,7 @@ public function toCriteria(): array
3132
{
3233
return [
3334
'id' => $this->id,
34-
'title' => $this->title,
35+
'label' => $this->label,
3536
'status' => $this->status,
3637
'created_at_range' => $this->createdAtRange,
3738
];

contexts/CategoryManagement/Application/DTOs/UpdateCategoryDTO.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
class UpdateCategoryDTO
88
{
99
public function __construct(
10-
public readonly ?string $title,
11-
public readonly ?string $body,
10+
public readonly ?string $label,
1211
public readonly ?string $status,
1312
public readonly ?string $created_at
14-
) {}
13+
) {
14+
}
1515

1616
public static function fromRequest(array $data): self
1717
{
1818
return new self(
19-
$data['title'] ?? null,
20-
$data['body'] ?? null,
19+
$data['label'] ?? null,
2120
$data['status'] ?? null,
2221
$data['created_at'] ?? null,
2322
);

contexts/CategoryManagement/Domain/Events/CategoryPublishedEvent.php renamed to contexts/CategoryManagement/Domain/Events/CategoryCreatedEvent.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
use Contexts\CategoryManagement\Domain\Models\CategoryId;
88
use Illuminate\Foundation\Events\Dispatchable;
99

10-
class CategoryPublishedEvent
10+
class CategoryCreatedEvent
1111
{
1212
use Dispatchable;
1313

1414
public function __construct(
1515
private readonly CategoryId $CategoryId,
16-
) {}
16+
) {
17+
}
1718

1819
public function getCategoryId(): CategoryId
1920
{

contexts/CategoryManagement/Domain/Models/Category.php

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,26 @@
66

77
use App\Http\DomainModel\BaseDomainModel;
88
use Carbon\CarbonImmutable;
9-
use Contexts\CategoryManagement\Domain\Events\CategoryPublishedEvent;
9+
use Contexts\CategoryManagement\Domain\Events\CategoryCreatedEvent;
1010

1111
class Category extends BaseDomainModel
1212
{
1313
private function __construct(
1414
public CategoryId $id,
15-
private string $title,
16-
private string $body,
15+
private string $label,
1716
private CategoryStatus $status,
1817
private ?CarbonImmutable $created_at = null,
1918
private ?CarbonImmutable $updated_at = null
2019
) {
2120
$this->created_at = $created_at ?? CarbonImmutable::now();
2221
}
2322

24-
public function revise(
25-
?string $newTitle,
26-
?string $newBody,
23+
public function modify(
24+
?string $newLabel,
2725
?CategoryStatus $newStatus,
2826
?CarbonImmutable $newCreatedAt = null,
2927
) {
30-
$this->title = $newTitle ?? $this->title;
31-
$this->body = $newBody ?? $this->body;
28+
$this->label = $newLabel ?? $this->label;
3229
if ($newStatus && ! $this->status->equals($newStatus)) {
3330
$this->transitionStatus($newStatus);
3431
}
@@ -37,52 +34,35 @@ public function revise(
3734

3835
public static function reconstitute(
3936
CategoryId $id,
40-
string $title,
41-
string $body,
37+
string $label,
4238
CategoryStatus $status,
4339
?CarbonImmutable $created_at = null,
4440
?CarbonImmutable $updated_at = null,
4541
array $events = []
4642
): self {
47-
$category = new self($id, $title, $body, $status, $created_at, $updated_at);
43+
$category = new self($id, $label, $status, $created_at, $updated_at);
4844
foreach ($events as $event) {
4945
$category->recordEvent($event);
5046
}
5147

5248
return $category;
5349
}
5450

55-
public static function createDraft(
51+
public static function create(
5652
CategoryId $id,
57-
string $title,
58-
string $body,
53+
string $label,
5954
?CarbonImmutable $created_at = null,
6055
?CarbonImmutable $updated_at = null
6156
): self {
62-
return new self($id, $title, $body, CategoryStatus::draft(), $created_at, $updated_at);
63-
}
64-
65-
public static function createPublished(
66-
CategoryId $id,
67-
string $title,
68-
string $body,
69-
?CarbonImmutable $created_at = null,
70-
?CarbonImmutable $updated_at = null
71-
): self {
72-
$category = new self($id, $title, $body, CategoryStatus::published(), $created_at, $updated_at);
73-
$category->recordEvent(new CategoryPublishedEvent($category->id));
57+
$category = new self($id, $label, CategoryStatus::active(), $created_at, $updated_at);
58+
$category->recordEvent(new CategoryCreatedEvent($category->id));
7459

7560
return $category;
7661
}
7762

78-
public function publish()
63+
public function subspend()
7964
{
80-
$this->transitionStatus(CategoryStatus::published());
81-
}
82-
83-
public function archive()
84-
{
85-
$this->transitionStatus(CategoryStatus::archived());
65+
$this->transitionStatus(CategoryStatus::subspended());
8666
}
8767

8868
public function delete()
@@ -95,7 +75,6 @@ private function transitionStatus(CategoryStatus $targetStatus): void
9575
$this->status = $this->status->transitionTo($targetStatus);
9676

9777
match ($this->status->getValue()) {
98-
CategoryStatus::PUBLISHED => $this->recordEvent(new CategoryPublishedEvent($this->id)),
9978
default => null,
10079
};
10180
}
@@ -105,14 +84,9 @@ public function getId(): CategoryId
10584
return $this->id;
10685
}
10786

108-
public function getTitle(): string
109-
{
110-
return $this->title;
111-
}
112-
113-
public function getbody(): string
87+
public function getLabel(): string
11488
{
115-
return $this->body;
89+
return $this->label;
11690
}
11791

11892
public function getCreatedAt(): CarbonImmutable

contexts/CategoryManagement/Domain/Models/CategoryStatus.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,27 @@
88

99
class CategoryStatus
1010
{
11-
public const DRAFT = 'draft';
11+
public const SUBSPENDED = 'subspended';
1212

13-
public const PUBLISHED = 'published';
14-
15-
public const ARCHIVED = 'archived';
13+
public const ACTIVE = 'active';
1614

1715
public const DELETED = 'deleted';
1816

1917
public function __construct(private string $value)
2018
{
21-
if (! in_array($value, [self::DRAFT, self::PUBLISHED, self::ARCHIVED, self::DELETED])) {
19+
if (! in_array($value, [self::SUBSPENDED, self::ACTIVE, self::DELETED])) {
2220
throw new InvalidArgumentException('Invalid category status');
2321
}
2422
}
2523

26-
public static function draft(): self
27-
{
28-
return new self(self::DRAFT);
29-
}
30-
31-
public static function published(): self
24+
public static function subspended(): self
3225
{
33-
return new self(self::PUBLISHED);
26+
return new self(self::SUBSPENDED);
3427
}
3528

36-
public static function archived(): self
29+
public static function active(): self
3730
{
38-
return new self(self::ARCHIVED);
31+
return new self(self::ACTIVE);
3932
}
4033

4134
public static function deleted(): self
@@ -46,9 +39,8 @@ public static function deleted(): self
4639
public function transitionTo(CategoryStatus $target): self
4740
{
4841
$validTransitions = match ($this->value) {
49-
self::DRAFT => [self::PUBLISHED, self::ARCHIVED, self::DELETED],
50-
self::PUBLISHED => [self::ARCHIVED, self::DRAFT],
51-
self::ARCHIVED => [self::DRAFT, self::PUBLISHED, self::DELETED],
42+
self::SUBSPENDED => [self::ACTIVE, self::DELETED],
43+
self::ACTIVE => [self::SUBSPENDED, self::DELETED],
5244
default => [],
5345
};
5446

@@ -62,7 +54,7 @@ public function transitionTo(CategoryStatus $target): self
6254

6355
public static function fromString(string $status): self
6456
{
65-
if (! in_array($status, [self::DRAFT, self::PUBLISHED, self::ARCHIVED, self::DELETED])) {
57+
if (! in_array($status, [self::SUBSPENDED, self::ACTIVE, self::DELETED])) {
6658
throw new InvalidArgumentException('Invalid category status');
6759
}
6860

contexts/CategoryManagement/Infrastructure/Migrations/2025_03_01_022301_create_categories_table.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66
use Illuminate\Database\Schema\Blueprint;
77
use Illuminate\Support\Facades\Schema;
88

9-
return new class extends Migration
10-
{
9+
return new class () extends Migration {
1110
/**
1211
* Run the migrations.
1312
*/
1413
public function up(): void
1514
{
1615
Schema::create('categories', function (Blueprint $table) {
1716
$table->increments('id');
18-
$table->string('title')->default('');
19-
$table->text('body')->nullable();
20-
$table->tinyInteger('status')->default(0)->comment('0: draft, 1: published, 2: archived, 3: deleted');
17+
$table->string('label')->default('');
18+
$table->tinyInteger('status')->default(0)->comment('0: subspended, 1: active, 2: deleted');
2119
$table->timestamps();
2220
});
2321
}

0 commit comments

Comments
 (0)