Blame view

api/app/Http/Controllers/CapecController.php 994 Bytes
0d8c0f816   Thanasis Naskos   initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  <?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use Cviebrock\LaravelElasticsearch\Facade as Elasticsearch;
  use Illuminate\Pagination\LengthAwarePaginator;
  use Illuminate\Pagination\Paginator;
  
  class CapecController extends Controller
  {
      public function index(Request $request)
      {
          $perPage = $request->get('perPage', 10);
          $from = ($request->get('page', 1) - 1) * $perPage;
  
          $params = [
              'size' => $perPage,
              'from' => $from,
              'body' => [
                  'query' => [
                      'match' => [
                          'type' => 'CAPEC'
                      ]
                  ]
              ]
          ];
  
          $res = Elasticsearch::search($params);
  
          return new LengthAwarePaginator(
              $res['hits']['hits'],
              $res['hits']['total']['value'],
              $perPage,
              Paginator::resolveCurrentPage(),
              [ 'path' => Paginator::resolveCurrentPath() ]);
      }
  }