diff options
author | Jonathan Tschanter <jmtw@tutanota.de> | 2022-01-22 21:24:24 +0100 |
---|---|---|
committer | Jonathan Tschanter <jmtw@tutanota.de> | 2022-01-22 21:24:24 +0100 |
commit | 10c86197f1f63e222dfc9e937911e35ab500a208 (patch) | |
tree | 388ca37920808a66697385c6d69cf6a76e5aaf7b /src | |
parent | 432bba3dee1d9be25b9626ac45115929a6422961 (diff) |
Adds edit cards and templates to include
Diffstat (limited to 'src')
-rw-r--r-- | src/Controller/Card.php | 61 | ||||
-rw-r--r-- | src/DB.php | 2 | ||||
-rw-r--r-- | src/Model/Card.php | 33 | ||||
-rw-r--r-- | src/Model/Vote.php | 8 | ||||
-rw-r--r-- | src/Model/VoteCard.php | 5 | ||||
-rw-r--r-- | src/Router.php | 6 |
6 files changed, 91 insertions, 24 deletions
diff --git a/src/Controller/Card.php b/src/Controller/Card.php index e7fa793..3d8d8e0 100644 --- a/src/Controller/Card.php +++ b/src/Controller/Card.php @@ -28,14 +28,13 @@ class Card public static function getAll(): array { - // TODO: is that correct? yes - how to call this getAll? return DB::$entityManager->getRepository(CardModel::class)->findAll(); } public static function listCards(): string { // get all cards - $cards = DB::$entityManager->getRepository(CardModel::class)->findAll(); + $cards = self::getAll(); return Template::render('card/card_list.twig', [ 'cards' => $cards @@ -47,25 +46,40 @@ class Card // TODO: how to access all available meta? return Template::render('card/card_add.twig', [ 'fields' => [ - 'name' => 'meta[name]', - 'converted mana cost' => 'meta[cmc]', - 'is uno reverse' => 'meta[is_uno_reverse]', + 'name' => 'name', + 'converted mana cost' => 'cmc', + 'is uno reverse' => 'is_uno_reverse', ], ]); } - public static function displayCard(Request $request): string + public static function viewCard(Request $request): string { $route = $request->attributes->get('route'); $cardId = $route['id']; $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); - return Template::render('card/card_display.twig', [ + return Template::render('card/card_view.twig', [ 'card' => $card ]); } + public static function viewArtwork(Request $request): string + { + $route = $request->attributes->get('route'); + $cardId = $route['id']; + $artworkId = $route['artworkId']; + + $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + $artwork = DB::$entityManager->getRepository(Artwork::class)->find($artworkId); + + return Template::render('artwork/artwork_view.twig', [ + 'card' => $card, + 'artwork' => $artwork + ]); + } + public static function editCard(Request $request) { $route = $request->attributes->get('route'); @@ -127,10 +141,41 @@ class Card { $route = $request->attributes->get('route'); $cardId = $route['id']; + $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); - // TODO: like add but edit + foreach ($request->get('meta') as $key => $value) { + $meta = $card->getMeta($key); + if ($meta == null) { + $meta = new CardMeta($key, $value); + $card->addMeta($meta); + } else { + $meta->value = strval($value); + } + + DB::save($meta); + } + + DB::save($card); $response = new RedirectResponse('/card/' . $cardId); return $response; } + + public static function vote(Request $request) + { + var_dump('VOTE'); + $route = $request->attributes->get('route'); + $cardId = $route['id']; + $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + + $vote = new VoteCard(); + $vote->value = intval($request->get('value')); + $card->addVote($vote); + DB::save($vote); + DB::save($card); + + // TODO: redirect to last visited + $response = new RedirectResponse('/card'); + return $response; + } } @@ -42,7 +42,7 @@ class DB if (! empty($criteria) && ! is_null($exists)) { foreach (get_object_vars($entity) as $key => $value) { - $exists->$key = $value; + $exists->$key = $value; } } else { $exists = $entity; diff --git a/src/Model/Card.php b/src/Model/Card.php index 59d2639..934fc2d 100644 --- a/src/Model/Card.php +++ b/src/Model/Card.php @@ -99,28 +99,35 @@ class Card * * @return string */ - public function getMeta(string $key): ?string + public function getMetaValue(string $key): ?string + { + return $this->getMeta($key)->value ?? null; + } + + /** + * @param string $key + * + * @return CardMeta + */ + public function getMeta(string $key): ?CardMeta { // if meta is already hydrated if ($this->meta->isInitialized()) { $meta = $this->meta->unwrap() #->findFirst(fn (CardMeta $item) => $item->key === $key); ->filter(fn ($item) => $item->key === $key)->first(); - return $meta->value ?? null; + return $meta ?? null; } // get directly from db otherwise $result = DB::$entityManager - ->createQuery( - 'SELECT cm.value - FROM Elements\Model\CardMeta cm - WHERE cm.key = :key AND cm.card = :card' - ) - ->setParameter('key', $key) - ->setParameter('card', $this) - ->getOneOrNullResult(); + ->getRepository(CardMeta::class) + ->findOneBy([ + 'key' => $key, + 'card' => $this + ]); - return $result['value'] ?? null; + return $result ?? null; } /** @@ -128,7 +135,7 @@ class Card */ public function getAllMeta() { - // TODO: do we need this? + // TODO: change! // if meta is already hydrated if ($this->meta->isInitialized()) { return $this->meta; @@ -152,7 +159,7 @@ class Card */ public function getAllArtworks() { - // TODO: do we need this? + // TODO: change! // if artworks are already hydrated if ($this->artworks->isInitialized()) { return $this->artworks; diff --git a/src/Model/Vote.php b/src/Model/Vote.php index c5415aa..273a7d8 100644 --- a/src/Model/Vote.php +++ b/src/Model/Vote.php @@ -26,5 +26,13 @@ class Vote * @Column(type="integer") */ public int $value; + + /** + * Vote constructor. + */ + public function __contruct(int $value) + { + $this->value = $value; + } } diff --git a/src/Model/VoteCard.php b/src/Model/VoteCard.php index e9b3e60..292789f 100644 --- a/src/Model/VoteCard.php +++ b/src/Model/VoteCard.php @@ -22,5 +22,10 @@ class VoteCard extends Vote * @ManyToOne(targetEntity="Card", inversedBy="votes", cascade={"persist"}) */ public Card $card; + + public function __contruct(int $value) + { + $this->card = $card; + } } diff --git a/src/Router.php b/src/Router.php index aad6122..46e3f4f 100644 --- a/src/Router.php +++ b/src/Router.php @@ -31,12 +31,14 @@ class Router $this->addRoute('GET', '/dashboard', [Home::class, 'dashboard']); // card - $this->addRoute('GET', '/card/list', [Card::class, 'listCards']); + $this->addRoute('GET', '/card', [Card::class, 'listCards']); $this->addRoute('GET', '/card/add', [Card::class, 'addCard']); - $this->addRoute('GET', '/card/{id}', [Card::class, 'displayCard']); + $this->addRoute('GET', '/card/{id}', [Card::class, 'viewCard']); $this->addRoute('GET', '/card/{id}/edit', [Card::class, 'editCard']); + $this->addRoute('GET', '/card/{id}/artwork/{artworkId}', [Card::class, 'viewArtwork']); $this->addRoute('POST', '/card/add', [Card::class, 'add']); $this->addRoute('POST', '/card/{id}/edit', [Card::class, 'edit']); + $this->addRoute('POST', '/card/{id}/vote', [Card::class, 'vote']); $context = new RequestContext(); $context->fromRequest($request); |