CategoryController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace SixShop\News\Controller\Api;
  3. use think\Response;
  4. use think\Request;
  5. use SixShop\News\Service\NewsCategoryService;
  6. class CategoryController
  7. {
  8. private $service;
  9. public function __construct(NewsCategoryService $service)
  10. {
  11. $this->service = $service;
  12. }
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return Response
  17. */
  18. public function index(): Response
  19. {
  20. $list = $this->service->getList();
  21. return json($list);
  22. }
  23. /**
  24. * Display the specified resource.
  25. *
  26. * @param int $id
  27. * @return Response
  28. */
  29. public function read($id): Response
  30. {
  31. $item = $this->service->getById($id);
  32. return json($item);
  33. }
  34. /**
  35. * Save a new resource in storage.
  36. *
  37. * @param Request $request
  38. * @return Response
  39. */
  40. public function save(Request $request): Response
  41. {
  42. $data = $request->post();
  43. $result = $this->service->create($data);
  44. return json($result);
  45. }
  46. /**
  47. * Update the specified resource in storage.
  48. *
  49. * @param Request $request
  50. * @param int $id
  51. * @return Response
  52. */
  53. public function update(Request $request, $id): Response
  54. {
  55. $data = $request->put();
  56. $result = $this->service->update($id, $data);
  57. return json($result);
  58. }
  59. /**
  60. * Remove the specified resource from storage.
  61. *
  62. * @param int $id
  63. * @return Response
  64. */
  65. public function delete($id): Response
  66. {
  67. $result = $this->service->delete($id);
  68. return json($result);
  69. }
  70. }