scriptotek /
bibrex
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use App\Http\Resources\ItemCollection; |
||
| 6 | use App\Http\Resources\LibraryCollection; |
||
| 7 | use App\Http\Resources\ThingCollection; |
||
| 8 | use App\Item; |
||
| 9 | use App\Library; |
||
| 10 | use App\Thing; |
||
| 11 | use Illuminate\Http\Request; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @OA\Info( |
||
| 15 | * title="Bibrex", |
||
| 16 | * version="0.1.0" |
||
| 17 | * ) |
||
| 18 | */ |
||
| 19 | class PublicApiController extends Controller |
||
| 20 | { |
||
| 21 | protected function doc() |
||
| 22 | { |
||
| 23 | return response()->view('apidoc'); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @OA\Get( |
||
| 28 | * path="/api/libraries", |
||
| 29 | * summary="List libraries", |
||
| 30 | * description="Get a list of libraries.", |
||
| 31 | * tags={"Libraries"}, |
||
| 32 | * @OA\Response( |
||
| 33 | * response=200, |
||
| 34 | * description="success" |
||
| 35 | * ) |
||
| 36 | * ) |
||
| 37 | * |
||
| 38 | * @param Request $request |
||
| 39 | * @return LibraryCollection |
||
| 40 | */ |
||
| 41 | protected function libraries(Request $request) |
||
|
0 ignored issues
–
show
|
|||
| 42 | { |
||
| 43 | $resources = Library::query() |
||
| 44 | ->orderBy('name') |
||
| 45 | ->get(); |
||
| 46 | |||
| 47 | return new LibraryCollection($resources); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @OA\Get( |
||
| 52 | * path="/api/things", |
||
| 53 | * summary="List things", |
||
| 54 | * description="Get a list of things.", |
||
| 55 | * tags={"Things"}, |
||
| 56 | * @OA\Response( |
||
| 57 | * response=200, |
||
| 58 | * description="success" |
||
| 59 | * ), |
||
| 60 | * @OA\Parameter( |
||
| 61 | * name="library", |
||
| 62 | * in="query", |
||
| 63 | * description="Filter by library ID. The item counts will also reflect the selected library.", |
||
| 64 | * @OA\Schema( |
||
| 65 | * type="integer" |
||
| 66 | * ) |
||
| 67 | * ), |
||
| 68 | * @OA\Parameter( |
||
| 69 | * name="items", |
||
| 70 | * in="query", |
||
| 71 | * description="Include list of items for each thing.", |
||
| 72 | * @OA\Schema( |
||
| 73 | * type="boolean" |
||
| 74 | * ) |
||
| 75 | * ), |
||
| 76 | * @OA\Parameter( |
||
| 77 | * name="name", |
||
| 78 | * in="query", |
||
| 79 | * description="Filter by name, case-insensitive, truncate with '*'", |
||
| 80 | * @OA\Schema( |
||
| 81 | * type="string" |
||
| 82 | * ) |
||
| 83 | * ) |
||
| 84 | * ) |
||
| 85 | * |
||
| 86 | * @param Request $request |
||
| 87 | * @return ThingCollection |
||
| 88 | */ |
||
| 89 | protected function things(Request $request) |
||
| 90 | { |
||
| 91 | $query = Thing::with('settings'); |
||
| 92 | |||
| 93 | if ($request->items && strtolower($request->items) !== 'false') { |
||
| 94 | $query->with([ |
||
| 95 | 'items' => function ($query) use ($request) { |
||
| 96 | if (isset($request->library)) { |
||
| 97 | $query->where('library_id', '=', $request->library); |
||
| 98 | } |
||
| 99 | }, |
||
| 100 | 'items.loans', |
||
| 101 | ]); |
||
| 102 | $query->with('items.library'); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (isset($request->library)) { |
||
| 106 | $query->whereHas('items', function ($itemQuery) use ($request) { |
||
| 107 | $itemQuery->where('library_id', '=', $request->library); |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | |||
| 111 | // if (isset($request->name)) { |
||
| 112 | // $query->where('name', 'ilike', str_replace('*', '%', $request->name)); |
||
| 113 | // } |
||
| 114 | |||
| 115 | $resources = $query |
||
| 116 | ->orderBy('properties->name->nob') |
||
| 117 | ->get(); |
||
| 118 | |||
| 119 | return new ThingCollection($resources); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @OA\Get( |
||
| 124 | * path="/api/items", |
||
| 125 | * summary="List items", |
||
| 126 | * description="Get a list of items. Only non-deleted items are returned.", |
||
| 127 | * tags={"Items"}, |
||
| 128 | * @OA\Response( |
||
| 129 | * response=200, |
||
| 130 | * description="success" |
||
| 131 | * ), |
||
| 132 | * @OA\Parameter( |
||
| 133 | * name="library", |
||
| 134 | * in="query", |
||
| 135 | * description="Filter by library ID.", |
||
| 136 | * @OA\Schema( |
||
| 137 | * type="integer" |
||
| 138 | * ) |
||
| 139 | * ), |
||
| 140 | * @OA\Parameter( |
||
| 141 | * name="thing", |
||
| 142 | * in="query", |
||
| 143 | * description="Filter by thing ID.", |
||
| 144 | * @OA\Schema( |
||
| 145 | * type="integer" |
||
| 146 | * ) |
||
| 147 | * ) |
||
| 148 | * ) |
||
| 149 | * |
||
| 150 | * @param Request $request |
||
| 151 | * @return ItemCollection |
||
| 152 | */ |
||
| 153 | protected function items(Request $request) |
||
| 154 | { |
||
| 155 | $query = Item::query(); |
||
| 156 | |||
| 157 | $query->with('thing'); |
||
| 158 | |||
| 159 | if (isset($request->library)) { |
||
| 160 | $query->where('library_id', '=', $request->library); |
||
| 161 | } |
||
| 162 | |||
| 163 | if (isset($request->thing)) { |
||
| 164 | $query->where('thing_id', '=', $request->thing); |
||
| 165 | } |
||
| 166 | |||
| 167 | $resources = $query |
||
| 168 | ->orderBy('barcode') |
||
| 169 | ->get(); |
||
| 170 | |||
| 171 | return new ItemCollection($resources); |
||
| 172 | } |
||
| 173 | } |
||
| 174 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.