Skip to content

Commit

Permalink
Merge pull request #4 from Open-RCA/chanelle
Browse files Browse the repository at this point in the history
Chanelle
  • Loading branch information
chanelle740 authored Mar 12, 2021
2 parents cfcfe6e + 6edb545 commit e45c860
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 37 deletions.
119 changes: 112 additions & 7 deletions app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,25 @@ class OrderController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index():JsonResponse
public function all():JsonResponse
{

return response()->json(Order::all());
}

public function getProductsOrder(Order $order):JsonResponse{
try {
return response()->json($order::with('products')->get());
} catch (Exception $th) {
//throw $th;

$RESPONSE = [
'success' => false,
'message' => $th->getMessage(),
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR
];

return response()->json($RESPONSE);
}
}


Expand All @@ -30,9 +46,53 @@ public function index():JsonResponse
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(Request $request)
{
try {
$validator = Validator::make($request->json()->all(), [

'user_id'=>'required|string|exists:users,_id',
'address'=>'required|string|min:1|max:255',
'city'=>'required|string|min:1|max:255',
'country'=>'required|string|min:1|max:255',
'phone_number'=>'required|string|min:1|max:20',
'items' => 'required|array',
'items.*.product_id' => 'required|string|exists:products,_id',
'items.*.quantity' => 'required|integer|min:0'
]);



if($validator->fails())
return response()->json($validator->errors(), 400);

$order = Order::query()->create([
'user_id'=>$request->json()->get('user_id'),
'address'=>$request->json()->get('address'),
'city'=>$request->json()->get('city'),
'country'=>$request->json()->get('country'),
'phone_number'=>$request->json()->get('phone_number')
]);

$order->items()->createMany($request->json()->get("items"));

$order->items;

return response()->json($order);

} catch (Exception $th) {
$RESPONSE = [
'success' => false,
'message' => $th->getMessage(),
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR
];

return response()-json($RESPONSE);

//throw $th;
}
//

}

/**
Expand All @@ -54,7 +114,16 @@ public function store(Request $request)
*/
public function show(Order $order)
{
//
try {
return response()->json($order);
} catch (Exception $th) {
$RESPONSE = [
'success' => false,
'message' => $th->getMessage(),
'status' => JsonResponse::HTTP_INTERNAL_SERVER_ERROR
];
return response()->json($RESPONSE);
}
}

/**
Expand All @@ -77,7 +146,39 @@ public function edit(Order $order)
*/
public function update(Request $request, Order $order)
{
//
try {
$validator = Validator::make($request->json()->all(),[
'user_id'=>'required|string|exists:users,_id',
'address'=>'required|string|min:1|max:255',
'city'=>'required|string|min:1|max:255',
'country'=>'required|string|min:1|max:255',
'phone_number'=>'required|string|min:1|max:20',
'items' => 'required|array',
'items.*.product_id' => 'required|string|exists:products,_id',
'items.*.quantity' => 'required|integer|min:0'
]);
if($validator->fails())
return response()->json($validator->errors());

$order = Order::query()->update([
'user_id'=>$request->json()->get('user_id'),
'address'=>$request->json()->get('address'),
'city'=>$request->json()->get('city'),
'country'=>$request->json()->get('country'),
'phone_number'=>$request->json()->get('phone_number')
]);
return response()->json($order);

} catch (Exception $th) {
//throw $th;
$RESPONSE = [
'success'=>false,
'message'=>$th->getMessage(),
'status'=>JsonResponse::HTTP_INTERNAL_SERVER_ERROR
];
return responde()->json($RESPONSE);

}
}

/**
Expand All @@ -86,8 +187,12 @@ public function update(Request $request, Order $order)
* @param \App\Models\Order $order
* @return \Illuminate\Http\Response
*/
public function destroy(Order $order)
public function delete(Order $order)
{
//
try {
return response()->json($order->delete());
} catch (Exception $th) {
return response()->json(['message' => 'Failed to cancel your order'], 500);
}
}
}
6 changes: 3 additions & 3 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model;

class Order extends Model
{
Expand All @@ -15,7 +15,7 @@ class Order extends Model
'user_id',
'status',
'total',
'item_count',
// 'item_count',
'address',
'city',
'country',
Expand All @@ -29,7 +29,7 @@ public function user() {
}

public function items(){
return $this->hasMany(order_items::class);
return $this->hasMany(OrderItem::class);
}

}
26 changes: 0 additions & 26 deletions app/Models/order_items.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function up()
$table->unsignedBigInteger('user_id');
$table->enum('status',['pending','completed','decline'])->default('pending');
$table->decimal('total',20,6);
$table->unsignedBigInteger('item_count');
// $table->unsignedBigInteger('item_count');
$table->string('address');
$table->string('city');
$table->string('country');
Expand Down
13 changes: 13 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Controllers\RoleController;

use App\Http\Controllers\ProductController;
use App\Http\Controllers\OrderController;
use App\Http\Controllers\ProductCategoryController;
use App\Http\Controllers\ProductSubCategoryController;
use App\Http\Controllers\FileController;
Expand Down Expand Up @@ -103,3 +104,15 @@
});
});


Route::group(['prefix'=>'orders'],function(){
Route::get("/", [OrderController::class, "all"]);
Route::post("/", [OrderController::class,"create"]);
Route::get('/products/{order}', [OrderController::class, 'getProductsOrder']);
Route::group(["prefix"=> '{order}'], function(){
Route::get('',[OrderController::class, "show"]);
Route::put('',[OrderController::class, "update"]);
Route::delete('',[OrderController::class, "delete"]);
});

});

0 comments on commit e45c860

Please sign in to comment.