add modules and upload file

This commit is contained in:
2024-09-06 16:53:20 +03:00
parent 4e031c7c8b
commit da77807b81
38 changed files with 180 additions and 782 deletions

View File

@ -1,32 +0,0 @@
<?php
namespace app\controllers;
use app\models\Answer;
use app\models\Upvote;
class AnswerController {
public function actionAddAnswer($answer,$question_id,$user_id)
{
return Answer::create(['answer'=>$answer,'question_id'=>$question_id,'user_id'=>$user_id]);
}
public static function actionUpvoteAnswer($answer_id,$user_id)
{
return Upvote::create(['answer_id'=>$answer_id,'user_id'=>$user_id]);
}
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,106 +0,0 @@
<?php
namespace app\controllers;
use app\helpers\Debug;
use app\models\forms\CreateMenuForm;
use app\services\MenuService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
use kernel\models\Menu;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class MenuController extends Controller
{
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionCreate(): void
{
$this->cgView->render("menu/form.php");
}
#[NoReturn] public function actionAdd(): void
{
$menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST);
if ($menuForm->validate()){
$menuItem = $menuService->create($menuForm);
if ($menuItem){
$this->redirect("/admin/menu/" . $menuItem->id);
}
}
$this->redirect("/admin/menu/create");
}
/**
* @throws Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("menu/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("menu/view.php", ['menu' => $menuItem]);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError|Exception
*/
public function actionUpdate($id): void
{
$model = Menu::find($id);
if (!$model){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("menu/form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
$menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST);
if ($menuForm->validate()){
$menuItem = $menuService->update($menuForm, $menuItem);
if ($menuItem){
$this->redirect("/admin/menu/" . $menuItem->id);
}
}
$this->redirect("/admin/menu/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
Menu::find($id)->delete();
$this->redirect("/admin/menu/");
}
}

View File

@ -1,106 +0,0 @@
<?php
namespace app\controllers;
use app\helpers\Debug;
use app\models\forms\CreatePostForm;
use app\models\Post;
use app\models\User;
use app\services\PostService;
use app\services\UserService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
class PostController extends Controller
{
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionCreate(): void
{
$this->cgView->render("post/form.php");
}
#[NoReturn] public function actionAdd(): void
{
$postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST);
if ($postForm->validate()) {
$post = $postService->create($postForm);
if ($post) {
$this->redirect("/admin/post/" . $post->id);
}
}
$this->redirect("/admin/post/create");
}
/**
* @throws Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("post/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$content = Post::find($id);
if (!$content){
throw new Exception(message: "The post not found");
}
$this->cgView->render("post/view.php", ['content' => $content]);
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = Post::find($id);
if (!$model){
throw new Exception(message: "The post not found");
}
$this->cgView->render("post/form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$post = Post::find($id);
if (!$post){
throw new Exception(message: "The post not found");
}
$postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST);
if((new UserService)->check($_REQUEST['user_id'])) {
if ($postForm->validate()) {
$post = $postService->update($postForm, $post);
if ($post) {
$this->redirect("/admin/post/" . $post->id);
}
}
}
$this->redirect("/admin/post/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
$post = Post::find($id)->first();
$post->delete();
$this->redirect("/admin/post/");
}
}

View File

@ -1,36 +0,0 @@
<?php
namespace app\controllers;
use app\models\Question;
use kernel\Controller;
class QuestionController extends Controller{
public function actionCreate()
{
echo $this->twig->render('question_create.html.twig');
}
public function actionGetQuestionsWithAnswers(): array
{
return Question::with('AnswerController')->get()->toArray();
}
public function actionGetQuestionsWithUsers(): array
{
return Question::with('user')->get()->toArray();
}
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,110 +0,0 @@
<?php
namespace app\controllers;
use app\models\forms\CreateUserForm;
use app\models\Question;
use app\models\User;
use app\services\UserService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class UserController extends Controller{
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionCreate(): void
{
$this->cgView->render("user/form.php");
}
#[NoReturn] public function actionAdd(): void
{
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
$user = $userService->create($userForm);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/create");
}
public function actionQuestionCount($user_id)
{
return Question::where('user_id', $user_id)->count();
}
/**
* @throws \Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("user/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$user = User::find($id);
if (!$user){
throw new Exception(message: "The user not found");
}
$this->cgView->render("user/view.php", ['user' => $user]);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function actionUpdate($id): void
{
$model = User::find($id);
if (!$model){
throw new Exception(message: "The user not found");
}
$this->cgView->render("user/form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$user = User::find($id);
if (!$user){
throw new Exception(message: "The user not found");
}
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
$user = $userService->update($userForm, $user);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
User::find($id)->delete();
$this->redirect("/admin/user/");
}
}