mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 11:14:06 +00:00
3.3 - Refusing Orders When There Are No More Tickets
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Exceptions\NotEnoughTicketsException;
|
||||
|
||||
class Concert extends Model
|
||||
{
|
||||
@@ -41,8 +42,13 @@ class Concert extends Model
|
||||
|
||||
public function orderTickets($email, $ticketQuantity)
|
||||
{
|
||||
$tickets = $this->tickets()->available()->take($ticketQuantity)->get();
|
||||
|
||||
if ($tickets->count() < $ticketQuantity) {
|
||||
throw new NotEnoughTicketsException;
|
||||
}
|
||||
|
||||
$order = $this->orders()->create(['email' => $email]);
|
||||
$tickets = $this->tickets()->take($ticketQuantity)->get();
|
||||
|
||||
foreach ($tickets as $ticket) {
|
||||
$order->tickets()->save($ticket);
|
||||
@@ -60,6 +66,6 @@ class Concert extends Model
|
||||
|
||||
public function ticketsRemaining()
|
||||
{
|
||||
return $this->tickets()->whereNull('order_id')->count();
|
||||
return $this->tickets()->available()->count();
|
||||
}
|
||||
}
|
||||
|
||||
5
app/Exceptions/NotEnoughTicketsException.php
Normal file
5
app/Exceptions/NotEnoughTicketsException.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
class NotEnoughTicketsException extends \RuntimeException {}
|
||||
@@ -6,5 +6,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Ticket extends Model
|
||||
{
|
||||
//
|
||||
public function scopeAvailable($query)
|
||||
{
|
||||
return $query->whereNull('order_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user