test routing

This commit is contained in:
Билай Станислав 2024-07-05 13:49:04 +03:00
parent a482a508e4
commit 2f2310e739
6 changed files with 79 additions and 23 deletions

View File

@ -1,22 +1,32 @@
<?php
namespace Controllers;
use Models\Answer;
use Models\Upvote;
namespace app\controllers;
use app\models\Answer;
use app\models\Upvote;
class Answers {
public static function add_answer($answer,$question_id,$user_id)
public function actionAddAnswer($answer,$question_id,$user_id)
{
return Answer::create(['answer'=>$answer,'question_id'=>$question_id,'user_id'=>$user_id]);
}
public static function upvote_answer($answer_id,$user_id)
public static function actionUpvoteAnswer($answer_id,$user_id)
{
return Upvote::create(['answer_id'=>$answer_id,'user_id'=>$user_id]);
}
public static function update_answer($answer_id,$new_answer)
public function actionUpdateAnswer($answer_id,$new_answer)
{
$answer = Answer::find($answer_id);
$answer->answer = $new_answer;
return $answer->save();
}
public function actionViewAllAnswers(): void
{
foreach (Answer::all() as $answer)
{
echo $answer->answer . "<br>";
}
}
}

View File

@ -1,13 +1,22 @@
<?php
namespace Controllers;
namespace app\controllers;
use Models\Post;
use app\models\Post;
class Posts
{
public static function create_post($post, $user_id)
public function actionCreatePost($post, $user_id)
{
return Post::create(['post'=>$post, 'user_id'=>$user_id]);
}
public function actionViewAllPosts()
{
foreach (Post::all() as $post)
{
echo $post->post . "<br>";
}
}
}

View File

@ -1,25 +1,34 @@
<?php
namespace Controllers;
use Models\Question;
namespace app\controllers;
use app\models\Question;
class Questions{
public static function create_question($question,$user_id)
public function actionCreateQuestion($question,$user_id)
{
return Question::create(['question'=>$question,'user_id'=>$user_id]);
}
public static function get_questions_with_answers()
public function actionGetQuestionsWithAnswers()
{
return Question::with('Answers')->get()->toArray();
}
public static function get_questions_with_users()
public function actionGetQuestionsWithUsers()
{
return Question::with('user')->get()->toArray();
}
public static function get_question_answers_upvotes($question_id)
public function actionGetQuestionAnswersUpvotes($question_id)
{
return Question::find($question_id)->answers()->with('upvotes')->get()->toArray();
}
public function actionViewAllQuestions()
{
foreach (Question::all() as $question)
{
echo $question->question. "<br>";
}
}
}

View File

@ -1,7 +1,11 @@
<?php
namespace Controllers;
use Models\User;
use Models\Question;
namespace app\controllers;
use app\models\Question;
use app\models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\DB;
class Users {
public function actionCreateUser($username, $email, $password)
@ -13,4 +17,22 @@ class Users {
{
return Question::where('user_id', $user_id)->count();
}
public function actionViewAllUsers(): void
{
foreach (User::all() as $user)
{
echo $user->username . "<br>";
}
}
public function actionViewUser(): void
{
echo User::where('id', '=', 13)->get();
// $user = User::where('id', '=', 13)->get();
// echo $user->username . "<br>";
// echo $user->email . "<br>";
// echo $user->created_at . "<br>";
}
}

View File

@ -1 +0,0 @@
<?php

View File

@ -3,10 +3,10 @@ require_once "vendor/autoload.php";
require 'bootstrap.php';
use app\controllers\MainController;
use Controllers\Users;
use Controllers\Questions;
use Controllers\Answers;
use Controllers\Posts;
use app\controllers\Users;
use app\controllers\Questions;
use app\controllers\Answers;
use app\controllers\Posts;
use Phroute\Phroute\RouteCollector;
@ -14,6 +14,13 @@ $router = new RouteCollector();
$router->get('/', [MainController::class, 'actionIndex']);
$router->get('/example', [MainController::class, 'actionExample']);
//$router->get('/createUser', [Users::class, 'actionCreateUser']);
$router->get('/allUsers', [Users::class, 'actionViewAllUsers']);
$router->get('/User', [Users::class, 'actionViewUser'], ['3']);
$router->get('/allQuestions', [Questions::class, 'actionViewAllQuestions']);
$router->get('/allPosts', [Posts::class, 'actionViewAllPosts']);
$router->get('/allAnswers', [Answers::class, 'actionViewAllAnswers']);
$dispatcher = new Phroute\Phroute\Dispatcher($router->getData());