mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-02-25 17:57:16 +00:00
2.6 - Encapsulating Relationship Logic in the Model
This commit is contained in:
@@ -33,4 +33,15 @@ class Concert extends Model
|
|||||||
{
|
{
|
||||||
return $this->hasMany(Order::class);
|
return $this->hasMany(Order::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function orderTickets($email, $ticketQuantity)
|
||||||
|
{
|
||||||
|
$order = $this->orders()->create(['email' => $email]);
|
||||||
|
|
||||||
|
foreach (range(1, $ticketQuantity) as $i) {
|
||||||
|
$order->tickets()->create([]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $order;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,16 +18,8 @@ class ConcertOrdersController extends Controller
|
|||||||
public function store($concertId)
|
public function store($concertId)
|
||||||
{
|
{
|
||||||
$concert = Concert::find($concertId);
|
$concert = Concert::find($concertId);
|
||||||
$ticketQuantity = request('ticket_quantity');
|
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||||
$amount = $ticketQuantity * $concert->ticket_price;
|
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
||||||
$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);
|
return response()->json([], 201);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,4 +53,15 @@ class ConcertTest extends TestCase
|
|||||||
$this->assertTrue($publishedConcerts->contains($publishedConcertB));
|
$this->assertTrue($publishedConcerts->contains($publishedConcertB));
|
||||||
$this->assertFalse($publishedConcerts->contains($unpublishedConcert));
|
$this->assertFalse($publishedConcerts->contains($unpublishedConcert));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function can_order_concert_tickets()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create();
|
||||||
|
|
||||||
|
$order = $concert->orderTickets('jane@example.com', 3);
|
||||||
|
|
||||||
|
$this->assertEquals('jane@example.com', $order->email);
|
||||||
|
$this->assertEquals(3, $order->tickets()->count());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user