initial rest api for react.js
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
/.env.local.php
|
||||
/.env.*.local
|
||||
/config/secrets/prod/prod.decrypt.private.php
|
||||
config/jwt
|
||||
!config/jwt/.gitkeep
|
||||
/public/bundles/
|
||||
/var/
|
||||
/vendor/
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
"doctrine/doctrine-migrations-bundle": "^3.3",
|
||||
"doctrine/orm": "^3.2",
|
||||
"league/commonmark": "^2.4",
|
||||
"lexik/jwt-authentication-bundle": "^3.2",
|
||||
"nelmio/cors-bundle": "^2.6",
|
||||
"predis/predis": "^2.2",
|
||||
"symfony/console": "6.4.*",
|
||||
"symfony/dotenv": "6.4.*",
|
||||
@@ -20,6 +22,7 @@
|
||||
"symfony/http-client": "^7.1",
|
||||
"symfony/runtime": "6.4.*",
|
||||
"symfony/security-bundle": "6.4.*",
|
||||
"symfony/serializer-pack": "^1.3",
|
||||
"symfony/stimulus-bundle": "^2.18",
|
||||
"symfony/twig-bundle": "6.4.*",
|
||||
"symfony/ux-turbo": "^2.18",
|
||||
|
||||
Generated
+1716
-637
File diff suppressed because it is too large
Load Diff
@@ -12,4 +12,6 @@ return [
|
||||
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
|
||||
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
|
||||
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
|
||||
Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
|
||||
Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
|
||||
];
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
lexik_jwt_authentication:
|
||||
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
|
||||
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
|
||||
pass_phrase: '%env(JWT_PASSPHRASE)%'
|
||||
clock_skew: 0
|
||||
@@ -0,0 +1,8 @@
|
||||
nelmio_cors:
|
||||
defaults:
|
||||
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
|
||||
allow_headers: ['Content-Type', 'Authorization']
|
||||
allow_methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']
|
||||
max_age: 3600
|
||||
paths:
|
||||
'^/api/': ~
|
||||
@@ -13,6 +13,20 @@ security:
|
||||
dev:
|
||||
pattern: ^/(_(profiler|wdt)|css|images|js)/
|
||||
security: false
|
||||
login:
|
||||
pattern: ^/api/auth/login
|
||||
stateless: true
|
||||
json_login:
|
||||
check_path: /api/auth/login
|
||||
username_path: username
|
||||
password_path: password
|
||||
success_handler: lexik_jwt_authentication.handler.authentication_success
|
||||
failure_handler: lexik_jwt_authentication.handler.authentication_failure
|
||||
|
||||
api:
|
||||
pattern: ^/api
|
||||
stateless: true
|
||||
jwt: ~
|
||||
main:
|
||||
lazy: true
|
||||
provider: app_user_provider
|
||||
@@ -24,22 +38,13 @@ security:
|
||||
always_use_default_target_path: true
|
||||
logout:
|
||||
path: app_logout
|
||||
# where to redirect after logout
|
||||
# target: app_any_route
|
||||
|
||||
# activate different ways to authenticate
|
||||
# https://symfony.com/doc/current/security.html#the-firewall
|
||||
|
||||
# https://symfony.com/doc/current/security/impersonating_user.html
|
||||
# switch_user: true
|
||||
|
||||
# Easy way to control access for large sections of your site
|
||||
# Note: Only the *first* access control that matches will be used
|
||||
access_control:
|
||||
- { path: ^/login, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/user/activate, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/admin, roles: ROLE_ADMIN }
|
||||
- { path: ^/, roles: ROLE_USER }
|
||||
- { path: ^/api/auth, roles: PUBLIC_ACCESS }
|
||||
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
|
||||
|
||||
when@test:
|
||||
security:
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace DoctrineMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20260507143656 extends AbstractMigration
|
||||
{
|
||||
public function getDescription(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE UNIQUE INDEX report_todo ON reports_todo (report_id, name)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('DROP INDEX report_todo ON reports_todo');
|
||||
}
|
||||
}
|
||||
@@ -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']]);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user