Add more API resources; user bid management and tests #35 #36

This commit is contained in:
Nabeel Shahzad
2017-12-12 16:58:27 -06:00
parent 5b25a464ba
commit 248a8d1488
27 changed files with 470 additions and 166 deletions

View File

@@ -0,0 +1,79 @@
<?php
/**
* Created by IntelliJ IDEA.
* User: nshahzad
* Date: 12/12/17
* Time: 2:48 PM
*/
namespace App\Services;
use Log;
use App\Models\Flight;
use App\Models\User;
use App\Models\UserBid;
class FlightService extends BaseService
{
/**
* Allow a user to bid on a flight. Check settings and all that good stuff
* @param Flight $flight
* @param User $user
* @return UserBid
*/
public function addBid(Flight $flight, User $user): UserBid
{
# If it's already been bid on, then it can't be bid on again
if($flight->has_bid && setting('bids.disable_flight_on_bid')) {
Log.info($flight->id . ' already has a bid, skipping');
return null;
}
# See if we're allowed to have multiple bids or not
if(!setting('bids.allow_multiple_bids')) {
$user_bids = UserBid::where(['user_id' => $user->id])->first();
if($user_bids) {
Log.info('User "' . $user->id . '" already has bids, skipping');
return null;
}
}
# See if this user has this flight bid on already
$bid_data = [
'user_id' => $user->id,
'flight_id' => $flight->id
];
$user_bid = UserBid::where($bid_data)->first();
if($user_bid) {
return $user_bid;
}
$user_bid = UserBid::create($bid_data);
$flight->has_bid = true;
$flight->save();
return $user_bid;
}
/**
* Remove a bid from a given flight
* @param Flight $flight
* @param User $user
* @throws Exception
*/
public function removeBid(Flight $flight, User $user)
{
$user_bid = UserBid::where([
'flight_id' => $flight->id, 'user_id' => $user->id
])->first();
if($user_bid) {
$user_bid->forceDelete();
}
$flight->has_bid = false;
$flight->save();
}
}

View File

@@ -3,6 +3,7 @@
namespace App\Services;
use App\Events\UserRegistered;
use App\Facades\Utils;
use App\Models\User;
use App\Models\Rank;
use App\Models\Role;
@@ -15,32 +16,32 @@ use Illuminate\Support\Facades\Hash;
class UserService extends BaseService
{
public function adjustFlightCount(User &$pilot, int $count): User
public function adjustFlightCount(User $user, int $count): User
{
$pilot->refresh();
$pilot->flights = $pilot->flights + $count;
$pilot->save();
$user->refresh();
$user->flights += $count;
$user->save();
event(new UserStateChanged($pilot));
event(new UserStateChanged($user));
return $pilot;
return $user;
}
public function adjustFlightHours(User &$pilot, int $hours): User
public function adjustFlightHours(User $user, int $hours): User
{
$pilot->refresh();
$pilot->flight_time = $pilot->flight_time + $hours;
$pilot->save();
$user->refresh();
$user->flight_time += $hours;
$user->save();
event(new UserStateChanged($pilot));
event(new UserStateChanged($user));
return $pilot;
return $user;
}
public function calculatePilotRank(User &$pilot): User
public function calculatePilotRank(User $user): User
{
$pilot->refresh();
$pilot_hours = $pilot->flight_time / 3600;
$user->refresh();
$pilot_hours = $user->flight_time / 3600;
# TODO: Cache
$ranks = Cache::remember(
@@ -54,23 +55,28 @@ class UserService extends BaseService
if($rank->hours > $pilot_hours) {
break;
} else {
$pilot->rank_id = $rank->id;
$user->rank_id = $rank->id;
}
}
$pilot->save();
$user->save();
event(new UserStateChanged($pilot));
event(new UserStateChanged($user));
return $pilot;
return $user;
}
/**
* Register a pilot
* @param array $data
* @return mixed
*/
public function createPilot(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'apikey' => User::generateApiKey(),
'api_key' => Utils::generateApiKey(),
'airline_id' => $data['airline'],
'home_airport_id' => $data['home_airport'],
'curr_airport_id' => $data['home_airport'],