While the existing tests are passing, let's refactor the Controller and move the logic for loading posts into a new `PostNodeRepository` class.
Doing this will make the Controller simpler and cleaner and make it easier to test that posts are returned in the correct order.
## Creating a PostNodeRepository
I like the Repository design pattern.
It's much better to have all logic to find and load nodes in one place instead of duplicating them across an application.
It also makes it easier to test.
To start, within your `atdc` module, create an `src/Repository` directory and a `PostNodeRepository.php` file inside it.
This will contain the `PostNodeRepository` class that will be responsible for loading the post nodes from the database instead of within `BlogPageController`.
I like to make my classes `final`, but this is optional and, by adding this docblock, we can specify the `findAll()` method should return an array of `NodeInterface` objects - making the code easier to read and providing better completion.
So far, you haven't changed `BlogPageController`, so the tests should still pass.
Next, let's move the logic for loading nodes into the Repository.
public static function create(ContainerInterface $container): self {
return new self(
$container->get(PostNodeRepository::class),
);
}
```
You may also need to add `use Symfony\Component\DependencyInjection\ContainerInterface;` at the top of the file for the correct `ContainerInterface` to be used.
## Creating a service
`$container->get()` uses a service's ID to retrieve it from the container, but `PostNodeRepository` isn't in the service container.
To do this, create an `atdc.services.yml` file within your module.
Add `PostNodeRepository` using the fully-qualified class name as the service name: