public function getById($id) : Fiche { $data = $this->storage->query(function ($pdo, $operations) use($id) { $dbClass = $operations . '\\FetchData'; return $dbClass::getRow($pdo, ['*'], 'fiche', ['id' => $id]); }); $id = new UniqueId($data['id']); $groupRepository = new Group($this->storage); $group = $groupRepository->getById($data['group_id']); return FicheFactory::create($id, $group, $data['word'], $data['explain_word']); }
private function save(Fiche $fiche = null) { $ficheRepository = new FicheRepository($this->storage); $groupRepository = new GroupRepository($this->storage); $word = $this->request->get('word'); $explain = $this->request->get('explain'); $group = $groupRepository->getById($this->request->get('group')); try { if (empty($fiche)) { $fiche = FicheFactory::create(new UniqueId(), $group, $word, $explain); $ficheRepository->insert($fiche); } } catch (DataNotValid $e) { return ['messages' => ['field' => $e->getFieldName(), 'message' => $e->getMessage()], 'data' => ['word' => $word, 'explain' => $explain]]; } return $this->app->redirect('/groups/show/' . $group->getId()); }
private function save(Group $group = null) { $groupRepository = new GroupRepository($this->storage); try { if (empty($group)) { $groupName = new GroupName($this->request->get('name')); $group = new Group(new UniqueId(), $this->getCurrentUser(), $groupName, new Fiches($this->storage)); $groupRepository->insert($group); $userGroupsRepository = new UserGroups($this->storage); $userGroupsRepository->insert($group, $this->currentUser); } else { $group->setName(new GroupName($this->request->get('name'))); $groupRepository->update($group); } } catch (DataNotValid $e) { return ['messages' => ['field' => $e->getFieldName(), 'message' => $e->getMessage()], 'data' => ['name' => $this->request->get('name')]]; } return $this->app->redirect('/groups'); }