Browse Source

Create Journal entity, add more components

imwald
Nuša Pukšič 1 year ago
parent
commit
76f70de1eb
  1. 24
      src/Controller/DefaultController.php
  2. 80
      src/Entity/Journal.php
  3. 43
      src/Repository/JournalRepository.php
  4. 2
      src/Service/NostrClient.php
  5. 2
      src/Twig/Components/Molecules/Card.php
  6. 24
      src/Twig/Components/Molecules/UserFromNpub.php
  7. 1
      src/Twig/Components/Organisms/CardList.php
  8. 2
      templates/components/Molecules/Card.html.twig
  9. 5
      templates/components/Molecules/UserFromNpub.html.twig
  10. 4
      templates/components/Organisms/CardList.html.twig
  11. 4
      templates/components/UserMenu.html.twig
  12. 1
      templates/home.html.twig

24
src/Controller/DefaultController.php

@ -4,19 +4,37 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use App\Entity\Article;
use App\Service\NostrClient;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController class DefaultController extends AbstractController
{ {
public function __construct(private readonly EntityManagerInterface $entityManager,
private readonly NostrClient $nostrClient)
{
}
/** /**
* @Route("/default") * @throws \Exception
*/ */
#[\Symfony\Component\Routing\Attribute\Route('/', name: 'default')] #[Route('/', name: 'default')]
public function index(): Response public function index(): Response
{ {
return $this->render('home.html.twig'); $list = $this->entityManager->getRepository(Article::class)->findAll();
$current = array_slice($list, 0,10);
$npubs = array_map(function($obj) {
return $obj->getPubkey();
}, $list);
$this->nostrClient->getMetadata(array_unique($npubs));
return $this->render('home.html.twig', [
'list' => $current
]);
} }
} }

80
src/Entity/Journal.php

@ -0,0 +1,80 @@
<?php
namespace App\Entity;
use App\Repository\JournalRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: JournalRepository::class)]
class Journal
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $npub = null;
#[ORM\Column]
private array $mainCategories = [];
#[ORM\Column(nullable: true)]
private ?array $lists = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $editor = null;
public function getId(): ?int
{
return $this->id;
}
public function getNpub(): ?string
{
return $this->npub;
}
public function setNpub(string $npub): static
{
$this->npub = $npub;
return $this;
}
public function getMainCategories(): array
{
return $this->mainCategories;
}
public function setMainCategories(array $mainCategories): static
{
$this->mainCategories = $mainCategories;
return $this;
}
public function getLists(): ?array
{
return $this->lists;
}
public function setLists(?array $lists): static
{
$this->lists = $lists;
return $this;
}
public function getEditor(): ?string
{
return $this->editor;
}
public function setEditor(?string $editor): static
{
$this->editor = $editor;
return $this;
}
}

43
src/Repository/JournalRepository.php

@ -0,0 +1,43 @@
<?php
namespace App\Repository;
use App\Entity\Journal;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Journal>
*/
class JournalRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Journal::class);
}
// /**
// * @return Journal[] Returns an array of Journal objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('j')
// ->andWhere('j.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('j.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Journal
// {
// return $this->createQueryBuilder('j')
// ->andWhere('j.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

2
src/Service/NostrClient.php

@ -39,7 +39,7 @@ class NostrClient
$filter = new Filter(); $filter = new Filter();
$filter->setKinds([KindsEnum::LONGFORM]); $filter->setKinds([KindsEnum::LONGFORM]);
// TODO make filters configurable // TODO make filters configurable
$filter->setSince(strtotime('-1 day')); // $filter->setSince(strtotime('-1 week')); //
$requestMessage = new RequestMessage($subscriptionId, [$filter]); $requestMessage = new RequestMessage($subscriptionId, [$filter]);
// TODO make relays configurable // TODO make relays configurable
$relays = new RelaySet(); $relays = new RelaySet();

2
src/Twig/Components/Molecules/Card.php

@ -8,4 +8,6 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
final class Card final class Card
{ {
public string $tag = 'div'; public string $tag = 'div';
public string $category = '';
public object $article;
} }

24
src/Twig/Components/Molecules/UserFromNpub.php

@ -0,0 +1,24 @@
<?php
namespace App\Twig\Components\Molecules;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent]
final class UserFromNpub
{
public string $npub;
public ?User $user = null;
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function mount(string $npub): void
{
$this->npub = $npub;
$this->user = $this->entityManager->getRepository(User::class)->findOneBy(['npub' => $npub]);
}
}

1
src/Twig/Components/Organisms/CardList.php

@ -7,4 +7,5 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent] #[AsTwigComponent]
final class CardList final class CardList
{ {
public array $list;
} }

2
templates/components/Molecules/Card.html.twig

@ -5,6 +5,6 @@
<h1 class="card-title">{{ article.title }}</h1> <h1 class="card-title">{{ article.title }}</h1>
</div> </div>
<div class="card-footer"> <div class="card-footer">
<small>{{ article.author }}</small> <small><twig:Molecules:UserFromNpub npub="{{ article.pubkey }}" /></small>
</div> </div>
</{{ tag }}> </{{ tag }}>

5
templates/components/Molecules/UserFromNpub.html.twig

@ -0,0 +1,5 @@
{% if user %}
<span>{{ user.displayName }}</span>
{% else %}
<span>{{ npub }}</span>
{% endif %}

4
templates/components/Organisms/CardList.html.twig

@ -1,3 +1,5 @@
<div {{ attributes }}> <div {{ attributes }}>
<!-- component html --> {% for item in list %}
<twig:Molecules:Card :article="item" ></twig:Molecules:Card>
{% endfor %}
</div> </div>

4
templates/components/UserMenu.html.twig

@ -2,8 +2,8 @@
{% if app.user %} {% if app.user %}
<p>Hello, {{ app.user.displayName }}</p> <p>Hello, {{ app.user.displayName }}</p>
<a class="btn btn-primary" href="/logout" data-action="live#$render">Log out</a> <twig:Atoms:Button tag="a" href="/logout" data-action="live#$render">Log out</twig:Atoms:Button>
{% else %} {% else %}
<button class="btn btn-primary" {{ stimulus_action('login', 'loginAct') }}>Log in</button> <twig:Atoms:Button {{ ...stimulus_action('login', 'loginAct') }}>Log in</twig:Atoms:Button>
{% endif %} {% endif %}
</div> </div>

1
templates/home.html.twig

@ -6,6 +6,7 @@
{% block body %} {% block body %}
{# content #} {# content #}
<twig:Organisms:CardList :list="list" />
{% endblock %} {% endblock %}
{% block aside %} {% block aside %}

Loading…
Cancel
Save