PostController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 15:58
  7. */
  8. namespace App\Http\Controllers\Post;
  9. use App\Repositories\Post\PostRepository;
  10. use App\Transformers\Post\PostTransformer;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Validator;
  13. use App\Http\Controllers\Controller;
  14. use Illuminate\Validation\Rule;
  15. use League\Fractal\Manager;
  16. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  17. use League\Fractal\Resource\Collection;
  18. class PostController extends Controller
  19. {
  20. public function __construct(PostRepository $postRepository)
  21. {
  22. $this->postRepository = $postRepository;
  23. }
  24. /**
  25. * 内容列表
  26. */
  27. public function index(Request $request)
  28. {
  29. $productList = $this->postRepository->lists($request->all());
  30. $fractal = new Manager();
  31. $resource = new Collection($productList, new PostTransformer());
  32. $resource->setPaginator(new IlluminatePaginatorAdapter($productList));
  33. $data = $fractal->createData($resource)->toArray();
  34. $data['extra'] = [
  35. 'filters' => [
  36. 'keyword',
  37. 'content',
  38. 'topic_ids',
  39. 'category_ids',
  40. 'is_suggest',
  41. 'created_at',
  42. 'type',
  43. 'sort'
  44. ],
  45. 'columns' => [
  46. 'id',
  47. 'created_at',
  48. 'uid',
  49. 'topic',
  50. 'content',
  51. 'location',
  52. 'pv',
  53. 'praise_count',
  54. 'share_count',
  55. 'comment_count',
  56. 'collect_count',
  57. 'create_bean',
  58. 'is_suggest'
  59. ]
  60. ];
  61. return $data;
  62. }
  63. /**
  64. * 发布内容
  65. */
  66. public function create(Request $request)
  67. {
  68. $validator = Validator::make($request->all(), [
  69. 'uid' => 'required|integer',
  70. 'type' => ['required',Rule::in('image', 'video', 'html')],
  71. 'is_suggest' => ['required',Rule::in(0, 1)],
  72. 'img' => 'required|url',
  73. 'video' => 'required_if:type,video|string|url',
  74. 'topic_ids' => 'required|string|max:64',
  75. 'title' => 'nullable|string|max:20',
  76. 'content' => 'required|string|max:20',
  77. 'location' => 'required|string',
  78. 'imgs' => 'required_if:type,image|array|max:9',
  79. 'imgs.*' => 'required|url',
  80. ]);
  81. if ($validator->fails()) {
  82. return $this->response->error($validator->errors()->first(), 500);
  83. }
  84. return $this->postRepository->create($request->all());
  85. }
  86. /**
  87. * 推荐内容
  88. */
  89. public function suggest(Request $request)
  90. {
  91. $validator = Validator::make($request->all(), [
  92. 'id' => 'required|integer',
  93. ]);
  94. if ($validator->fails()) {
  95. return $this->response->error($validator->errors()->first(), 500);
  96. }
  97. return $this->postRepository->suggest($request->all());
  98. }
  99. /**
  100. * 删除内容
  101. */
  102. public function delete(Request $request)
  103. {
  104. $validator = Validator::make($request->all(), [
  105. 'id' => 'required|integer',
  106. ]);
  107. if ($validator->fails()) {
  108. return $this->response->error($validator->errors()->first(), 500);
  109. }
  110. return $this->postRepository->delete($request->all());
  111. }
  112. /**
  113. * 隐藏内容
  114. */
  115. public function hide(Request $request)
  116. {
  117. $validator = Validator::make($request->all(), [
  118. 'id' => 'required|integer',
  119. ]);
  120. if ($validator->fails()) {
  121. return $this->response->error($validator->errors()->first(), 500);
  122. }
  123. return $this->postRepository->hide($request->all());
  124. }
  125. }