Commit cf24e168 authored by 王源's avatar 王源 🎧

优化repository文件

parent 1652c299
...@@ -13,6 +13,12 @@ interface RepositoryInterface ...@@ -13,6 +13,12 @@ interface RepositoryInterface
{ {
public function list(); public function list();
public function show($id);
/**
* @param array $columns
* @return mixed
*/
public function all($columns = array('*')); public function all($columns = array('*'));
public function paginate($perPage = 10, $columns = array('*')); public function paginate($perPage = 10, $columns = array('*'));
...@@ -27,22 +33,4 @@ interface RepositoryInterface ...@@ -27,22 +33,4 @@ interface RepositoryInterface
public function findBy($field, $value, $columns = array('*')); 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
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace Meibuyu\Micro\Repository\Eloquent; namespace Meibuyu\Micro\Repository\Eloquent;
use Hyperf\DbConnection\Model\Model; use Hyperf\DbConnection\Model\Model;
use Hyperf\HttpServer\Contract\RequestInterface;
use Meibuyu\Micro\Exceptions\HttpResponseException; use Meibuyu\Micro\Exceptions\HttpResponseException;
use Meibuyu\Micro\Exceptions\RepositoryException; use Meibuyu\Micro\Exceptions\RepositoryException;
use Meibuyu\Micro\Exceptions\ValidatorException; use Meibuyu\Micro\Exceptions\ValidatorException;
...@@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -24,6 +25,11 @@ abstract class BaseRepository implements RepositoryInterface
*/ */
private $container; private $container;
/**
* @var RequestInterface
*/
protected $request;
/** /**
* @var Model * @var Model
*/ */
...@@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -37,11 +43,13 @@ abstract class BaseRepository implements RepositoryInterface
/** /**
* BaseRepository constructor. * BaseRepository constructor.
* @param ContainerInterface $container * @param ContainerInterface $container
* @param RequestInterface $request
* @throws RepositoryException * @throws RepositoryException
*/ */
public function __construct(ContainerInterface $container) public function __construct(ContainerInterface $container, RequestInterface $request)
{ {
$this->container = $container; $this->container = $container;
$this->request = $request;
$this->makeModel(); $this->makeModel();
$this->makeValidator(); $this->makeValidator();
} }
...@@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -100,7 +108,7 @@ abstract class BaseRepository implements RepositoryInterface
/** /**
* @param $id * @param $id
* @param array $columns * @param array $columns
* @return mixed * @return mixed|Model
* @throws HttpResponseException * @throws HttpResponseException
*/ */
public function find($id, $columns = ['*']) public function find($id, $columns = ['*'])
...@@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -123,20 +131,12 @@ abstract class BaseRepository implements RepositoryInterface
public function list() 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->find($id);
return $this;
}
public function orderBy($column, $direction = 'asc')
{
$this->model = $this->model->orderBy($column, $direction);
return $this;
} }
public function paginate($perPage = 10, $columns = ['*']) public function paginate($perPage = 10, $columns = ['*'])
...@@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -164,6 +164,7 @@ abstract class BaseRepository implements RepositoryInterface
* @param array $attributes * @param array $attributes
* @param $id * @param $id
* @return Model | mixed * @return Model | mixed
* @throws HttpResponseException
* @throws ValidatorException * @throws ValidatorException
*/ */
public function update(array $attributes, $id) public function update(array $attributes, $id)
...@@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -172,7 +173,7 @@ abstract class BaseRepository implements RepositoryInterface
$this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE); $this->validator->with($attributes)->setId($id)->passesOrFail(ValidatorInterface::RULE_UPDATE);
} }
$model = $this->model->findOrFail($id); $model = $this->find($id);
$model->fill($attributes); $model->fill($attributes);
$model->save(); $model->save();
return $model; return $model;
...@@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface ...@@ -186,7 +187,11 @@ abstract class BaseRepository implements RepositoryInterface
public function delete($id) public function delete($id)
{ {
$model = $this->find($id); $model = $this->find($id);
try {
$delete = $model->delete(); $delete = $model->delete();
} catch (\Exception $e) {
throw new HttpResponseException($e->getMessage());
}
if ($delete !== false) { if ($delete !== false) {
return $delete; return $delete;
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment