6.8 - Reserving Individual Tickets

This commit is contained in:
Adam Wathan
2016-11-20 18:50:04 -05:00
parent 2b21f07aa2
commit c3c80722f5
6 changed files with 45 additions and 0 deletions

View File

@@ -56,6 +56,11 @@ class Concert extends Model
return $this->createOrder($email, $tickets);
}
public function reserveTickets($quantity)
{
return $this->findTickets($quantity);
}
public function findTickets($quantity)
{
$tickets = $this->tickets()->available()->take($quantity)->get();

View File

@@ -2,6 +2,7 @@
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
class Ticket extends Model
@@ -13,6 +14,11 @@ class Ticket extends Model
return $query->whereNull('order_id');
}
public function reserve()
{
$this->update(['reserved_at' => Carbon::now()]);
}
public function release()
{
$this->update(['order_id' => null]);

View File

@@ -51,3 +51,12 @@ $factory->state(App\Concert::class, 'unpublished', function ($faker) {
'published_at' => null,
];
});
$factory->define(App\Ticket::class, function (Faker\Generator $faker) {
return [
'concert_id' => function () {
return factory(App\Concert::class)->create()->id;
},
];
});

View File

@@ -17,6 +17,7 @@ class CreateTicketsTable extends Migration
$table->increments('id');
$table->unsignedInteger('concert_id');
$table->unsignedInteger('order_id')->nullable();
$table->datetime('reserved_at')->nullable();
$table->timestamps();
});
}

View File

@@ -117,4 +117,16 @@ class ConcertTest extends TestCase
$this->fail("Order succeeded even though there were not enough tickets remaining.");
}
/** @test */
function can_reserve_available_tickets()
{
$concert = factory(Concert::class)->create()->addTickets(3);
$this->assertEquals(3, $concert->ticketsRemaining());
$reservedTickets = $concert->reserveTickets(2);
$this->assertCount(2, $reservedTickets);
$this->assertEquals(1, $concert->ticketsRemaining());
}
}

View File

@@ -1,5 +1,6 @@
<?php
use App\Ticket;
use App\Concert;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
@@ -9,6 +10,17 @@ class TicketTest extends TestCase
{
use DatabaseMigrations;
/** @test */
function a_ticket_can_be_reserved()
{
$ticket = factory(Ticket::class)->create();
$this->assertNull($ticket->reserved_at);
$ticket->reserve();
$this->assertNotNull($ticket->fresh()->reserved_at);
}
/** @test */
function a_ticket_can_be_released()
{