/** * @operationName("Edit Post") * @operationDescription("Edit Post") */ public function editAction() { $this->view->changeRender('admin/post/create'); $post = Models\Post::findFirst($this->dispatcher->getParam('id')); if (!$post) { throw new Exception\ResourceNotFoundException('ERR_BLOG_POST_NOT_FOUND'); } $form = new Forms\PostForm(); $form->setModel($post); $form->addForm('text', 'Eva\\EvaBlog\\Forms\\TextForm'); $this->view->setVar('form', $form); $this->view->setVar('item', $post); if (!$this->request->isPost()) { return false; } $data = $this->request->getPost(); if (!$form->isFullValid($data)) { return $this->showInvalidMessages($form); } try { $form->save('updatePost'); } catch (\Exception $e) { return $this->showException($e, $form->getModel()->getMessages()); } $this->flashSession->success('SUCCESS_POST_UPDATED'); return $this->redirectHandler('/admin/post/edit/' . $post->id); }
/** * * @SWG\Api( * path="/admin/posts", * description="Post related api", * produces="['application/json']", * @SWG\Operations( * @SWG\Operation( * method="POST", * summary="Create new post", * notes="Returns a post based on ID", * @SWG\Parameters( * @SWG\Parameter( * name="post json", * description="Post info", * paramType="body", * required=true, * type="string" * ) * ) * ) * ) * ) * @operationName("创建文章") * @operationDescription("创建文章") */ public function postAction() { $data = $this->request->getRawBody(); if (!$data) { throw new Exception\InvalidArgumentException('No data input'); } if (!($data = json_decode($data, true))) { throw new Exception\InvalidArgumentException('Data not able to decode as JSON'); } $form = new Forms\PostForm(); $post = new Models\Post(); $form->setModel($post); $form->addForm('text', 'Eva\\EvaBlog\\Forms\\TextForm'); if (!$form->isFullValid($data)) { return $this->showInvalidMessagesAsJson($form); } try { $form->save('createPost'); $data = $post->dump(Models\Post::$defaultDump); return $this->response->setJsonContent($data); } catch (\Exception $e) { return $this->showExceptionAsJson($e, $form->getModel()->getMessages()); } }