Ken hace 5 días
padre
commit
5198f6e94c
Se han modificado 2 ficheros con 53 adiciones y 1 borrados
  1. 31 0
      app/Http/Controllers/admin/Backflow.php
  2. 22 1
      app/Services/BackflowService.php

+ 31 - 0
app/Http/Controllers/admin/Backflow.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Http\Controllers\admin;
+
+use App\Constants\HttpStatus;
+use App\Http\Controllers\Controller;
+use App\Services\BackflowService;
+use Exception;
+use Illuminate\Validation\ValidationException;
+
+class Backflow extends Controller
+{
+    function index()
+    {
+        try {
+            $params = request()->validate([
+                'page' => ['required', 'integer', 'min:1'],
+                'limit' => ['required', 'integer', 'min:1'],
+                'member_id' => ['nullable', 'string'],
+            ]);
+            $params['page'] = request()->input('page', 1);
+            $params['limit'] = request()->input('limit', 10);
+            $result = BackflowService::paginate($params);
+        } catch (ValidationException $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error($e->getCode(), $e->getMessage());
+        }
+        return $this->success($result);
+    }
+}

+ 22 - 1
app/Services/BackflowService.php

@@ -49,7 +49,28 @@ class BackflowService extends BaseService
 
     public static function getWhere(array $search = []): array
     {
+        $where = [];
+        if (isset($search['id']) && !empty($search['id'])) {
+            $where[] = ['id', '=', $search['id']];
+        }
+
+        if (isset($search['member_id']) && !empty($search['member_id'])) {
+            $where[] = ['member_id', '=', $search['member_id']];
+        }
+
+
 
-        return [];
+        if (isset($search['status']) && $search['status'] != '') {
+            $where[] = ['status', '=', $search['status']];
+        }
+
+        return $where;
+    }
+
+    public static function paginate(array $search = []): array
+    {
+        $limit = isset($search['limit']) ? $search['limit'] : 15;
+        $paginator = self::$MODEL::where(self::getWhere($search))->paginate($limit);
+        return ['total' => $paginator->total(), 'data' => $paginator->items()];
     }
 }