diff --git a/app/Concert.php b/app/Concert.php index 7c669fd..09f8207 100644 --- a/app/Concert.php +++ b/app/Concert.php @@ -28,4 +28,9 @@ class Concert extends Model { return number_format($this->ticket_price / 100, 2); } + + public function orders() + { + return $this->hasMany(Order::class); + } } diff --git a/app/Http/Controllers/ConcertOrdersController.php b/app/Http/Controllers/ConcertOrdersController.php index dfe4ae3..0cf2ce0 100644 --- a/app/Http/Controllers/ConcertOrdersController.php +++ b/app/Http/Controllers/ConcertOrdersController.php @@ -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); } } diff --git a/app/Order.php b/app/Order.php new file mode 100644 index 0000000..fdd6420 --- /dev/null +++ b/app/Order.php @@ -0,0 +1,15 @@ +hasMany(Ticket::class); + } +} diff --git a/app/Ticket.php b/app/Ticket.php new file mode 100644 index 0000000..02dcf30 --- /dev/null +++ b/app/Ticket.php @@ -0,0 +1,10 @@ +increments('id'); + $table->unsignedInteger('concert_id'); + $table->string('email'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('orders'); + } +} diff --git a/database/migrations/2016_11_08_210440_create_tickets_table.php b/database/migrations/2016_11_08_210440_create_tickets_table.php new file mode 100644 index 0000000..a4cc2ef --- /dev/null +++ b/database/migrations/2016_11_08_210440_create_tickets_table.php @@ -0,0 +1,32 @@ +increments('id'); + $table->unsignedInteger('order_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('tickets'); + } +} diff --git a/tests/features/PurchaseTicketsTest.php b/tests/features/PurchaseTicketsTest.php index 6e6b170..ef564a6 100644 --- a/tests/features/PurchaseTicketsTest.php +++ b/tests/features/PurchaseTicketsTest.php @@ -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()); } }