mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-28 00:58:43 +00:00
3.2 - Adding Tickets to Concerts
This commit is contained in:
@@ -34,14 +34,32 @@ class Concert extends Model
|
|||||||
return $this->hasMany(Order::class);
|
return $this->hasMany(Order::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function tickets()
|
||||||
|
{
|
||||||
|
return $this->hasMany(Ticket::class);
|
||||||
|
}
|
||||||
|
|
||||||
public function orderTickets($email, $ticketQuantity)
|
public function orderTickets($email, $ticketQuantity)
|
||||||
{
|
{
|
||||||
$order = $this->orders()->create(['email' => $email]);
|
$order = $this->orders()->create(['email' => $email]);
|
||||||
|
$tickets = $this->tickets()->take($ticketQuantity)->get();
|
||||||
|
|
||||||
foreach (range(1, $ticketQuantity) as $i) {
|
foreach ($tickets as $ticket) {
|
||||||
$order->tickets()->create([]);
|
$order->tickets()->save($ticket);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $order;
|
return $order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addTickets($quantity)
|
||||||
|
{
|
||||||
|
foreach (range(1, $quantity) as $i) {
|
||||||
|
$this->tickets()->create([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function ticketsRemaining()
|
||||||
|
{
|
||||||
|
return $this->tickets()->whereNull('order_id')->count();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ class CreateTicketsTable extends Migration
|
|||||||
{
|
{
|
||||||
Schema::create('tickets', function (Blueprint $table) {
|
Schema::create('tickets', function (Blueprint $table) {
|
||||||
$table->increments('id');
|
$table->increments('id');
|
||||||
$table->unsignedInteger('order_id');
|
$table->unsignedInteger('concert_id');
|
||||||
|
$table->unsignedInteger('order_id')->nullable();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,4 +64,24 @@ class ConcertTest extends TestCase
|
|||||||
$this->assertEquals('jane@example.com', $order->email);
|
$this->assertEquals('jane@example.com', $order->email);
|
||||||
$this->assertEquals(3, $order->tickets()->count());
|
$this->assertEquals(3, $order->tickets()->count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function can_add_tickets()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
|
$concert->addTickets(50);
|
||||||
|
|
||||||
|
$this->assertEquals(50, $concert->ticketsRemaining());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function tickets_remaining_does_not_include_tickets_associated_with_an_order()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create();
|
||||||
|
$concert->addTickets(50);
|
||||||
|
$concert->orderTickets('jane@example.com', 30);
|
||||||
|
|
||||||
|
$this->assertEquals(20, $concert->ticketsRemaining());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user