-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Open-RCA/eCom-laravel-backend…
… into chanelle
- Loading branch information
Showing
16 changed files
with
416 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\ProductRating; | ||
use App\Models\ProductSubCategory; | ||
use Illuminate\Http\JsonResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Mockery\Exception; | ||
|
||
class ProductRatingController extends Controller | ||
{ | ||
|
||
public function all(): JsonResponse { | ||
try { | ||
$product_ratings = ProductRating::all(); | ||
return response()->json($product_ratings); | ||
} | ||
catch (Exception $exception) { | ||
$RESPONSE = [ | ||
'success' => false, | ||
'message' => $exception->getMessage(), | ||
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR | ||
]; | ||
return response()->json($RESPONSE); | ||
} | ||
} | ||
|
||
public function show(ProductRating $productRating): JsonResponse { | ||
try { | ||
return response()->json($productRating); | ||
} | ||
catch (Exception $exception) { | ||
$RESPONSE = [ | ||
'success' => false, | ||
'message' => $exception->getMessage(), | ||
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR | ||
]; | ||
return response()->json($RESPONSE); | ||
} | ||
} | ||
|
||
public function create(Request $request): JsonResponse | ||
{ | ||
try { | ||
$validator = Validator::make($request->json()->all(), [ | ||
'product_id' => 'required|string|min:1|max:50|exists:products, _id', | ||
'rate' => ['integer', Rule::in([20, 40, 60, 80, 100])], | ||
'user_id' => 'required|string|min:1|max:50|exists:users, _id', | ||
'comment' | ||
]); | ||
|
||
if ($validator->fails()) | ||
return response()->json($validator->errors()); | ||
|
||
$productSubCategory = ProductRating::query()->create([ | ||
'category' => $request->json()->get('category'), | ||
'product_category_id' => $request->json()->get('product_category_id'), | ||
'description' => $request->json()->get('description') | ||
]); | ||
|
||
return response()->json($productSubCategory); | ||
} catch (Exception $exception) { | ||
$RESPONSE = [ | ||
'success' => false, | ||
'message' => $exception->getMessage(), | ||
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR | ||
]; | ||
return response()->json($RESPONSE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Models\Role; | ||
|
||
use Validator; | ||
|
||
class RoleController extends Controller | ||
{ | ||
/** | ||
* Create a role | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function create(Request $request) | ||
{ | ||
$validator = Validator::make($request->json()->all(), [ | ||
'type' => 'required|string|max:255', | ||
'description' => 'string|max:255' | ||
]); | ||
|
||
if($validator->fails()){ | ||
return response()->json($validator->errors(), 400); | ||
} | ||
|
||
$role = Role::query()->create([ | ||
'type' => $request->json()->get('type'), | ||
'description' =>$request->json()->get('description') | ||
]); | ||
|
||
if(!$role) return response()->json([ | ||
'message' => 'Failed to create role' | ||
], 500); | ||
|
||
return response()->json([ | ||
'message' => 'New role registered successfully', | ||
'role' => $role | ||
], 201); | ||
} | ||
|
||
/** | ||
* Get all roles | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function all() | ||
{ | ||
return response()->json(Role::all()); | ||
} | ||
|
||
/** | ||
* Show a certain role | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function show(Role $role) : JsonResponse | ||
{ | ||
return response()->json($role); | ||
} | ||
/** | ||
* Edit a certain role | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function edit(Role $role, Request $request){ | ||
$validator = Validator::make($request->json()->all(), [ | ||
'type' => 'required|string|max:255', | ||
'description' => 'string|max:255' | ||
]); | ||
|
||
if($validator->fails()) { | ||
return response()->json($validator->error(), 400); | ||
} | ||
|
||
$role->update($request->all()); | ||
|
||
$role->save(); | ||
|
||
return response()->json([ | ||
'message' => 'Role updated successfully', | ||
'role' => $role | ||
], 200); | ||
} | ||
|
||
/** | ||
* Delete role | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function delete(Role $role) | ||
{ | ||
$role->delete(); | ||
|
||
return response()->json([ | ||
'message' => 'Role deleted successfully' | ||
], 200); | ||
} | ||
} |
Oops, something went wrong.