mirror of
https://github.com/anna-sara/lan_kiosk
synced 2025-10-26 21:17:13 +01:00
Backend: Purchase
This commit is contained in:
parent
182bdedde1
commit
a015871a92
3 changed files with 137 additions and 0 deletions
90
app/Http/Controllers/PurchaseController.php
Normal file
90
app/Http/Controllers/PurchaseController.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Purchase;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Customer;
|
||||
|
||||
class PurchaseController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$purchases = Purchase::get();
|
||||
return $purchases->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'customer_id' => 'required',
|
||||
'amount' => 'nullable',
|
||||
]);
|
||||
|
||||
Purchase::create([
|
||||
'customer_id' => $data['customer_id'],
|
||||
'amount' => $data['amount'],
|
||||
]);
|
||||
|
||||
$customer = Customer::findOrFail( $data['customer_id'] );
|
||||
$customer->amount_left = $customer->amount_left - $data['amount'];
|
||||
$customer->save();
|
||||
|
||||
return redirect('customer/' . $customer->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Purchase $purchase)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Purchase $purchase)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Purchase $purchase)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$purchase = Purchase::findOrFail( $id );
|
||||
$customer = Customer::where('id', $purchase->customer_id);
|
||||
$customer->amount_left = $customer->amount_left + $purchase->amount;
|
||||
$customer->amount_used = $customer->amount_used - $purchase->amount;
|
||||
$customer->save();
|
||||
$purchase->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true, 'message' => 'Customer deleted successfully'
|
||||
]);
|
||||
}
|
||||
}
|
||||
18
app/Models/Purchase.php
Normal file
18
app/Models/Purchase.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Purchase extends Model
|
||||
{
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'customer_id',
|
||||
'amount',
|
||||
];
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('purchases', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('customer_id')->nullable();
|
||||
$table->integer('amount')->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('purchases');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue