Skip to content

Commit ea7356e

Browse files
committed
refact: code optimization
1 parent 5faeac5 commit ea7356e

File tree

8 files changed

+43
-120
lines changed

8 files changed

+43
-120
lines changed

app/Http/Controllers/API/CategoryController.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public function show(Category $category): JsonResponse
4747
{
4848
if (!$this->canAccess($category)) {
4949
return $this->error([], AuthConstants::PERMISSION);
50-
} else {
51-
return $this->success(new CategoryResource($category));
5250
}
51+
52+
return $this->success(new CategoryResource($category));
5353
}
5454

5555
/**
@@ -61,14 +61,14 @@ public function update(CategoryRequest $request, Category $category): JsonRespon
6161
{
6262
if (!$this->canAccess($category)) {
6363
return $this->error([], AuthConstants::PERMISSION);
64-
} else {
65-
$category->update($request->all());
66-
67-
return $this->success(
68-
new CategoryResource($category),
69-
CategoryConstants::UPDATE
70-
);
7164
}
65+
66+
$category->update($request->all());
67+
68+
return $this->success(
69+
new CategoryResource($category),
70+
CategoryConstants::UPDATE
71+
);
7272
}
7373

7474
/**
@@ -79,10 +79,10 @@ public function destroy(Category $category): JsonResponse
7979
{
8080
if (!$this->canAccess($category)) {
8181
return $this->error([], AuthConstants::PERMISSION);
82-
} else {
83-
$category->delete();
84-
85-
return $this->success([], CategoryConstants::DESTROY);
8682
}
83+
84+
$category->delete();
85+
86+
return $this->success([], CategoryConstants::DESTROY);
8787
}
8888
}

app/Http/Controllers/API/ProductController.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function show(Product $product): JsonResponse
5353
{
5454
if (!$this->canAccess($product)) {
5555
return $this->error([], AuthConstants::PERMISSION);
56-
} else {
57-
return $this->success(new ProductResource($product));
5856
}
57+
58+
return $this->success(new ProductResource($product));
5959
}
6060

6161
/**
@@ -67,20 +67,20 @@ public function update(ProductRequest $request, Product $product): JsonResponse
6767
{
6868
if (!$this->canAccess($product)) {
6969
return $this->error(AuthConstants::PERMISSION);
70-
} else {
71-
if (isset($request->categories)) {
72-
$categories = Category::ForUserByIds($request->categories);
73-
74-
$product->categories()->detach();
75-
if (!$categories->isEmpty()) {
76-
$product->categories()->attach($categories);
77-
}
78-
}
70+
}
7971

80-
$product->update($request->all());
72+
if (isset($request->categories)) {
73+
$categories = Category::ForUserByIds($request->categories);
8174

82-
return $this->success(new ProductResource($product), ProductConstants::UPDATE);
75+
$product->categories()->detach();
76+
if (!$categories->isEmpty()) {
77+
$product->categories()->attach($categories);
78+
}
8379
}
80+
81+
$product->update($request->all());
82+
83+
return $this->success(new ProductResource($product), ProductConstants::UPDATE);
8484
}
8585

8686
/**
@@ -91,10 +91,10 @@ public function destroy(Product $product): JsonResponse
9191
{
9292
if (!$this->canAccess($product)) {
9393
return $this->error(AuthConstants::PERMISSION);
94-
} else {
95-
$product->delete();
96-
97-
return $this->success([], ProductConstants::DESTROY);
9894
}
95+
96+
$product->delete();
97+
98+
return $this->success([], ProductConstants::DESTROY);
9999
}
100100
}

app/Http/Middleware/ApiAuthenticate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function handle(Request $request, Closure $next): Response
2222
{
2323
if ($user = auth('sanctum')->user()) {
2424
auth()->login($user);
25-
25+
2626
return $next($request);
2727
}
2828

app/Http/Requests/AuthRequest.php

-20
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace App\Http\Requests;
44

5-
use App\Constants\ValidationConstants;
6-
use App\Http\Traits\HttpResponses;
7-
use Illuminate\Contracts\Validation\Validator;
85
use Illuminate\Foundation\Http\FormRequest;
9-
use Illuminate\Http\Exceptions\HttpResponseException;
106

117
class AuthRequest extends FormRequest
128
{
13-
use HttpResponses;
14-
159
/**
1610
* @return array
1711
*/
@@ -22,18 +16,4 @@ public function rules(): array
2216
'password' => 'required',
2317
];
2418
}
25-
26-
/**
27-
* @param Validator $validator
28-
* @return HttpResponseException
29-
*/
30-
public function failedValidation(Validator $validator): HttpResponseException
31-
{
32-
throw new HttpResponseException(
33-
$this->error(
34-
$validator->errors()->messages(),
35-
ValidationConstants::ERROR
36-
)
37-
);
38-
}
3919
}

app/Http/Requests/CategoryRequest.php

-20
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace App\Http\Requests;
44

5-
use App\Constants\ValidationConstants;
6-
use App\Http\Traits\HttpResponses;
7-
use Illuminate\Contracts\Validation\Validator;
85
use Illuminate\Foundation\Http\FormRequest;
9-
use Illuminate\Http\Exceptions\HttpResponseException;
106

117
class CategoryRequest extends FormRequest
128
{
13-
use HttpResponses;
14-
159
/**
1610
* @return array
1711
*/
@@ -21,18 +15,4 @@ public function rules(): array
2115
'name' => 'required|min:4',
2216
];
2317
}
24-
25-
/**
26-
* @param Validator $validator
27-
* @return HttpResponseException
28-
*/
29-
public function failedValidation(Validator $validator): HttpResponseException
30-
{
31-
throw new HttpResponseException(
32-
$this->error(
33-
$validator->errors()->messages(),
34-
ValidationConstants::ERROR
35-
)
36-
);
37-
}
3818
}

app/Http/Requests/ProductRequest.php

-20
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace App\Http\Requests;
44

5-
use App\Constants\ValidationConstants;
6-
use App\Http\Traits\HttpResponses;
7-
use Illuminate\Contracts\Validation\Validator;
85
use Illuminate\Foundation\Http\FormRequest;
9-
use Illuminate\Http\Exceptions\HttpResponseException;
106

117
class ProductRequest extends FormRequest
128
{
13-
use HttpResponses;
14-
159
/**
1610
* @return array
1711
*/
@@ -22,18 +16,4 @@ public function rules(): array
2216
'price' => 'required|numeric',
2317
];
2418
}
25-
26-
/**
27-
* @param Validator $validator
28-
* @return HttpResponseException
29-
*/
30-
public function failedValidation(Validator $validator): HttpResponseException
31-
{
32-
throw new HttpResponseException(
33-
$this->error(
34-
$validator->errors()->messages(),
35-
ValidationConstants::ERROR
36-
)
37-
);
38-
}
3919
}

app/Http/Requests/RegisterRequest.php

-20
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace App\Http\Requests;
44

5-
use App\Constants\ValidationConstants;
6-
use App\Http\Traits\HttpResponses;
7-
use Illuminate\Contracts\Validation\Validator;
85
use Illuminate\Foundation\Http\FormRequest;
9-
use Illuminate\Http\Exceptions\HttpResponseException;
106

117
class RegisterRequest extends FormRequest
128
{
13-
use HttpResponses;
14-
159
/**
1610
* @return array
1711
*/
@@ -24,18 +18,4 @@ public function rules(): array
2418
'confirm_password' => 'required|same:password'
2519
];
2620
}
27-
28-
/**
29-
* @param Validator $validator
30-
* @return HttpResponseException
31-
*/
32-
public function failedValidation(Validator $validator): HttpResponseException
33-
{
34-
throw new HttpResponseException(
35-
$this->error(
36-
$validator->errors()->messages(),
37-
ValidationConstants::ERROR
38-
)
39-
);
40-
}
4121
}

app/Http/Traits/HttpResponses.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
trait HttpResponses
99
{
1010
/**
11-
* @param [type] $data
12-
* @param [type] $message
13-
* @param [type] $code
11+
* @param $data
12+
* @param string|null $message
13+
* @param int $code
1414
* @return JsonResponse
1515
*/
16-
protected function success($data, $message = null, $code = ResponseAlias::HTTP_OK): JsonResponse
16+
protected function success($data, string $message = null, int $code = ResponseAlias::HTTP_OK): JsonResponse
1717
{
1818
return response()->json([
1919
'status' => '',
@@ -23,13 +23,16 @@ protected function success($data, $message = null, $code = ResponseAlias::HTTP_O
2323
}
2424

2525
/**
26-
* @param [type] $data
27-
* @param [type] $message
28-
* @param [type] $code
26+
* @param $data
27+
* @param string|null $message
28+
* @param int $code
2929
* @return JsonResponse
3030
*/
31-
protected function error($data, $message = null, $code = ResponseAlias::HTTP_BAD_REQUEST): JsonResponse
32-
{
31+
protected function error(
32+
$data,
33+
string $message = null,
34+
int $code = ResponseAlias::HTTP_BAD_REQUEST
35+
): JsonResponse {
3336
return response()->json([
3437
'status' => '',
3538
'message' => $message,

0 commit comments

Comments
 (0)