Просмотр файла app/Controllers/FavoriteController.php

Размер файла: 3.19Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Controllers;
  6.  
  7. use App\Models\Favorite;
  8. use App\Models\Poll;
  9. use App\Models\Story;
  10. use App\Models\User;
  11. use App\Repositories\FavoriteRepository;
  12. use App\Services\Session;
  13. use App\Services\Validator;
  14. use App\Services\View;
  15. use MotorORM\Collection;
  16. use MotorORM\CollectionPaginate;
  17. use MotorORM\Pagination;
  18. use Psr\Http\Message\ResponseInterface as Response;
  19. use Psr\Http\Message\ServerRequestInterface as Request;
  20.  
  21. /**
  22. * FavoriteController
  23. */
  24. class FavoriteController extends Controller
  25. {
  26. public function __construct(
  27. protected View $view,
  28. protected Session $session,
  29. protected Validator $validator,
  30. protected FavoriteRepository $favoriteRepository,
  31. ) {}
  32.  
  33. /**
  34. * Favorites
  35. *
  36. * @param Response $response
  37. *
  38. * @return Response
  39. */
  40. public function index(Response $response): Response
  41. {
  42. $favorites = $this->favoriteRepository->getFavorites(setting('story.per_page'));
  43.  
  44. foreach ($favorites as $key => $favorite) {
  45. $favorites->remove($key);
  46. $favorites->add($favorite->story);
  47. }
  48.  
  49. $user = getUser();
  50. $stories = $favorites;
  51.  
  52. return $this->view->render(
  53. $response,
  54. 'users/favorite',
  55. compact('stories', 'user')
  56. );
  57. }
  58.  
  59. /**
  60. * Add/delete to favorite
  61. *
  62. * @param int $id
  63. * @param Request $request
  64. * @param Response $response
  65. *
  66. * @return Response
  67. */
  68. public function change(int $id, Request $request, Response $response): Response
  69. {
  70. $input = (array) $request->getParsedBody();
  71.  
  72. $model = Story::query()->find($id);
  73.  
  74. if (! $model) {
  75. return $this->json($response, [
  76. 'success' => false,
  77. 'message' => 'Запись не найдена!',
  78. ]);
  79. }
  80.  
  81. $this->validator
  82. ->required(['csrf'])
  83. ->same('csrf', $this->session->get('csrf'), 'Неверный идентификатор сессии, повторите действие!');
  84.  
  85. if ($this->validator->isValid($input)) {
  86. $favorite = Favorite::query()
  87. ->where('story_id', $id)
  88. ->where('user_id', getUser('id'))
  89. ->first();
  90.  
  91. if ($favorite) {
  92. $favorite->delete();
  93.  
  94. return $this->json($response, [
  95. 'success' => true,
  96. 'type' => 'delete',
  97. 'message' => 'Статья успешно удалена из избранного!',
  98. ]);
  99. }
  100.  
  101. Favorite::query()->create([
  102. 'user_id' => getUser('id'),
  103. 'story_id' => $id,
  104. 'created_at' => time(),
  105. ]);
  106.  
  107. return $this->json($response, [
  108. 'success' => true,
  109. 'type' => 'add',
  110. 'message' => 'Статья успешно добавлена в избранное!',
  111. ]);
  112. }
  113.  
  114. return $this->json($response, [
  115. 'success' => false,
  116. 'message' => current($this->validator->getErrors()),
  117. ]);
  118. }
  119. }