2.5 - Adding Tickets to Orders

This commit is contained in:
Adam Wathan
2016-11-08 16:39:09 -05:00
parent 46640c5096
commit 25cc370adf
7 changed files with 103 additions and 1 deletions

View File

@@ -28,4 +28,9 @@ class Concert extends Model
{
return number_format($this->ticket_price / 100, 2);
}
public function orders()
{
return $this->hasMany(Order::class);
}
}

View File

@@ -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
View 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
View File

@@ -0,0 +1,10 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ticket extends Model
{
//
}

View File

@@ -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');
}
}

View File

@@ -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');
}
}

View File

@@ -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());
}
}