Skip to content

Commit f037a06

Browse files
author
Shashank Jain
committed
Merge branch 'master' into 5.2
2 parents d397859 + fc81951 commit f037a06

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
This package provides a powerful Rest API functionality for your Laravel project, with minimum code required for you to write.
88

9-
**Note**: This package is under development and not recommended for use in production.
10-
119
## Documentation
1210
For full documentation please refer to [plugin's wiki](https://github.com/Froiden/laravel-rest-api/wiki).
1311

src/ApiController.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function index()
164164
*
165165
* @return mixed
166166
*/
167-
public function show()
167+
public function show(...$args)
168168
{
169169
// We need to do this in order to support multiple parameter resource routes. For example,
170170
// if we map route /user/{user}/comments/{comment} to a controller, Laravel will pass `user`
@@ -216,7 +216,7 @@ public function store()
216216
return ApiResponse::make("Resource created successfully", [ "id" => $object->id ], $meta);
217217
}
218218

219-
public function update()
219+
public function update(...$args)
220220
{
221221
\DB::beginTransaction();
222222

@@ -254,7 +254,7 @@ public function update()
254254
return ApiResponse::make("Resource updated successfully", [ "id" => $object->id ], $meta);
255255
}
256256

257-
public function destroy()
257+
public function destroy(...$args)
258258
{
259259
\DB::beginTransaction();
260260

@@ -436,7 +436,13 @@ protected function addIncludes()
436436
// need to be attached
437437
if ($q instanceof BelongsTo) {
438438
$fields[] = $q->getOtherKey();
439-
// $relations[$key]["foreign"] = $q->getOtherKey();
439+
440+
if (strpos($key, ".") !== false) {
441+
$parts = explode(".", $key);
442+
array_pop($parts);
443+
444+
$relation["limit"] = $relations[implode(".", $parts)]["limit"];
445+
}
440446
}
441447
else if ($q instanceof HasOne) {
442448
$fields[] = $q->getForeignKey();
@@ -448,6 +454,8 @@ protected function addIncludes()
448454
else if ($q instanceof HasMany) {
449455
$fields[] = $q->getForeignKey();
450456
$relations[$key]["foreign"] = $q->getForeignKey();
457+
458+
$q->orderBy($primaryKey, ($relation["order"] == "chronological") ? "ASC" : "DESC");
451459
}
452460

453461
$q->select($fields);
@@ -694,7 +702,7 @@ protected function getMetaData($single = false)
694702
\DB::disableQueryLog();
695703

696704
$meta["queries"] = count($log);
697-
// $meta["queries_list"] = $log;
705+
$meta["queries_list"] = $log;
698706
}
699707

700708
return $meta;
@@ -770,7 +778,7 @@ protected function isUpdate()
770778
*/
771779
protected function isDelete()
772780
{
773-
return in_array("delete", explode(".", request()->route()->getName()));
781+
return in_array("destroy", explode(".", request()->route()->getName()));
774782
}
775783

776784
/**

src/Middleware/ApiMiddleware.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Froiden\RestAPI\ApiResponse;
77
use Froiden\RestAPI\Exceptions\UnauthorizedException;
8+
use Illuminate\Support\Str;
89
use Symfony\Component\HttpFoundation\StreamedResponse;
910

1011
class ApiMiddleware
@@ -15,7 +16,7 @@ public function handle($request, Closure $next)
1516
// Add CORS headers
1617
$response = $next($request);
1718

18-
if ($response->getStatusCode() == 403 && $response->getContent() == "Forbidden") {
19+
if ($response->getStatusCode() == 403 && ($response->getContent() == "Forbidden" || Str::contains($response->getContent(), ['HttpException', 'authorized']))) {
1920
$response = ApiResponse::exception(new UnauthorizedException());
2021
}
2122

src/RequestParser.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class RequestParser
2222
/**
2323
* Extracts fields parts
2424
*/
25-
const FIELD_PARTS_REGEX = "/([^{.]+)(.limit\\(([0-9]+)\\)|.offset\\(([0-9]+)\\)|.order\\(([A-Za-z]+)\\))*(\\{((?>[^{}]+)|(?R))*\\})?/i";
25+
const FIELD_PARTS_REGEX = "/([^{.]+)(.limit\\(([0-9]+)\\)|.offset\\(([0-9]+)\\)|.order\\(([A-Za-z_]+)\\))*(\\{((?>[^{}]+)|(?R))*\\})?/i";
2626

2727
/**
2828
* Checks if filters are correctly specified
@@ -456,8 +456,6 @@ private function parseFields($fields)
456456
else {
457457
$this->relations[$parent]["fields"][] = $singular;
458458
}
459-
460-
\Log::debug($this->relations[$parent]);
461459
}
462460
else {
463461

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function createTables()
116116
Schema::create('dummy_users', function (Blueprint $table) {
117117
$table->increments('id');
118118
$table->string('name');
119-
$table->string('email')->unique();
119+
$table->string('email', 100)->unique();
120120
$table->integer('age');
121121
$table->timestamps();
122122
});

0 commit comments

Comments
 (0)