From c3c80722f51b2e8b946e47871567154c1a43b7d5 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Sun, 20 Nov 2016 18:50:04 -0500 Subject: [PATCH] 6.8 - Reserving Individual Tickets --- app/Concert.php | 5 +++++ app/Ticket.php | 6 ++++++ database/factories/ModelFactory.php | 9 +++++++++ .../2016_11_08_210440_create_tickets_table.php | 1 + tests/unit/ConcertTest.php | 12 ++++++++++++ tests/unit/TicketTest.php | 12 ++++++++++++ 6 files changed, 45 insertions(+) diff --git a/app/Concert.php b/app/Concert.php index 5108354..31041e7 100644 --- a/app/Concert.php +++ b/app/Concert.php @@ -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(); diff --git a/app/Ticket.php b/app/Ticket.php index 584872d..5e2f52b 100644 --- a/app/Ticket.php +++ b/app/Ticket.php @@ -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]); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index d6f1bd8..a5f1746 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -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; + }, + ]; +}); diff --git a/database/migrations/2016_11_08_210440_create_tickets_table.php b/database/migrations/2016_11_08_210440_create_tickets_table.php index 383c51f..a173b5e 100644 --- a/database/migrations/2016_11_08_210440_create_tickets_table.php +++ b/database/migrations/2016_11_08_210440_create_tickets_table.php @@ -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(); }); } diff --git a/tests/unit/ConcertTest.php b/tests/unit/ConcertTest.php index c3f3ccc..c717649 100644 --- a/tests/unit/ConcertTest.php +++ b/tests/unit/ConcertTest.php @@ -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()); + } } diff --git a/tests/unit/TicketTest.php b/tests/unit/TicketTest.php index 685de82..61350c7 100644 --- a/tests/unit/TicketTest.php +++ b/tests/unit/TicketTest.php @@ -1,5 +1,6 @@ create(); + $this->assertNull($ticket->reserved_at); + + $ticket->reserve(); + + $this->assertNotNull($ticket->fresh()->reserved_at); + } + /** @test */ function a_ticket_can_be_released() {