/** * @operationName("Edit Livenews (For Data)") * @operationDescription("Edit Livenews (For Data)") */ public function editAction() { $this->view->changeRender('admin/data/create'); $news = Models\NewsManager::findFirst($this->dispatcher->getParam('id')); if (!$news) { throw new Exception\ResourceNotFoundException('ERR_LIVENEWS_NEWS_NOT_FOUND'); } $form = new Forms\NewsForm(); $form->setModel($news); $form->addForm('text', 'Eva\\EvaLivenews\\Forms\\TextForm'); $this->view->setVar('form', $form); $this->view->setVar('item', $news); if (!$this->request->isPost()) { return false; } $data = $this->request->getPost(); if ($this->request->isAjax()) { if (!$form->isFullValid($data)) { return $this->showInvalidMessagesAsJson($form); } try { $form->save('updateNews'); } catch (\Exception $e) { return $this->showExceptionAsJson($e, $form->getModel()->getMessages()); } return $this->showResponseAsJson($form->getModel()->dump(Models\NewsManager::$defaultDump)); } else { if (!$form->isFullValid($data)) { return $this->showInvalidMessages($form); } try { $form->save('updateNews'); } catch (\Exception $e) { return $this->showException($e, $form->getModel()->getMessages()); } $this->flashSession->success('SUCCESS_NEWS_UPDATED'); return $this->redirectHandler('/admin/livenews/news/edit/' . $news->id); } }
/** * * @SWG\Api( * path="/admin/livenews", * description="Livenews related api", * produces="['application/json']", * @SWG\Operations( * @SWG\Operation( * method="POST", * summary="Create new livenews", * notes="Returns a livenews based on ID", * @SWG\Parameters( * @SWG\Parameter( * name="livenews json", * description="Livenews 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\NewsForm(); $livenews = new Models\NewsManager(); $form->setModel($livenews); $form->addForm('text', 'Eva\\EvaLivenews\\Forms\\TextForm'); if (!$form->isFullValid($data)) { return $this->showInvalidMessagesAsJson($form); } try { $form->save('createNews'); $data = $livenews->dump(Models\NewsManager::$defaultDump); return $this->response->setJsonContent($data); } catch (\Exception $e) { return $this->showExceptionAsJson($e, $form->getModel()->getMessages()); } }