c1
This commit is contained in:
parent
4ea8fcc7ad
commit
67a18321f4
79
paragonik-backend/app/Http/Controllers/ReceiptController.php
Normal file
79
paragonik-backend/app/Http/Controllers/ReceiptController.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Receipt;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ReceiptController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return response()->json(Receipt::with('receipt_items')->get(), 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$receipt = Receipt::create([
|
||||
'payment_date' => $request->payment_date,
|
||||
'payment_amount' => $request->payment_amount,
|
||||
'user_id' => $request->user_id,
|
||||
'store_id' => $request->store_id,
|
||||
]);
|
||||
|
||||
return response()->json($receipt, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$receipt = Receipt::with('receipt_items')->get()->find($id);
|
||||
if(is_null($receipt)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
return response()->json($receipt, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$receipt = Receipt::find($id);
|
||||
if(is_null($receipt)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
$receipt->update($request->all());
|
||||
return response()->json($receipt, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$receipt = Receipt::find($id);
|
||||
if(is_null($receipt)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
$receipt->delete();
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\ReceiptItem;
|
||||
|
||||
class ReceiptItemController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return response()->json(ReceiptItem::get(), 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$receiptItem = ReceiptItem::create([
|
||||
'name' => $request->name,
|
||||
'price' => $request->price,
|
||||
'quantity' => $request->quantity,
|
||||
'receipt_id' => $request->receipt_id,
|
||||
'category_id' => $request->category_id,
|
||||
]);
|
||||
|
||||
return response()->json($receiptItem, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$receiptItem = ReceiptItem::find($id);
|
||||
if(is_null($receiptItem)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
return response()->json($receiptItem, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$receiptItem = ReceiptItem::find($id);
|
||||
if(is_null($receiptItem)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
$receiptItem->update($request->all());
|
||||
return response()->json($receiptItem, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$receiptItem = ReceiptItem::find($id);
|
||||
if(is_null($receiptItem)){ return response()->json(["message " => "Record not found."], 404);}
|
||||
$receiptItem->delete();
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
@ -6,6 +6,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Receipt extends Model
|
||||
{
|
||||
protected $fillable = ['id', 'payment_date', 'payment_amount', 'user_id', 'store_id', 'created_at', 'updated_at'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('App\User');
|
||||
|
@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
class ReceiptItem extends Model
|
||||
{
|
||||
protected $table = 'receipt_items';
|
||||
protected $fillable = ['id', 'name', 'price', 'quantity' , 'receipt_id', 'category_id', 'created_at', 'updated_at'];
|
||||
|
||||
public function receipts()
|
||||
{
|
||||
|
@ -36,4 +36,10 @@ class User extends Authenticatable
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
|
||||
public function receipts()
|
||||
{
|
||||
return $this->hasMany('App\Models\Receipt');
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ return [
|
||||
'driver' => 'eloquent',
|
||||
'model' => App\User::class,
|
||||
],
|
||||
|
||||
|
||||
// 'users' => [
|
||||
// 'driver' => 'database',
|
||||
// 'table' => 'users',
|
||||
|
@ -13,6 +13,11 @@ use Illuminate\Http\Request;
|
||||
|
|
||||
*/
|
||||
|
||||
#Route::apiResource('/receipt', 'ReceiptController');
|
||||
|
||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});
|
||||
|
||||
Route::apiResource('receiptItem', 'ReceiptItemController');
|
||||
Route::apiResource('receipt', 'ReceiptController');
|
Loading…
Reference in New Issue
Block a user