initial rest api for react.js

This commit is contained in:
2026-06-29 18:54:40 +02:00
parent 2eb770a328
commit 22da85872d
17 changed files with 1999 additions and 649 deletions
@@ -0,0 +1,18 @@
<?php
namespace App\Controller\Api;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AuthController extends AbstractController
{
#[Route('/api/auth/login', name: 'api_login', methods: ['POST'])]
public function login(): JsonResponse
{
throw new \LogicException('Intercepted by firewall.');
}
}
@@ -0,0 +1,61 @@
<?php
namespace App\Controller\Api;
use App\Entity\Clients;
use App\Repository\ClientsRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api/clients', name: 'api_')]
class ClientsController extends AbstractController
{
public function __construct(
private ClientsRepository $repo,
private SerializerInterface $serializer,
) {}
#[Route('', name: 'list', methods: ['GET'])]
public function list(): JsonResponse
{
$products = $this->repo->findAll();
$prepare = [];
foreach ($products as $product) {
$prepare[] = [
'id' => $product->getId(),
'strName' => $product->getStrName(),
'strAddress' => $product->getStrAddress(),
'dateLegitimacy' => $product->getDateLegitimacy()->format('Y-m-d'),
'dateProclamation' => $product->getDateProclamation()->format('Y-m-d'),
'extendInfo' => [
'intNip' => $product->getIntNIP(),
'intPhone' => $product->getIntPhone(),
'strEmail' => $product->getStrEmail(),
]
];
}
return $this->json($prepare, context: ['groups' => ['client:read']]);
}
#[Route('/add', name: 'add_client', methods: ['POST'])]
public function add(Request $request, EntityManagerInterface $entityManager): JsonResponse
{
return $this->json($request->toArray(), context: ['groups' => ['client:add', 'client:read']]);
}
// #[Route('/{id}', name: 'show', methods: ['GET'])]
// public function show(Product $product): JsonResponse
// {
// return $this->json($product, context: ['groups' => ['product:read']]);
// }
//
// #[Route('', name: 'create', methods: ['POST'])]
// public function create(Request $request): JsonResponse
// {
// $data = $request->toArray();
// // walidacja + zapis...
// return $this->json($product, 201, context: ['groups' => ['product:read']]);
// }
}
@@ -0,0 +1,72 @@
<?php
namespace App\Controller\Api;
use App\Entity\Reports;
use App\Entity\ReportsTodo;
use App\Enum\ReportStatus;
use App\Enum\ReportTodoEnum;
use App\Helper\MonthHelper;
use App\Repository\ReportsRepository;
use App\Services\AddReports;
use App\Services\OverdueReports;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
#[Route('/api/reports', name: 'api_reports')]
class ReportsController extends AbstractController
{
public function __construct(
private readonly ReportsRepository $repo,
private readonly OverdueReports $overdueReports
)
{
}
#[Route('/{monthId}', name: 'api_reports_all', methods: ['GET'])]
public function getReports(int $monthId): JsonResponse
{
$monthHelper = new MonthHelper($this->overdueReports);
$clientId=0;
$monthCurrentDate = $monthHelper->getCurrentMonthDate($monthId);
$reportsAll = $this->repo->findAllInMonth($monthCurrentDate,$clientId);
return $this->json($reportsAll, context: ['groups' => ['reportsAll:read','reportstodo:read']]);
}
#[Route('/todo', name: 'api_reports_todo', methods: ['POST'])]
public function updateReportTodo(Request $request, AddReports $reportsService, EntityManagerInterface $entityManager): JsonResponse
{
$reportId = $request->toArray()['reportId'];
$todoName = $request->toArray()['todoName'];
$nameObj = ReportTodoEnum::tryFromNameStatic($todoName);
if (!$nameObj) {
throw $this->createNotFoundException();
}
$reportReference = $entityManager->getReference(Reports::class, $reportId);
$todo = $entityManager->getRepository(ReportsTodo::class)->findOneBy(['name' => $nameObj, 'report' => $reportReference]);
if($todo){
$todo->setDeleted(!$todo->isDeleted());
} else {
$todo = new ReportsTodo();
$todo->setName($nameObj);
$todo->setReport($reportReference);
}
$reportReference->addReportsTodo($todo);
$entityManager->persist($todo);
$entityManager->flush();
$entityManager->getCache()->evictCollection(Reports::class,"reportsTodos",$reportReference->getId());
return $this->json([
'reportId' => $reportId,
'reportStatusTodo' => $reportReference->getStatusTodo(),
'todoItem' => $todo
], context: ['groups' => ['reportstodo:read']]);
}
}
+14
View File
@@ -8,6 +8,8 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: ClientsRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'client_cache')]
@@ -16,41 +18,53 @@ class Clients
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['client:read'])]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['client:read','reportsAll:read'])]
private ?string $strName = null;
#[ORM\Column(type: Types::BIGINT)]
#[Groups(['client:read'])]
private ?int $intNIP = null;
#[ORM\Column(length: 255)]
#[Groups(['client:read'])]
private ?string $strAddress = null;
#[ORM\Column(length: 255)]
#[Groups(['client:read'])]
private ?string $strEmail = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Groups(['client:read'])]
private ?\DateTimeInterface $dateLegitimacy = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Groups(['client:read'])]
private ?\DateTimeInterface $dateProclamation = null;
/**
* @var Collection<int, Schedule>
*/
#[Groups(['client:read'])]
#[ORM\OneToMany(targetEntity: Schedule::class, mappedBy: 'client')]
private Collection $schedules;
#[Groups(['client:read'])]
#[ORM\Column(options: ["default" => "0"])]
private bool $isDeleted = false;
#[Groups(['client:read'])]
#[ORM\Column(options: ["default" => "0"])]
private bool $isComplete = false;
#[Groups(['client:read'])]
#[ORM\Column(nullable: true)]
private ?int $intPhone = null;
#[Groups(['client:read'])]
#[ORM\Column(enumType: ReportHighlight::class, options: ["default" => "0"])]
private ReportHighlight $highlight = ReportHighlight::STANDARD;
+28
View File
@@ -13,6 +13,7 @@ use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: ReportsRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'report_cache')]
@@ -22,13 +23,16 @@ class Reports
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['report:read','reportsAll:read'])]
private ?int $id = null;
#[ORM\ManyToOne]
#[ORM\JoinColumn(referencedColumnName: "id", nullable: false)]
#[Groups(['report:read','reportsAll:read'])]
private ?Clients $client = null;
#[ORM\Column(type: Types::DATE_MUTABLE)]
#[Groups(['report:read'])]
private ?\DateTimeInterface $dateStart = null;
/**
@@ -36,21 +40,26 @@ class Reports
*/
#[ORM\OneToMany(targetEntity: ReportsTodo::class, mappedBy: 'report', orphanRemoval: true)]
#[ORM\Cache(usage: 'READ_ONLY', region: 'report_cache')]
#[Groups(['report:read','reportsAll:read'])]
private Collection $reportsTodos;
#[ORM\Column(options: ["default" => "0"])]
#[Groups(['report:read'])]
private bool $isDelete = false;
private int $numberOfReport = 0;
#[ORM\Column(options: ["default" => "0"])]
#[Groups(['report:read'])]
private bool $isDone = false;
#[ORM\Column]
#[Groups(['report:read','reportsAll:read'])]
private ?int $number = null;
#[ORM\Column(enumType: ReportHighlight::class, options: ["default" => "0"])]
#[Groups(['report:read'])]
private ReportHighlight $highlight = ReportHighlight::STANDARD;
public function __construct()
@@ -150,6 +159,25 @@ class Reports
return $count;
}
#[Groups(['reportsAll:read'])]
public function getStatusTodo(): ReportStatus
{
if ($this->countTodoReports() === 0) {
return ReportStatus::INSERT;
}
$status = ReportStatus::WORKING;
foreach ($this->getReportsTodos() as $reportsTodo) {
if ($reportsTodo->isDeleted()) {
continue;
}
if ($reportsTodo->getName() === ReportTodoEnum::COMPLETE) {
$status = ReportStatus::COMPLETE;
break;
}
}
return $status;
}
public function takeStatusTodo(): ReportStatus
{
if ($this->countTodoReports() === 0) {
+21
View File
@@ -7,17 +7,29 @@ use App\Enum\ReportTodoEnum;
use App\Repository\ReportsTodoRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
#[ORM\Entity(repositoryClass: ReportsTodoRepository::class)]
#[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'report_cache')]
#[ORM\Table(
name: 'reports_todo',
uniqueConstraints: [
new ORM\UniqueConstraint(
name: 'report_todo',
columns: ['report_id', 'name'],
)
]
)]
class ReportsTodo
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Groups(['reportstodo:read'])]
private ?int $id = null;
#[ORM\Column(type: Types::SMALLINT, enumType: ReportTodoEnum::class)]
#[Groups(['reportstodo:read'])]
private ?ReportTodoEnum $name = null;
#[ORM\ManyToOne(inversedBy: 'reportsTodos')]
@@ -25,8 +37,12 @@ class ReportsTodo
private ?Reports $report = null;
#[ORM\Column(options: ["default" => false])]
#[Groups(['reportstodo:read'])]
private ?bool $deleted = false;
#[Groups(['reportstodo:read'])]
private ?string $nameString = null;
public function getId(): ?int
{
return $this->id;
@@ -67,4 +83,9 @@ class ReportsTodo
return $this;
}
public function getNameString(): ?string
{
return $this->name->name;
}
}
+4
View File
@@ -36,6 +36,10 @@ enum ReportTodoEnum: int {
}
public function tryFromName(string $name): ?ReportTodoEnum
{
return self::tryFromNameStatic($name);
}
public static function tryFromNameStatic(string $name): ?ReportTodoEnum
{
$name = strtoupper($name);
foreach (self::cases() as $v) {