Loading src/Repository/Contracts/RepositoryInterface.php +6 −18 Original line number Diff line number Diff line Loading @@ -13,6 +13,12 @@ interface RepositoryInterface { public function list(); public function show($id); /** * @param array $columns * @return mixed */ public function all($columns = array('*')); public function paginate($perPage = 10, $columns = array('*')); Loading @@ -27,22 +33,4 @@ interface RepositoryInterface public function findBy($field, $value, $columns = array('*')); /** * Load relations * * @param $relations * * @return $this */ public function with($relations); /** * Order collection by a given column * * @param string $column * @param string $direction * * @return $this */ public function orderBy($column, $direction = 'asc'); } No newline at end of file src/Repository/Eloquent/BaseRepository.php +20 −15 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ namespace Meibuyu\Micro\Repository\Eloquent; use Hyperf\DbConnection\Model\Model; use Hyperf\HttpServer\Contract\RequestInterface; use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\RepositoryException; use Meibuyu\Micro\Exceptions\ValidatorException; Loading @@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface */ private $container; /** * @var RequestInterface */ protected $request; /** * @var Model */ Loading @@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface /** * BaseRepository constructor. * @param ContainerInterface $container * @param RequestInterface $request * @throws RepositoryException */ public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container, RequestInterface $request) { $this->container = $container; $this->request = $request; $this->makeModel(); $this->makeValidator(); } Loading Loading @@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface /** * @param $id * @param array $columns * @return mixed * @return mixed|Model * @throws HttpResponseException */ public function find($id, $columns = ['*']) Loading @@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface public function list() { return $this->model->all(); return $this->all(); } public function with($relations) public function show($id) { $this->model = $this->model->with($relations); return $this; } public function orderBy($column, $direction = 'asc') { $this->model = $this->model->orderBy($column, $direction); return $this; return $this->find($id); } public function paginate($perPage = 10, $columns = ['*']) Loading Loading @@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface * @param array $attributes * @param $id * @return Model | mixed * @throws HttpResponseException * @throws ValidatorException */ public function update(array $attributes, $id) Loading @@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); } $model = $this->model->findOrFail($id); $model = $this->find($id); $model->fill($attributes); $model->save(); return $model; Loading @@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface public function delete($id) { $model = $this->find($id); try { $delete = $model->delete(); } catch (\Exception $e) { throw new HttpResponseException($e->getMessage()); } if ($delete !== false) { return $delete; } else { Loading Loading
src/Repository/Contracts/RepositoryInterface.php +6 −18 Original line number Diff line number Diff line Loading @@ -13,6 +13,12 @@ interface RepositoryInterface { public function list(); public function show($id); /** * @param array $columns * @return mixed */ public function all($columns = array('*')); public function paginate($perPage = 10, $columns = array('*')); Loading @@ -27,22 +33,4 @@ interface RepositoryInterface public function findBy($field, $value, $columns = array('*')); /** * Load relations * * @param $relations * * @return $this */ public function with($relations); /** * Order collection by a given column * * @param string $column * @param string $direction * * @return $this */ public function orderBy($column, $direction = 'asc'); } No newline at end of file
src/Repository/Eloquent/BaseRepository.php +20 −15 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ namespace Meibuyu\Micro\Repository\Eloquent; use Hyperf\DbConnection\Model\Model; use Hyperf\HttpServer\Contract\RequestInterface; use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\RepositoryException; use Meibuyu\Micro\Exceptions\ValidatorException; Loading @@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface */ private $container; /** * @var RequestInterface */ protected $request; /** * @var Model */ Loading @@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface /** * BaseRepository constructor. * @param ContainerInterface $container * @param RequestInterface $request * @throws RepositoryException */ public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container, RequestInterface $request) { $this->container = $container; $this->request = $request; $this->makeModel(); $this->makeValidator(); } Loading Loading @@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface /** * @param $id * @param array $columns * @return mixed * @return mixed|Model * @throws HttpResponseException */ public function find($id, $columns = ['*']) Loading @@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface public function list() { return $this->model->all(); return $this->all(); } public function with($relations) public function show($id) { $this->model = $this->model->with($relations); return $this; } public function orderBy($column, $direction = 'asc') { $this->model = $this->model->orderBy($column, $direction); return $this; return $this->find($id); } public function paginate($perPage = 10, $columns = ['*']) Loading Loading @@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface * @param array $attributes * @param $id * @return Model | mixed * @throws HttpResponseException * @throws ValidatorException */ public function update(array $attributes, $id) Loading @@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); } $model = $this->model->findOrFail($id); $model = $this->find($id); $model->fill($attributes); $model->save(); return $model; Loading @@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface public function delete($id) { $model = $this->find($id); try { $delete = $model->delete(); } catch (\Exception $e) { throw new HttpResponseException($e->getMessage()); } if ($delete !== false) { return $delete; } else { Loading