1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Models\Collect;
- use App\Repositories\Contracts\CollectInterface;
- /**
- * 收藏
- * @author lilin
- *
- */
- class CollectFacadeRepository extends BaseRepository implements CollectInterface
- {
- public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
- {
- $result = Collect::with(['scale'])->where(function($query) use($conditions){
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- if (isset($conditions['column'])){
- $query->where('column', $conditions['column']);
- }
- if (isset($conditions['object_id'])){
- $query->where('object_id', $conditions['object_id']);
- }
- })->orderByRaw($sort)->paginate($limit, $fields);
-
- return $this->response($result);
- }
-
- public function findBy(array $conditions, array $fields){
- $result = Collect::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- if (isset($conditions['column'])){
- $query->where('column', $conditions['column']);
- }
- if (isset($conditions['object_id'])){
- $query->where('object_id', $conditions['object_id']);
- }
- })->first($fields);
- return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
- }
-
- public function create(array $data)
- {
- $find = Collect::where('uid', $data['uid'])->where('column', $data['column'])->where('object_id', $data['object_id'])->first();
-
- if ($find){
- $result = Collect::destroy($find->id);
- }else{
- $result = Collect::create($data);
- }
-
- return $result ? $this->response() : $this->error()->fail();
- }
-
- public function updateBy(array $conditions, array $data)
- {
- $result = Collect::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- if (isset($conditions['column'])){
- $query->where('column', $conditions['column']);
- }
- if (isset($conditions['object_id'])){
- $query->where('object_id', $conditions['object_id']);
- }
- })->update($data);
- return $result ? $this->response($result) : $this->error()->fail();
- }
-
- public function deleteBy(array $conditions)
- {
- $result = Collect::where(function($query) use($conditions){
- if (isset($conditions['id'])){
- $query->where('id', $conditions['id']);
- }
- if (isset($conditions['uid'])){
- $query->where('uid', $conditions['uid']);
- }
- })->delete();
- return $result ? $this->response($result) : $this->error()->fail();
- }
- }
|