mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
6.1 - Uncovering a New Domain Object
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Order;
|
||||
use App\Concert;
|
||||
use App\Reservation;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Billing\PaymentGateway;
|
||||
use App\Billing\PaymentFailedException;
|
||||
@@ -31,12 +32,13 @@ class ConcertOrdersController extends Controller
|
||||
try {
|
||||
// Find some tickets
|
||||
$tickets = $concert->findTickets(request('ticket_quantity'));
|
||||
$reservation = new Reservation($tickets);
|
||||
|
||||
// Charge the customer for the tickets
|
||||
$this->paymentGateway->charge($tickets->sum('price'), request('payment_token'));
|
||||
$this->paymentGateway->charge($reservation->totalCost(), request('payment_token'));
|
||||
|
||||
// Create an order for those tickets
|
||||
$order = Order::forTickets($tickets, request('email'), $tickets->sum('price'));
|
||||
$order = Order::forTickets($tickets, request('email'), $reservation->totalCost());
|
||||
|
||||
return response()->json($order, 201);
|
||||
|
||||
|
||||
18
app/Reservation.php
Normal file
18
app/Reservation.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
class Reservation
|
||||
{
|
||||
private $tickets;
|
||||
|
||||
public function __construct($tickets)
|
||||
{
|
||||
$this->tickets = $tickets;
|
||||
}
|
||||
|
||||
public function totalCost()
|
||||
{
|
||||
return $this->tickets->sum('price');
|
||||
}
|
||||
}
|
||||
23
tests/unit/ReservationTest.php
Normal file
23
tests/unit/ReservationTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use App\Concert;
|
||||
use App\Reservation;
|
||||
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class ReservationTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
/** @test */
|
||||
function calculating_the_total_cost()
|
||||
{
|
||||
$concert = factory(Concert::class)->create(['ticket_price' => 1200])->addTickets(3);
|
||||
$tickets = $concert->findTickets(3);
|
||||
|
||||
$reservation = new Reservation($tickets);
|
||||
|
||||
$this->assertEquals(3600, $reservation->totalCost());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user