mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-03-03 19:59:58 +00:00
5.4 - Extracting a Named Constructor
This commit is contained in:
@@ -69,16 +69,7 @@ class Concert extends Model
|
|||||||
|
|
||||||
public function createOrder($email, $tickets)
|
public function createOrder($email, $tickets)
|
||||||
{
|
{
|
||||||
$order = Order::create([
|
return Order::forTickets($tickets, $email);
|
||||||
'email' => $email,
|
|
||||||
'amount' => $tickets->sum('price'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
foreach ($tickets as $ticket) {
|
|
||||||
$order->tickets()->save($ticket);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $order;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addTickets($quantity)
|
public function addTickets($quantity)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Order;
|
||||||
use App\Concert;
|
use App\Concert;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Billing\PaymentGateway;
|
use App\Billing\PaymentGateway;
|
||||||
@@ -36,7 +37,7 @@ class ConcertOrdersController extends Controller
|
|||||||
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
$this->paymentGateway->charge(request('ticket_quantity') * $concert->ticket_price, request('payment_token'));
|
||||||
|
|
||||||
// Create an order for those tickets
|
// Create an order for those tickets
|
||||||
$order = $concert->createOrder(request('email'), $tickets);
|
$order = Order::forTickets($tickets, request('email'));
|
||||||
|
|
||||||
return response()->json($order, 201);
|
return response()->json($order, 201);
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,20 @@ class Order extends Model
|
|||||||
{
|
{
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
|
public static function forTickets($tickets, $email)
|
||||||
|
{
|
||||||
|
$order = self::create([
|
||||||
|
'email' => $email,
|
||||||
|
'amount' => $tickets->sum('price'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($tickets as $ticket) {
|
||||||
|
$order->tickets()->save($ticket);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $order;
|
||||||
|
}
|
||||||
|
|
||||||
public function concert()
|
public function concert()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Concert::class);
|
return $this->belongsTo(Concert::class);
|
||||||
|
|||||||
@@ -10,6 +10,20 @@ class OrderTest extends TestCase
|
|||||||
{
|
{
|
||||||
use DatabaseMigrations;
|
use DatabaseMigrations;
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
function creating_an_order_from_tickets_and_email()
|
||||||
|
{
|
||||||
|
$concert = factory(Concert::class)->create(['ticket_price' => 1200])->addTickets(5);
|
||||||
|
$this->assertEquals(5, $concert->ticketsRemaining());
|
||||||
|
|
||||||
|
$order = Order::forTickets($concert->findTickets(3), 'john@example.com');
|
||||||
|
|
||||||
|
$this->assertEquals('john@example.com', $order->email);
|
||||||
|
$this->assertEquals(3, $order->ticketQuantity());
|
||||||
|
$this->assertEquals(3600, $order->amount);
|
||||||
|
$this->assertEquals(2, $concert->ticketsRemaining());
|
||||||
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
function converting_to_an_array()
|
function converting_to_an_array()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user