query->get('status', ''); if ($filter !== '') { $status = MahlzeitStatus::tryFrom($filter); if ($status === null) { throw new BadRequestHttpException('status muss "geplant" oder "gegessen" sein.'); } } $items = $this->list->execute(new MahlzeitListRequest(status: $status))->items; return new JsonResponse([ 'mahlzeiten' => \array_map(self::serialize(...), $items), ]); } public function checkoff(int $id): JsonResponse { return new JsonResponse(self::serialize($this->checkOff->execute($id))); } public function uncheck(int $id): JsonResponse { return new JsonResponse(self::serialize($this->uncheck->execute($id))); } public function create(Request $request): JsonResponse { $payload = json_decode((string) $request->getContent(), true); if (!\is_array($payload)) { throw new BadRequestHttpException('JSON-Body erwartet, z. B. {"name": "Bockbrot"}'); } $name = \trim((string) ($payload['name'] ?? '')); if ($name === '') { throw new BadRequestHttpException('name ist ein Pflichtfeld.'); } $notiz = \trim((string) ($payload['notiz'] ?? '')); $wochentage = Wochentag::ausWerten((array) ($payload['wochentage'] ?? [])); return new JsonResponse(self::serialize($this->create->execute(new MahlzeitCreateRequest( name: $name, notiz: $notiz, wochentage: $wochentage, ))->mahlzeit), 201); } /** Serialisiert ein Read-Modell in das API-Format. */ public static function serialize(object $m): array { return [ 'id' => $m->id, 'name' => $m->name, 'notiz' => $m->notiz, 'wochentage' => \array_map(static fn (Wochentag $t): string => $t->value, $m->wochentage), 'status' => $m->status->value, 'sort_order' => $m->sortOrder, 'gegessen_am' => $m->gegessenAm?->format(\DateTimeInterface::RFC3339), ]; } }