Просмотр файла app/Repositories/StoryRepository.php

Размер файла: 3.4Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Repositories;
  6.  
  7. use App\Models\Story;
  8. use App\Models\Tag;
  9. use MotorORM\Collection;
  10. use MotorORM\CollectionPaginate;
  11.  
  12. class StoryRepository implements RepositoryInterface
  13. {
  14. /**
  15. * Get by id
  16. *
  17. * @param int $id
  18. *
  19. * @return Story|null
  20. */
  21. public function getById(int $id): ?Story
  22. {
  23. return Story::query()->find($id);
  24. }
  25.  
  26. /**
  27. * Get by slug
  28. *
  29. * @param string $slug
  30. *
  31. * @return Story|null
  32. */
  33. public function getBySlug(string $slug): ?Story
  34. {
  35. $data = explode('-', $slug);
  36.  
  37. return Story::query()->find(end($data));
  38. }
  39.  
  40. /**
  41. * Get stories
  42. *
  43. * @param int $perPage
  44. *
  45. * @return CollectionPaginate<Story>
  46. */
  47. public function getStories(int $perPage): CollectionPaginate
  48. {
  49. return Story::query()
  50. ->when(! isAdmin(), function (Story $query) {
  51. $query->active()
  52. ->where('created_at', '<', time());
  53. })
  54. ->orderByDesc('locked')
  55. ->orderByDesc('created_at')
  56. ->with(['user', 'poll', 'comments', 'favorite', 'favorites'])
  57. ->paginate($perPage);
  58. }
  59.  
  60. /**
  61. * Get user stories
  62. *
  63. * @param int $userId
  64. * @param int $perPage
  65. *
  66. * @return CollectionPaginate
  67. */
  68. public function getStoriesByUserId(int $userId, int $perPage): CollectionPaginate
  69. {
  70. return Story::query()
  71. ->when(! isAdmin(), function (Story $query) {
  72. $query->where('created_at', '<', time());
  73. })
  74. ->where('user_id', $userId)
  75. ->orderByDesc('locked')
  76. ->orderByDesc('created_at')
  77. ->with(['user', 'poll', 'comments', 'favorite', 'favorites'])
  78. ->paginate($perPage);
  79. }
  80.  
  81. /**
  82. * Get stories by tag
  83. *
  84. * @param string $tag
  85. * @param int $perPage
  86. *
  87. * @return CollectionPaginate<Story>
  88. */
  89. public function getStoriesByTag(string $tag, int $perPage): CollectionPaginate
  90. {
  91. $tags = Tag::query()
  92. ->where('tag', 'like', $tag)
  93. ->get()
  94. ->pluck('story_id');
  95.  
  96. return Story::query()
  97. ->active()
  98. ->where('created_at', '<', time())
  99. ->whereIn('id', $tags)
  100. ->orderByDesc('created_at')
  101. ->paginate($perPage);
  102. }
  103.  
  104. /**
  105. * Get stories by search
  106. *
  107. * @param string $search
  108. * @param int $perPage
  109. *
  110. * @return CollectionPaginate<Story>
  111. */
  112. public function getStoriesBySearch(string $search, int $perPage): CollectionPaginate
  113. {
  114. return Story::query()
  115. ->active()
  116. ->where('created_at', '<', time())
  117. ->where('text', 'like', $search)
  118. ->orderByDesc('created_at')
  119. ->paginate($perPage)
  120. ->appends(['search' => $search]);
  121. }
  122.  
  123. /**
  124. * Get active stories
  125. *
  126. * @return Collection<Story>
  127. */
  128. public function getActiveStories(): Collection
  129. {
  130. return Story::query()
  131. ->active()
  132. ->where('created_at', '<', time())
  133. ->get();
  134. }
  135.  
  136. /**
  137. * Get count stories
  138. *
  139. * @return int
  140. */
  141. public function getCount(): int
  142. {
  143. return Story::query()
  144. ->active()
  145. ->where('created_at', '<', time())
  146. ->count();
  147. }
  148. }