Skip to content

Commit

Permalink
feat(school): add school in new arch (#384)
Browse files Browse the repository at this point in the history
* feat: add school in new arch

* lint
  • Loading branch information
Lundli authored Oct 30, 2023
1 parent 5eb4189 commit 775abd9
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Controller/Api/SchoolApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Controller\Api;

use App\Core\Application\DTO\SchoolDTO;
use App\Core\Application\UseCase\SchoolUseCase;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes as OA;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

#[OA\Tag(name: 'School', description: 'School API endpoints')]
class SchoolApiController extends AbstractController
{
public function __construct(private readonly SchoolUseCase $schoolUseCase)
{
}

#[OA\Response(response: 200, description: 'Schools found', content: new OA\JsonContent(type: 'array', items: new OA\Items(new Model(type: SchoolDTO::class))))]
#[Route('/api/department/{departmentId}/schools', name: 'api_schools_by_department_id', methods: ['GET'])]
public function getSchoolsByDepartment(int $departmentId): JsonResponse
{
return new JsonResponse($this->schoolUseCase->getSchoolsByDepartmentId($departmentId));
}
}
1 change: 1 addition & 0 deletions src/Controller/SponsorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function sponsorEdit(Request $request, Sponsor $sponsor = null): Redirect
$this->fileUploader->deleteSponsor($oldImgPath);

$sponsor->setLogoImagePath($imgPath);

// Else use the old image.
} else {
$sponsor->setLogoImagePath($oldImgPath);
Expand Down
57 changes: 57 additions & 0 deletions src/Core/Application/DTO/SchoolDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Core\Application\DTO;

use App\Core\Domain\Entity\School;

class SchoolDTO implements \JsonSerializable
{
private ?int $id = null;
private ?string $name = null;
private ?string $contactPerson = null;
private ?string $department = null;
private ?string $email = null;
private ?string $phone = null;
private ?bool $international = null;
private ?bool $active = null;

public function __construct(?int $id, ?string $name, ?string $contactPerson, ?string $department, ?string $email, ?string $phone, ?bool $international, ?bool $active)
{
$this->id = $id;
$this->name = $name;
$this->contactPerson = $contactPerson;
$this->department = $department;
$this->email = $email;
$this->phone = $phone;
$this->international = $international;
$this->active = $active;
}

public static function createFromEntity(School $school): SchoolDTO
{
return new SchoolDTO(
$school->getId(),
$school->getName(),
$school->getContactPerson(),
$school->getDepartment()->getShortName(),
$school->getEmail(),
$school->getPhone(),
$school->isInternational(),
$school->isActive()
);
}

public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'contactPerson' => $this->contactPerson,
'department' => $this->department,
'email' => $this->email,
'phone' => $this->phone,
'international' => $this->international,
'active' => $this->active,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Core\Application\UseCase\Interfaces\Persistence;

interface ISchoolRepository
{
public function findByDepartmentId(int $departmentId): array;
}
26 changes: 26 additions & 0 deletions src/Core/Application/UseCase/SchoolUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Core\Application\UseCase;

use App\Core\Application\DTO\SchoolDTO;
use App\Core\Application\UseCase\Interfaces\Persistence\ISchoolRepository;
use Psr\Log\LoggerInterface;

class SchoolUseCase
{
public function __construct(private ISchoolRepository $schoolRepository, private LoggerInterface $logger)
{
}

public function getSchoolsByDepartmentId(int $departmentId): array
{
$schools = $this->schoolRepository->findByDepartmentId($departmentId);

$schoolDTOs = [];
foreach ($schools as $school) {
$schoolDTOs[] = SchoolDTO::createFromEntity($school);
}

return $schoolDTOs;
}
}
28 changes: 28 additions & 0 deletions src/Core/Infrastructure/Persistence/SchoolRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Core\Infrastructure\Persistence;

use App\Core\Application\UseCase\Interfaces\Persistence\ISchoolRepository;
use App\Core\Domain\Entity\School;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class SchoolRepository extends ServiceEntityRepository implements ISchoolRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, School::class);
}

public function findByDepartmentId(int $departmentId): array
{
$result = $this->createQueryBuilder('school')
->select('school')
->where('school.department = :department')
->setParameter('department', $departmentId)
->getQuery()
->getResult();

return $result;
}
}

0 comments on commit 775abd9

Please sign in to comment.