public function newAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $question = new Questions(); $question->id = uniqid(); $question->tags = $this->request->getPost('tags'); $question->title = $this->request->getPost('title'); $question->content = $this->request->getPost('content'); $question->users_id = $this->request->getPost('users_id'); if ($this->request->hasFiles() == true) { $baseLocation = 'files/'; foreach ($this->request->getUploadedFiles() as $file) { $photos = new Photos(); $unique_filename = $question->id; $photos->size = $file->getSize(); $photos->original_name = $file->getName(); $photos->file_name = $unique_filename; $photos->extension = $file->getExtension(); $location = $baseLocation . $unique_filename . "." . $file->getExtension(); $photos->public_link = $location; try { if (!$photos->save()) { $response->setResponseError($photos->getMessages()); } else { //Move the file into the application $file->moveTo($location); $question->photo = $photos->public_link; } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } } try { if ($question->save() == false) { $response->setResponseError($question->getMessages()); } else { $response->setResponseMessage($question->id); } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function loginFbAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $fbId = $this->request->getPost('fbId'); $username = $this->request->getPost('username'); $email = $this->request->getPost('email'); $avatar = $this->request->getPost('avatar'); $user = Users::findFirstByFbId($fbId); if ($user == true) { $response->setResponseMessage('Login successfully!'); return $response; } else { $user = new Users(); $user->id = uniqid(); $user->fbId = $fbId; $user->avatar = $avatar; $user->username = $username; $user->email = $email; try { if ($user->save() == false) { $response->setResponseError(implode(', ', $user->getMessages())); } else { $response->setResponseMessage('Register successfully!'); } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function detailAction() { $response = new ApiResponse(); if ($this->request->isGet()) { $offers_id = $this->request->get('offers_id'); $offer = Offers::findFirstById($offers_id); if ($offer == false) { $response->setResponseError('No offer found!'); return $response; } else { $response->setResponse($offer, 1); return $response; } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
public function downAction() { $response = new ApiResponse(); $value = -1; if ($this->request->isGet()) { $questions_id = $this->request->get('questions_id'); $answers_id = $this->request->get('answers_id'); $users_id = $this->request->get('users_id'); if ($questions_id != null && $answers_id == null) { $question = Questions::findFirstById($questions_id); if ($question == false) { $response->setResponseError('Question not found!'); return $response; } else { if ($question->users_id != $users_id && !Votes::checkIfVotedQuestion($users_id, $questions_id)) { $question->downvotes += 1; if ($question->save() == false) { $response->setResponseError($question->getMessages()); return $response; } $vote = new Votes(); $vote->id = uniqid(); $vote->questions_id = $questions_id; $vote->users_id = $users_id; $vote->value = $value; if ($vote->save() == false) { $response->setResponseError($vote->getMessages()); return $response; } else { $response->setResponseMessage("Create vote down {$vote->id} for question {$questions_id} successfully!"); } } else { $response->setResponseError('Cant re-vote or vote for yourself!'); return $response; } } } else { if ($questions_id == null && $answers_id != null) { $answer = Answers::findFirstById($answers_id); if ($answer == false) { $response->setResponseError('Answer not found!'); return $response; } else { if ($answer->users_id != $users_id && !Votes::checkIfVotedAnswer($users_id, $answers_id)) { var_dump(Votes::checkIfVotedAnswer($users_id, $answers_id)); $answer->downvotes += 1; if ($answer->save() == false) { $response->setResponseError($answer->getMessages()); return $response; } $vote = new Votes(); $vote->id = uniqid(); $vote->answers_id = $answers_id; $vote->users_id = $users_id; $vote->value = $value; if ($vote->save() == false) { $response->setResponseError($vote->getMessages()); return $response; } else { $response->setResponseMessage("Create vote down {$vote->id} for answer {$answers_id} successfully!"); } } else { $response->setResponseError('Cant re-vote or vote for yourself!'); return $response; } } } } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }