Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into chanelle
  • Loading branch information
chanelle740 committed Mar 12, 2021
2 parents 65e3264 + cfcfe6e commit 6edb545
Show file tree
Hide file tree
Showing 16 changed files with 416 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ APP_NAME=Ecommerce
APP_ENV=local
APP_KEY=base64:gpzJEN4AwReJpm1PrIZIeWxD6cDD+zMtAPqcetGDYAM=
APP_DEBUG=true
APP_URL=http://Server.test
APP_MODE=development
APP_DEV_URL=http://localhost:8000
APP_PROD_URL=http://Server.test

LOG_CHANNEL=stack
LOG_LEVEL=debug
Expand Down
6 changes: 4 additions & 2 deletions .rest
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
GET http://localhost:8000/api/product-sub-categories/products/603885b976690000580050d6
GET http://localhost:8000/api/products


###

GET http://localhost:8000/api/product-categories/603877d676690000580050b8
DELETE http://localhost:8000/api/products/604a1a48606800004c000ce3

###

Expand All @@ -13,6 +13,8 @@ Content-Type: application/json
{
"name": "product",
"product_sub_category_id": "603885b976690000580050d6",
"description": "This is a product",
"sizes": [23, 2],
"quantity": 4,
"unit_price": 4000
}
79 changes: 72 additions & 7 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

namespace App\Http\Controllers;

use App\Models\File;
use App\Models\Product;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Exception;
use Illuminate\Support\Str;

class ProductController extends Controller
{

public function all(): JsonResponse
{
try {
$products = Product::with('productSubCategory')->get();
$products = $this->returnImages(Product::with('productSubCategory')->get(), true);
return response()->json($products);
} catch (Exception $exception) {
dd($exception);
$RESPONSE = [
'success' => false,
'message' => $exception->getMessage(),
Expand All @@ -27,9 +28,46 @@ public function all(): JsonResponse
}
}

private function returnImages($products, $many=false) {
$productFiles = array();
if ($many) {
foreach ($products as $product) {
$productFiles = array();
foreach ($product->images as $image) {
$file = File::query()->find($image);

$domain = (env('APP_MODE') == 'development') ? env('APP_DEV_URL') : env('APP_PROD_URL');
$productFile = array(
'id' => $image,
'file_path' => $file->file_url,
'file_url' => $domain . ($file->file_url));
array_push($productFiles, $productFile);
}
$product->images = $productFiles;
}
return $products;
}
else {
$product = $products;
foreach ($product->images as $image) {
$file = File::query()->find($image);
$domain = (env('APP_MODE') == 'development') ? env('APP_DEV_URL') : env('APP_PROD_URL');
$productFile = array(
'id' => $image,
'file_path' => $file->file_url,
'file_url' => $domain . ($file->file_url));
array_push($productFiles, $productFile);
}
$product->images = $productFiles;
}
return $products;
}
public function show(Product $product): JsonResponse
{
try {

$product = Product::with('productSubCategory')->find($product->id);
$product = $this->returnImages($product);
return response()->json($product);
} catch (Exception $exception) {
$RESPONSE = [
Expand All @@ -49,6 +87,8 @@ public function create(Request $request): JsonResponse
'name' => 'required|string|min:1|max:50|unique:product_sub_categories',
'product_sub_category_id' => 'required|string|exists:product_categories,_id',
'unit_price' => 'required|numeric|min:1',
"sizes.*" => "numeric|distinct|min:1",
'description' => 'required|string|min:10',
'quantity' => 'required|numeric|min:1'
]);

Expand All @@ -57,8 +97,11 @@ public function create(Request $request): JsonResponse

$product = Product::query()->create([
'name' => $request->json()->get('name'),
'description' => $request->json()->get('description'),
'product_sub_category_id' => $request->json()->get('product_sub_category_id'),
'unit_price' => $request->json()->get('unit_price'),
'images' => array(),
'sizes' => $request->json()->get('sizes'),
'quantity' => $request->json()->get('quantity'),
'status' => 'ACTIVE'
]);
Expand All @@ -83,6 +126,8 @@ public function update(Request $request, Product $product): JsonResponse
'name' => 'required|string|min:1|max:50|unique:product_sub_categories',
'product_sub_category_id' => 'required|string|exists:product_categories,_id',
'unit_price' => 'required|numeric|min:1',
"sizes.*" => "numeric|distinct|min:1",
'description' => 'required|string|min:10',
'quantity' => 'required|numeric|min:1',
]);

Expand All @@ -91,8 +136,10 @@ public function update(Request $request, Product $product): JsonResponse

$product = Product::query()->create([
'name' => $request->json()->get('name'),
'description' => $request->json()->get('description'),
'product_sub_category_id' => $request->json()->get('product_sub_category_id'),
'unit_price' => $request->json()->get('unit_price'),
'sizes' => $request->json()->get('sizes'),
'quantity' => $request->json()->get('quantity')
]);

Expand All @@ -110,7 +157,10 @@ public function update(Request $request, Product $product): JsonResponse

public function saveProductImage(Request $request, Product $product): JsonResponse {
try {

$file = $request->only('file');
$color = $request->only('color');

if (empty($file)){
$RESPONSE = [
'success' => false,
Expand All @@ -121,18 +171,25 @@ public function saveProductImage(Request $request, Product $product): JsonRespon
}


$validator = Validator::make($file, [
$fileValidator = Validator::make($file, [
'file' => 'required|mimes:jpeg,jpg,png,gif|max:2048'
]);

if ($validator->fails())
return response()->json($validator->errors());
$colorValidator = Validator::make($color, [
'color' => ['string', 'min:3' , 'regex:/^(#(?:[0-9a-f]{2}){2,4}|#[0-9a-f]{3}|(?:rgba?|hsla?)\((?:\d+%?(?:deg|rad|grad|turn)?(?:,|\s)+){2,3}[\s\/]*[\d\.]+%?\))$/i'],
]);

if ($fileValidator->fails() || $colorValidator->fails())
return response()->json(['file' => $fileValidator->errors(), 'color' => $colorValidator->errors()]);


$file = $file['file'];
$savedFile = (new FileController)->save($file);

$product->push('images', $savedFile->_id);
$image = array('file' => $savedFile->id, 'color' => $color['color']);


$product->push('images', $image);

$product->save();
return response()->json($product);
Expand All @@ -149,7 +206,15 @@ public function saveProductImage(Request $request, Product $product): JsonRespon
public function delete(Product $product): JsonResponse
{
try {
return response()->json($product->delete());
if ($product->delete()) {
$RESPONSE = [
'success' => true,
'message' => 'Deleted Successfully',
'status' => JsonResponse::HTTP_OK
];
return response()->json($RESPONSE);
}

} catch (Exception $exception) {
$RESPONSE = [
'success' => false,
Expand Down
74 changes: 74 additions & 0 deletions app/Http/Controllers/ProductRatingController.php
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);
}
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/ProductSubCategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function create(Request $request): JsonResponse {
try {
$validator = Validator::make($request->json()->all(), [
'category' => 'required|string|min:1|max:50|unique:product_sub_categories',

'product_category_id' => 'required|string|exists:product_categories,_id',
'description' => 'required|string|min:1|max:50'
]);
Expand Down
100 changes: 100 additions & 0 deletions app/Http/Controllers/RoleController.php
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);
}
}
Loading

0 comments on commit 6edb545

Please sign in to comment.