From cbaac886448279ce92fc766d6e3db08e709546f8 Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Sun, 9 Feb 2025 14:31:53 +0900 Subject: [PATCH] feat: Add view to see non-conforming notes --- src/Controller/AnkiController.php | 34 ++++++++++++++++++++------ src/Entity/Note.php | 8 ++++++ src/Service/AnkiService.php | 9 +++++-- templates/anki/nonconforming.html.twig | 22 +++++++++++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 templates/anki/nonconforming.html.twig diff --git a/src/Controller/AnkiController.php b/src/Controller/AnkiController.php index 34923fb..951e3f7 100644 --- a/src/Controller/AnkiController.php +++ b/src/Controller/AnkiController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Entity\Note; use App\Service\AnkiService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; @@ -14,24 +15,43 @@ class AnkiController extends AbstractController private AnkiService $ankiService, ) {} + public static function tmpl(string $path): string + { + return "anki/$path.html.twig"; + } + #[Route('/', name: 'main')] public function index(): Response { - $note = $this->ankiService->getLatestNote(); + $allIds = $this->ankiService->getAllNoteIds(); - $this->ankiService->updateNote($note); + $allNotes = $this->ankiService->getNotes($allIds); - dd($note); - dd($note->toAnki()); + dd($allNotes); + } - return $this->render('anki/index.html.twig', [ - 'controller_name' => $latestId, + #[Route('/nonconforming', name: 'nonconforming')] + public function nonconforming(): Response + { + $allIds = $this->ankiService->getAllNoteIds(); + + $allNotes = $this->ankiService->getNotes($allIds); + + $lacking = array_filter($allNotes, function ($note) { + assert($note instanceof Note); + return empty($note->getTerms()); + }); + + return $this->render(self::tmpl('nonconforming'), [ + 'notes' => $lacking, ]); } #[Route('/note/{nid}/get', name: 'get_note')] public function get_note(int $nid) { - dd($this->ankiService->getNote($nid)); + $note = $this->ankiService->getNote($nid); + //$this->ankiService->updateNote($note); + dd($note); } } diff --git a/src/Entity/Note.php b/src/Entity/Note.php index c85f64f..1d9dc9b 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -31,6 +31,14 @@ class Note { return $this->id; } + public function getTerms(): array + { + return $this->terms; + } + public function getFields(): array + { + return $this->fields; + } public function hasTerm(string $kanji): bool { diff --git a/src/Service/AnkiService.php b/src/Service/AnkiService.php index 19ccdb0..10faa09 100644 --- a/src/Service/AnkiService.php +++ b/src/Service/AnkiService.php @@ -41,7 +41,7 @@ class AnkiService /** Give an array of IDs, the note Infos are returned. if info for a given * doesn't exist, it is assigned to null instead of the default []. */ - private function getNotesInfo(array $noteIds): array + public function getNotesInfo(array $noteIds): array { $noteInfos = $this->request('notesInfo', ['notes' => $noteIds]); @@ -53,11 +53,16 @@ class AnkiService } /** Returns info form note given an ID, returns null if it doesn't exist */ - private function getNoteInfo(int $noteId): ?array + public function getNoteInfo(int $noteId): ?array { return $this->getNotesInfo([$noteId])[0]; } + public function getNotes(array $nids): array + { + return array_map(Note::fromAnki(...), $this->getNotesInfo($nids)); + } + public function getNote(int $nid): ?Note { return Note::fromAnki($this->getNoteInfo($nid)); diff --git a/templates/anki/nonconforming.html.twig b/templates/anki/nonconforming.html.twig new file mode 100644 index 0000000..db2c411 --- /dev/null +++ b/templates/anki/nonconforming.html.twig @@ -0,0 +1,22 @@ +{% extends 'base.html.twig' %} + +{% block title %}Main{% endblock %} + +{% block body %} +
+

hello

+ total {{ notes|length }} + {% for note in notes %} +
+ {{ note.fields.SentKanji }} +
+ view +
+ {% endfor %} +
+{% endblock %}