Skip to content
This repository has been archived by the owner on Dec 11, 2023. It is now read-only.

Commit

Permalink
[backoffice-course] List all existing courses
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Sep 25, 2019
1 parent f8ced75 commit 8ecc12f
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 6 deletions.
4 changes: 4 additions & 0 deletions apps/backoffice/frontend/config/routes/api_courses.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
api_courses_get:
path: /api/courses
controller: CodelyTv\Apps\Backoffice\Frontend\Controller\Courses\ApiCoursesGetController
methods: [GET]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Apps\Backoffice\Frontend\Controller\Courses;

use CodelyTv\Backoffice\Courses\Application\BackofficeCourseResponse;
use CodelyTv\Backoffice\Courses\Application\BackofficeCoursesResponse;
use CodelyTv\Backoffice\Courses\Application\SearchAll\SearchAllBackofficeCoursesQuery;
use CodelyTv\Shared\Infrastructure\Symfony\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use function Lambdish\Phunctional\map;

final class ApiCoursesGetController extends Controller
{
public function __invoke(Request $request): JsonResponse
{
/** @var BackofficeCoursesResponse $response */
$response = $this->ask(new SearchAllBackofficeCoursesQuery());

return new JsonResponse(map($this->toArray(), $response->courses()));
}

private function toArray(): callable
{
return static function (BackofficeCourseResponse $course) {
return [
'id' => $course->id(),
'name' => $course->name(),
'duration' => $course->duration(),
];
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@
</div>
</div>
{{ include('pages/courses/partials/new_course_form.html.twig') }}
<div class="clearfix mb-10"></div>
<hr class="mb-10">
{{ include('pages/courses/partials/list_courses.html.twig') }}
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<h3 class="font-sans text-gray-800 text-center text-3xl mb-10">Cursos existentes</h3>

<table class="text-left w-full border-collapse">
<thead>
<tr>
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Id</th>
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Nombre</th>
<th class="py-4 px-6 bg-grey-lightest font-bold uppercase text-sm text-grey-dark border-b border-grey-light">Duración</th>
</tr>
</thead>
<tbody id="courses-list">

</tbody>
</table>

<script>
const tableBody = document.getElementById("courses-list");
fetch("{{ path('api_courses_get') }}")
.then(function (response) {
return response.json();
})
.then(function (courses) {
tableBody.innerHTML = "";
courses.forEach(function (course) {
let courseRow = document.createElement("tr");
courseRow.appendChild(tableCellFor(course.id));
courseRow.appendChild(tableCellFor(course.name));
courseRow.appendChild(tableCellFor(course.duration));
tableBody.appendChild(courseRow);
})
});
function tableCellFor(text) {
const cell = document.createElement("td");
cell.appendChild(document.createTextNode(text));
return cell;
}
</script>
34 changes: 34 additions & 0 deletions src/Backoffice/Courses/Application/BackofficeCourseResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application;

final class BackofficeCourseResponse
{
private $id;
private $name;
private $duration;

public function __construct(string $id, string $name, string $duration)
{
$this->id = $id;
$this->name = $name;
$this->duration = $duration;
}

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

public function name(): string
{
return $this->name;
}

public function duration(): string
{
return $this->duration;
}
}
22 changes: 22 additions & 0 deletions src/Backoffice/Courses/Application/BackofficeCoursesResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application;

use CodelyTv\Shared\Domain\Bus\Query\Response;

final class BackofficeCoursesResponse implements Response
{
private $courses;

public function __construct(BackofficeCourseResponse ...$courses)
{
$this->courses = $courses;
}

public function courses(): array
{
return $this->courses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application\Create;

use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;

final class BackofficeCourseCreator
{
private $repository;

public function __construct(BackofficeCourseRepository $repository)
{
$this->repository = $repository;
}

public function create(string $id, string $name, string $duration): void
{
$this->repository->save(new BackofficeCourse($id, $name, $duration));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

namespace CodelyTv\Backoffice\Courses\Application\Create;

use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;
use CodelyTv\Mooc\Courses\Domain\CourseCreatedDomainEvent;
use CodelyTv\Shared\Domain\Bus\Event\DomainEventSubscriber;

final class CreateBackofficeCourseOnCourseCreated implements DomainEventSubscriber
{
private $repository;
private $creator;

public function __construct(BackofficeCourseRepository $repository)
public function __construct(BackofficeCourseCreator $creator)
{
$this->repository = $repository;
$this->creator = $creator;
}

public static function subscribedTo(): array
Expand All @@ -25,6 +23,6 @@ public static function subscribedTo(): array

public function __invoke(CourseCreatedDomainEvent $event)
{
$this->repository->save(new BackofficeCourse($event->aggregateId(), $event->name(), $event->duration()));
$this->creator->create($event->aggregateId(), $event->name(), $event->duration());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application\SearchAll;

use CodelyTv\Backoffice\Courses\Application\BackofficeCourseResponse;
use CodelyTv\Backoffice\Courses\Application\BackofficeCoursesResponse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourse;
use CodelyTv\Backoffice\Courses\Domain\BackofficeCourseRepository;
use function Lambdish\Phunctional\map;

final class AllBackofficeCoursesSearcher
{
private $repository;

public function __construct(BackofficeCourseRepository $repository)
{
$this->repository = $repository;
}

public function searchAll(): BackofficeCoursesResponse
{
return new BackofficeCoursesResponse(...map($this->toResponse(), $this->repository->searchAll()));
}

private function toResponse(): callable
{
return static function (BackofficeCourse $course) {
return new BackofficeCourseResponse($course->id(), $course->name(), $course->duration());
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application\SearchAll;

use CodelyTv\Shared\Domain\Bus\Query\Query;

final class SearchAllBackofficeCoursesQuery implements Query
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types = 1);

namespace CodelyTv\Backoffice\Courses\Application\SearchAll;

use CodelyTv\Backoffice\Courses\Application\BackofficeCoursesResponse;
use CodelyTv\Shared\Domain\Bus\Query\QueryHandler;

final class SearchAllBackofficeCoursesQueryHandler implements QueryHandler
{
private $searcher;

public function __construct(AllBackofficeCoursesSearcher $searcher)
{
$this->searcher = $searcher;
}

public function __invoke(SearchAllBackofficeCoursesQuery $query): BackofficeCoursesResponse
{
return $this->searcher->searchAll();
}
}
2 changes: 2 additions & 0 deletions src/Backoffice/Courses/Domain/BackofficeCourseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface BackofficeCourseRepository
{
public function save(BackofficeCourse $course): void;

public function searchAll(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public function save(BackofficeCourse $course): void
{
$this->persist($course);
}

public function searchAll(): array
{
return $this->repository(BackofficeCourse::class)->findAll();
}
}

0 comments on commit 8ecc12f

Please sign in to comment.