mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +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);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
$concert = Concert::find($concertId);
|
||||
$ticketQuantity = request('ticket_quantity');
|
||||
$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([]);
|
||||
}
|
||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||
$order = $concert->orderTickets(request('email'), request('ticket_quantity'));
|
||||
|
||||
return response()->json([], 201);
|
||||
}
|
||||
|
||||
@@ -53,4 +53,15 @@ class ConcertTest extends TestCase
|
||||
$this->assertTrue($publishedConcerts->contains($publishedConcertB));
|
||||
$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