109 lines
3.7 KiB
PHP
109 lines
3.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Controller;
|
||
|
|
|
||
|
|
use App\Entity\Goal;
|
||
|
|
use App\Entity\User;
|
||
|
|
use App\Repository\GoalRepository;
|
||
|
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
use Symfony\Component\Routing\Attribute\Route;
|
||
|
|
|
||
|
|
#[Route('/api/goals')]
|
||
|
|
class GoalController extends AbstractController
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly EntityManagerInterface $em,
|
||
|
|
private readonly GoalRepository $goals,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
#[Route('', methods: ['GET'])]
|
||
|
|
public function list(): JsonResponse
|
||
|
|
{
|
||
|
|
$user = $this->getUser();
|
||
|
|
if (!$user instanceof User) {
|
||
|
|
return new JsonResponse(['error' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED);
|
||
|
|
}
|
||
|
|
$goals = array_map(fn(Goal $g) => $g->toArray(), $this->goals->findByUser($user->getId()));
|
||
|
|
return new JsonResponse($goals);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('', methods: ['POST'])]
|
||
|
|
public function create(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$user = $this->getUser();
|
||
|
|
if (!$user instanceof User) {
|
||
|
|
return new JsonResponse(['error' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = json_decode($request->getContent(), true) ?? [];
|
||
|
|
$name = trim($data['name'] ?? '');
|
||
|
|
if (!$name) {
|
||
|
|
return new JsonResponse(['error' => 'Name fehlt'], Response::HTTP_BAD_REQUEST);
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
$start = new \DateTime($data['start'] ?? 'now');
|
||
|
|
} catch (\Exception) {
|
||
|
|
$start = new \DateTime();
|
||
|
|
}
|
||
|
|
|
||
|
|
$goal = new Goal();
|
||
|
|
$goal->setUserId($user->getId())
|
||
|
|
->setName($name)
|
||
|
|
->setUnit(trim($data['unit'] ?? 'Stück') ?: 'Stück')
|
||
|
|
->setDaily((float)($data['daily'] ?? 1))
|
||
|
|
->setDays((int)($data['days'] ?? 30))
|
||
|
|
->setStart($start)
|
||
|
|
->setSets(is_array($data['sets'] ?? null) ? $data['sets'] : []);
|
||
|
|
|
||
|
|
$this->em->persist($goal);
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return new JsonResponse($goal->toArray());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/{id}', methods: ['PATCH'])]
|
||
|
|
public function update(int $id, Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$user = $this->getUser();
|
||
|
|
if (!$user instanceof User) {
|
||
|
|
return new JsonResponse(['error' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED);
|
||
|
|
}
|
||
|
|
|
||
|
|
$goal = $this->goals->findOneBy(['id' => $id, 'userId' => $user->getId()]);
|
||
|
|
if (!$goal) {
|
||
|
|
return new JsonResponse(['error' => 'Nicht gefunden'], Response::HTTP_NOT_FOUND);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = json_decode($request->getContent(), true) ?? [];
|
||
|
|
if (isset($data['name'])) $goal->setName((string)$data['name']);
|
||
|
|
if (isset($data['unit'])) $goal->setUnit((string)$data['unit']);
|
||
|
|
if (isset($data['daily'])) $goal->setDaily((float)$data['daily']);
|
||
|
|
if (isset($data['days'])) $goal->setDays((int)$data['days']);
|
||
|
|
if (isset($data['sets'])) $goal->setSets((array)$data['sets']);
|
||
|
|
|
||
|
|
$this->em->flush();
|
||
|
|
return new JsonResponse(['ok' => true]);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/{id}', methods: ['DELETE'])]
|
||
|
|
public function delete(int $id): JsonResponse
|
||
|
|
{
|
||
|
|
$user = $this->getUser();
|
||
|
|
if (!$user instanceof User) {
|
||
|
|
return new JsonResponse(['error' => 'Unauthorized'], Response::HTTP_UNAUTHORIZED);
|
||
|
|
}
|
||
|
|
|
||
|
|
$goal = $this->goals->findOneBy(['id' => $id, 'userId' => $user->getId()]);
|
||
|
|
if ($goal) {
|
||
|
|
$this->em->remove($goal);
|
||
|
|
$this->em->flush();
|
||
|
|
}
|
||
|
|
return new JsonResponse(['ok' => true]);
|
||
|
|
}
|
||
|
|
}
|