CollectFacadeRepository.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Models\Collect;
  4. use App\Repositories\Contracts\CollectInterface;
  5. /**
  6. * 收藏
  7. * @author lilin
  8. *
  9. */
  10. class CollectFacadeRepository extends BaseRepository implements CollectInterface
  11. {
  12. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  13. {
  14. $result = Collect::with(['scale'])->where(function($query) use($conditions){
  15. if (isset($conditions['uid'])){
  16. $query->where('uid', $conditions['uid']);
  17. }
  18. if (isset($conditions['column'])){
  19. $query->where('column', $conditions['column']);
  20. }
  21. if (isset($conditions['object_id'])){
  22. $query->where('object_id', $conditions['object_id']);
  23. }
  24. })->orderByRaw($sort)->paginate($limit, $fields);
  25. return $this->response($result);
  26. }
  27. public function findBy(array $conditions, array $fields){
  28. $result = Collect::where(function($query) use($conditions){
  29. if (isset($conditions['id'])){
  30. $query->where('id', $conditions['id']);
  31. }
  32. if (isset($conditions['uid'])){
  33. $query->where('uid', $conditions['uid']);
  34. }
  35. if (isset($conditions['column'])){
  36. $query->where('column', $conditions['column']);
  37. }
  38. if (isset($conditions['object_id'])){
  39. $query->where('object_id', $conditions['object_id']);
  40. }
  41. })->first($fields);
  42. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  43. }
  44. public function create(array $data)
  45. {
  46. $find = Collect::where('uid', $data['uid'])->where('column', $data['column'])->where('object_id', $data['object_id'])->first();
  47. if ($find){
  48. $result = Collect::destroy($find->id);
  49. }else{
  50. $result = Collect::create($data);
  51. }
  52. return $result ? $this->response() : $this->error()->fail();
  53. }
  54. public function updateBy(array $conditions, array $data)
  55. {
  56. $result = Collect::where(function($query) use($conditions){
  57. if (isset($conditions['id'])){
  58. $query->where('id', $conditions['id']);
  59. }
  60. if (isset($conditions['uid'])){
  61. $query->where('uid', $conditions['uid']);
  62. }
  63. if (isset($conditions['column'])){
  64. $query->where('column', $conditions['column']);
  65. }
  66. if (isset($conditions['object_id'])){
  67. $query->where('object_id', $conditions['object_id']);
  68. }
  69. })->update($data);
  70. return $result ? $this->response($result) : $this->error()->fail();
  71. }
  72. public function deleteBy(array $conditions)
  73. {
  74. $result = Collect::where(function($query) use($conditions){
  75. if (isset($conditions['id'])){
  76. $query->where('id', $conditions['id']);
  77. }
  78. if (isset($conditions['uid'])){
  79. $query->where('uid', $conditions['uid']);
  80. }
  81. })->delete();
  82. return $result ? $this->response($result) : $this->error()->fail();
  83. }
  84. }