diff options
author | Jonathan Tschanter <jmtw@tutanota.de> | 2022-01-21 00:14:34 +0100 |
---|---|---|
committer | Jonathan Tschanter <jmtw@tutanota.de> | 2022-01-21 00:14:34 +0100 |
commit | 2bde1b0660585e16116fbd12c86b53e886b6f3be (patch) | |
tree | 8c23fbad83e8b947c60a50b7af6be2c8b0d4867a /src | |
parent | c2b0ebcdbb23a38a31b4d8c9e39f2a2be4cc862b (diff) |
Add first layout with simplecss
Diffstat (limited to 'src')
-rw-r--r-- | src/Controller/Card.php | 101 | ||||
-rw-r--r-- | src/Controller/Home.php | 11 | ||||
-rw-r--r-- | src/Model/Card.php | 61 | ||||
-rw-r--r-- | src/Router.php | 13 |
4 files changed, 154 insertions, 32 deletions
diff --git a/src/Controller/Card.php b/src/Controller/Card.php index d4ee393..e7fa793 100644 --- a/src/Controller/Card.php +++ b/src/Controller/Card.php @@ -8,6 +8,7 @@ use Elements\Model\Card as CardModel; use Elements\Model\CardMeta; use Elements\Model\VoteArtwork; use Elements\Model\VoteCard; +use Elements\Template; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -15,6 +16,69 @@ use Symfony\Component\HttpFoundation\Response; class Card { + public static function get(Request $request) + { + $route = $request->attributes->get('route'); + $cardId = $route['id']; + + $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + + return $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(); + + return Template::render('card/card_list.twig', [ + 'cards' => $cards + ]); + } + + public static function addCard(): string + { + // 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]', + ], + ]); + } + + public static function displayCard(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', [ + 'card' => $card + ]); + } + + public static function editCard(Request $request) + { + $route = $request->attributes->get('route'); + $cardId = $route['id']; + + $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + $metas = DB::$entityManager->getRepository(CardMeta::class)->findAll(); + + return Template::render('card/card_edit.twig', [ + 'card' => $card + ]); + } + public static function add(Request $request) { $card = new CardModel(); @@ -28,19 +92,19 @@ class Card /**@var UploadedFile[] $files*/ if ($files = $request->files->get('images')) { foreach ($files as $file) { - $image = Artwork::fromUploadedFile($file); - - /*TEST*/ - for ($i = 0; $i <= rand(0, 40); $i++) { - $vote = new VoteArtwork(); - $vote->value = rand(-1, 1) >= 0 ? 1 : -1; - $image->addVote($vote); - DB::save($vote); - } - /*TEST*/ - - $card->addArtwork($image); - DB::save($image); + $image = Artwork::fromUploadedFile($file); + + /*TEST*/ + for ($i = 0; $i <= rand(0, 40); $i++) { + $vote = new VoteArtwork(); + $vote->value = rand(-1, 1) >= 0 ? 1 : -1; + $image->addVote($vote); + DB::save($vote); + } + /*TEST*/ + + $card->addArtwork($image); + DB::save($image); } } @@ -55,19 +119,18 @@ class Card DB::save($card); - $response = new RedirectResponse('/'); - + $response = new RedirectResponse('/card/' . $card->id); return $response; } - public static function get(Request $request) + public static function edit(Request $request) { $route = $request->attributes->get('route'); $cardId = $route['id']; - $card = DB::$entityManager->getRepository(CardModel::class)->find($cardId); + // TODO: like add but edit - return $card->getMeta('name'); + $response = new RedirectResponse('/card/' . $cardId); + return $response; } } - diff --git a/src/Controller/Home.php b/src/Controller/Home.php index 1c1500c..7eda5ee 100644 --- a/src/Controller/Home.php +++ b/src/Controller/Home.php @@ -21,11 +21,16 @@ class Home return Template::render('home.twig', [ 'fields' => [ - 'name' => 'meta[name]', - 'converted mana cost' => 'meta[cmc]', - 'is uno reverse' => 'meta[is_uno_reverse]', + 'name' => 'meta[name]', + 'converted mana cost' => 'meta[cmc]', + 'is uno reverse' => 'meta[is_uno_reverse]', ], ]); } + + public static function dashboard() + { + return Template::render('dashboard.twig', []); + } } diff --git a/src/Model/Card.php b/src/Model/Card.php index abaa06e..59d2639 100644 --- a/src/Model/Card.php +++ b/src/Model/Card.php @@ -104,24 +104,71 @@ class Card // 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(); - + #->findFirst(fn (CardMeta $item) => $item->key === $key); + ->filter(fn ($item) => $item->key === $key)->first(); return $meta->value ?? 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' + 'SELECT cm.value + FROM Elements\Model\CardMeta cm + WHERE cm.key = :key AND cm.card = :card' ) ->setParameter('key', $key) ->setParameter('card', $this) ->getOneOrNullResult(); - return $result['value'] ?? null; + return $result['value'] ?? null; + } + + /** + * @return CardMeta[] + */ + public function getAllMeta() + { + // TODO: do we need this? + // if meta is already hydrated + if ($this->meta->isInitialized()) { + return $this->meta; + } + + // get directly from db otherwise + $result = DB::$entityManager + ->createQuery( + 'SELECT cm + FROM Elements\Model\CardMeta cm + WHERE cm.card = :card' + ) + ->setParameter('card', $this) + ->getArrayResult(); + + return $result; + } + + /** + * @return Artwork[] + */ + public function getAllArtworks() + { + // TODO: do we need this? + // if artworks are already hydrated + if ($this->artworks->isInitialized()) { + return $this->artworks; + } + + // get directly from db otherwise + $result = DB::$entityManager + ->createQuery( + 'SELECT aw + FROM Elements\Model\Artwork aw + WHERE aw.card = :card' + ) + ->setParameter('card', $this) + ->getArrayResult(); + + return $result; } /** diff --git a/src/Router.php b/src/Router.php index 5635d5c..aad6122 100644 --- a/src/Router.php +++ b/src/Router.php @@ -27,9 +27,16 @@ class Router $response = new Response(); $this->routes = new RouteCollection(); - $this->addRoute('GET', '/', [Home::class, 'index']); - $this->addRoute('GET', '/card/{id}', [Card::class, 'get']); - $this->addRoute('POST', '/card/add', [Card::class, 'add']); + $this->addRoute('GET', '/', [Home::class, 'index']); + $this->addRoute('GET', '/dashboard', [Home::class, 'dashboard']); + + // card + $this->addRoute('GET', '/card/list', [Card::class, 'listCards']); + $this->addRoute('GET', '/card/add', [Card::class, 'addCard']); + $this->addRoute('GET', '/card/{id}', [Card::class, 'displayCard']); + $this->addRoute('GET', '/card/{id}/edit', [Card::class, 'editCard']); + $this->addRoute('POST', '/card/add', [Card::class, 'add']); + $this->addRoute('POST', '/card/{id}/edit', [Card::class, 'edit']); $context = new RequestContext(); $context->fromRequest($request); |