Completed
Branch master (beb88f)
by Mohamed
05:11 queued 03:39
created

ApiController::getCards()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 22
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the Moo\FlashCard package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moo\FlashCard\Controller;
13
14
use Illuminate\Http\Request;
15
use Illuminate\Routing\Controller;
16
use Moo\FlashCard\Api\SearchableCardFilter;
17
use Moo\FlashCard\Entity\Card;
18
use Moo\FlashCard\Entity\Category;
19
use Spatie\QueryBuilder\Filter;
20
use Spatie\QueryBuilder\QueryBuilder;
21
22
/**
23
 * RestApiController is the default REST API controller.
24
 *
25
 * @author Mohamed Alsharaf <[email protected]>
26
 */
27
class ApiController extends Controller
28
{
29
    /**
30
     * Get collection of cards
31
     *
32
     * @param Request $request
33
     * @return \Illuminate\Http\JsonResponse
34
     */
35
    public function getCards(Request $request)
36
    {
37
        // Parameter for limit by
38
        $limit = (int)$request->input('limit');
39
40
        $cards = QueryBuilder::for(Card::class)
41
            ->defaultSort('title')
42
            ->allowedFilters(
43
                Filter::exact('id'),
44
                'slug',
45
                'title',
46
                'category_id',
47
                Filter::custom('search', SearchableCardFilter::class)
48
            )
49
            ->allowedIncludes('category')
50
            ->allowedAppends('short_modified')
51
            ->active();
52
53
        if ($limit) {
54
            $cards = $cards->simplePaginate($limit);
55
        } else {
56
            $cards = $cards->get();
57
        }
58
59
        return response()->json($cards);
60
    }
61
62
    /**
63
     * Get details of a card
64
     *
65
     * @return \Illuminate\Http\JsonResponse
66
     */
67
    public function getCard()
68
    {
69
        $card = QueryBuilder::for(Card::class)
70
            ->defaultSort('title')
71
            ->allowedFilters(
72
                Filter::exact('id'),
73
                'slug'
74
            )
75
            ->allowedIncludes('category')
76
            ->active()
77
            ->first();
78
79
        return response()->json($card);
80
    }
81
82
    /**
83
     * Get collection of categories
84
     *
85
     * @return \Illuminate\Http\JsonResponse
86
     */
87
    public function getCategories()
88
    {
89
        $categories = QueryBuilder::for(Category::class)
90
            ->defaultSort('title')
91
            ->allowedFilters(
92
                Filter::exact('id'),
93
                Filter::custom('search', SearchableCardFilter::class)
94
            )
95
            ->withCount([
96
                'cards' => function ($query) {
97
                    $query->active();
98
                }
99
            ])
100
            ->active()
101
            ->get();
102
103
        return response()->json($categories);
104
    }
105
}
106