UserController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Controllers\Api\Base;
  3. use App\Contracts\Repositories\Base\AdminRepository;
  4. use App\Http\Controllers\Controller;
  5. use App\Repositories\Enums\ResponseCodeEnum;
  6. use App\Repositories\Models\Base\Admin;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Hash;
  9. use Jiannei\Response\Laravel\Support\Facades\Response;
  10. use Prettus\Validator\Contracts\ValidatorInterface;
  11. use App\Repositories\Validators\Base\AdminValidator;
  12. /**
  13. * Class AdminsController.
  14. *
  15. * @package namespace App\Http\Controllers;
  16. */
  17. class UserController extends Controller
  18. {
  19. /**
  20. * @var AdminRepository
  21. */
  22. protected $repository;
  23. /**
  24. * @var AdminValidator
  25. */
  26. protected $validator;
  27. /**
  28. * AdminsController constructor.
  29. *
  30. * @param AdminRepository $repository
  31. * @param AdminValidator $validator
  32. */
  33. public function __construct(AdminRepository $repository, AdminValidator $validator)
  34. {
  35. $this->repository = $repository;
  36. $this->validator = $validator;
  37. }
  38. public function index()
  39. {
  40. $lists = $this->repository->paginate(request('per_page', self::PAGE_NUM));
  41. return Response::success($lists);
  42. }
  43. public function store(Request $request)
  44. {
  45. $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_CREATE));
  46. try {
  47. $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_CREATE)));
  48. // unset($data['role_id']);
  49. if ($data['password']) {
  50. $data['password'] = Hash::make($data['password']);
  51. }
  52. $admin = Admin::query()->create($data);
  53. return Response::success($admin);
  54. } catch (\Exception $e) {
  55. return $this->errorStore($e);
  56. }
  57. }
  58. public function show($id)
  59. {
  60. $patient = $this->repository->find($id);
  61. return Response::success($patient);
  62. }
  63. public function update(Request $request, $id)
  64. {
  65. $this->validate($request, $this->validator->getRules(ValidatorInterface::RULE_UPDATE));
  66. try {
  67. $data = $request->only(array_keys($this->validator->getRules(ValidatorInterface::RULE_UPDATE)));
  68. if (isset($data['password'])) {
  69. $data['password'] = Hash::make($data['password']);
  70. } else {
  71. unset($data['password']);
  72. }
  73. $re = Admin::query()->where('id', $id)->update($data);
  74. if ($re) {
  75. return Response::success(null);
  76. }
  77. return $this->errorFail();
  78. } catch (\Exception $e) {
  79. $this->errorStore($e);
  80. }
  81. }
  82. public function destroy($id)
  83. {
  84. try {
  85. $re = $this->repository->delete($id);
  86. if ($re) {
  87. return Response::success(null, T('successfully delete.'));
  88. }
  89. return Response::fail(T('Delete failed.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  90. } catch (\Exception $exception) {
  91. return Response::fail(T('Delete failed.'), ResponseCodeEnum::SERVICE_UPDATE_DATA_ERROR);
  92. }
  93. }
  94. }