mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
2.5 - Adding Tickets to Orders
This commit is contained in:
@@ -28,4 +28,9 @@ class Concert extends Model
|
||||
{
|
||||
return number_format($this->ticket_price / 100, 2);
|
||||
}
|
||||
|
||||
public function orders()
|
||||
{
|
||||
return $this->hasMany(Order::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,13 @@ class ConcertOrdersController extends Controller
|
||||
$amount = $ticketQuantity * $concert->ticket_price;
|
||||
$token = request('payment_token');
|
||||
$this->paymentGateway->charge($amount, $token);
|
||||
|
||||
$order = $concert->orders()->create(['email' => request('email')]);
|
||||
|
||||
foreach (range(1, $ticketQuantity) as $i) {
|
||||
$order->tickets()->create([]);
|
||||
}
|
||||
|
||||
return response()->json([], 201);
|
||||
}
|
||||
}
|
||||
|
||||
15
app/Order.php
Normal file
15
app/Order.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Order extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function tickets()
|
||||
{
|
||||
return $this->hasMany(Ticket::class);
|
||||
}
|
||||
}
|
||||
10
app/Ticket.php
Normal file
10
app/Ticket.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Ticket extends Model
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateOrdersTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('orders', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->unsignedInteger('concert_id');
|
||||
$table->string('email');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('orders');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateTicketsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('tickets', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->unsignedInteger('order_id');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('tickets');
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,6 @@ class PurchaseTicketsTest extends TestCase
|
||||
// Make sure that an order exists for this customer
|
||||
$order = $concert->orders()->where('email', 'john@example.com')->first();
|
||||
$this->assertNotNull($order);
|
||||
$this->assertEquals(3, $order->tickets->count());
|
||||
$this->assertEquals(3, $order->tickets()->count());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user