Skip to content

Commit

Permalink
update getting started
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 15, 2023
1 parent a5bf255 commit 641af49
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ First we define the events that happen in our system.
A hotel can be created with a `name` and a `id`:

```php
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;

#[Event('hotel.created')]
final class HotelCreated
{
public function __construct(
public readonly string $hotelId,
public readonly BasicAggregateRootId $hotelId,
public readonly string $hotelName
) {
}
Expand Down Expand Up @@ -60,13 +62,16 @@ These events are thrown here and the state of the hotel is also changed.
```php
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\AggregateId;
use Patchlevel\EventSourcing\Attribute\Apply;

#[Aggregate('hotel')]
final class Hotel extends BasicAggregateRoot
{
private string $id;
#[AggregateId]
private BasicAggregateRootId $id;
private string $name;

/**
Expand All @@ -84,7 +89,7 @@ final class Hotel extends BasicAggregateRoot
return $this->guests;
}

public static function create(string $id, string $hotelName): static
public static function create(BasicAggregateRootId $id, string $hotelName): static
{
$self = new static();
$self->recordThat(new HotelCreated($id, $hotelName));
Expand Down Expand Up @@ -134,11 +139,6 @@ final class Hotel extends BasicAggregateRoot
)
);
}

public function aggregateRootId(): string
{
return $this->id;
}
}
```

Expand Down Expand Up @@ -189,7 +189,7 @@ final class HotelProjection implements Projector
$this->db->insert(
'hotel',
[
'id' => $event->hotelId,
'id' => $message->aggregateId(),
'name' => $event->hotelName,
'guests' => 0
]
Expand Down Expand Up @@ -356,14 +356,16 @@ $projectionist->boot();
We are now ready to use the Event Sourcing System. We can load, change and save aggregates.

```php
$hotel1 = Hotel::create('1', 'HOTEL');
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId;

$hotel1 = Hotel::create(new BasicAggregateRootId('1'), 'HOTEL');
$hotel1->checkIn('David');
$hotel1->checkIn('Daniel');
$hotel1->checkOut('David');

$hotelRepository->save($hotel1);

$hotel2 = $hotelRepository->load('2');
$hotel2 = $hotelRepository->load(new BasicAggregateRootId('2'));
$hotel2->checkIn('David');
$hotelRepository->save($hotel2);

Expand Down

0 comments on commit 641af49

Please sign in to comment.