Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BossOfGames committed Jan 20, 2024
0 parents commit b6e02cc
Show file tree
Hide file tree
Showing 16 changed files with 647 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Provide any configuration items here
*/

return [
'name' => 'CHFreeFlight'
];
87 changes: 87 additions & 0 deletions Http/Controllers/Admin/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Modules\CHFreeFlight\Http\Controllers\Admin;

use App\Contracts\Controller;
use Illuminate\Http\Request;

/**
* Admin controller
*/
class AdminController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return mixed
*/
public function index(Request $request)
{
return view('chfreeflight::admin.index');
}

/**
* Show the form for creating a new resource.
*
* @param Request $request
*
* @return mixed
*/
public function create(Request $request)
{
return view('chfreeflight::admin.create');
}

/**
* Store a newly created resource in storage.
*
* @param Request $request
*/
public function store(Request $request)
{
}

/**
* Show the form for editing the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function edit(Request $request)
{
return view('chfreeflight::admin.edit');
}

/**
* Show the specified resource.
*
* @param Request $request
*
* @return mixed
*/
public function show(Request $request)
{
return view('chfreeflight::admin.show');
}

/**
* Update the specified resource in storage.
*
* @param Request $request
*/
public function update(Request $request)
{
}

/**
* Remove the specified resource from storage.
*
* @param Request $request
*/
public function destroy(Request $request)
{
}
}
74 changes: 74 additions & 0 deletions Http/Controllers/Frontend/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Modules\CHFreeFlight\Http\Controllers\Frontend;

use App\Contracts\Controller;
use App\Models\Enums\PirepFieldSource;
use App\Models\Flight;
use App\Repositories\AirlineRepository;
use App\Services\BidService;
use App\Services\FlightService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Modules\CHFreeFlight\Providers\CHFreeFlightProvider;

/**
* Class $CLASS$
* @package
*/
class IndexController extends Controller
{
public function __construct(public AirlineRepository $airlineRepository,
public FlightService $flightService,
public BidService $bidService)
{
}

/**
* Display a listing of the resource.
*
* @param Request $request
*
* @return mixed
*/
public function create(Request $request)
{
return view('chfreeflight::create_flight', [
'airline_list' => $this->airlineRepository->selectBoxList(true)
]);
}

/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return mixed
*/
public function store(Request $request)
{
$fields = $request->all();

// Add the owner of the flight
$fields['owner_type'] = CHFreeFlightProvider::class;
$fields['user_id'] = Auth::user()->id;
$fields['visible'] = false;
$fields['active'] = true;
$fields['minutes'] = 0;
$fields['hours'] = 0;
// Create the flight
try {
$flight = $this->flightService->createFlight($fields);
} catch (\Exception $exception) {
//dd($exception);
Session::flash('error', $exception->getMessage());
return to_route('chfreeflight.index');
}

// Add the Bid to the User Account
$this->bidService->addBid($flight, Auth::user());

return to_route('frontend.flights.bids');
}
}
4 changes: 4 additions & 0 deletions Http/Routes/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

# This is the admin path. Comment this out if you don't have an admin panel component.
Route::get('/', 'AdminController@index');
17 changes: 17 additions & 0 deletions Http/Routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This is publicly accessible
*/
Route::group(['middleware' => []], function() {
Route::get('/', 'ApiController@index');
});

/**
* This is required to have a valid API key
*/
Route::group(['middleware' => [
'api.auth'
]], function() {
Route::get('/hello', 'ApiController@hello');
});
10 changes: 10 additions & 0 deletions Http/Routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

/*
* To register a route that needs to be authentication, wrap it in a
* Route::group() with the auth middleware
*/
Route::group(['middleware' => 'auth'], function() {
Route::get('/', 'IndexController@create')->name('create');
Route::post('/', 'IndexController@store')->name('store');
});
58 changes: 58 additions & 0 deletions Listeners/DeleteFlights.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Modules\CHFreeFlight\Listeners;

use App\Contracts\Listener;
use App\Models\Bid;
use App\Models\Enums\FlightType;
use App\Models\Enums\PirepState;
use App\Models\Flight;
use App\Models\Pirep;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Modules\CHFreeFlight\Providers\CHFreeFlightProvider;

/**
* Class DeleteFlights
* @package Modules\CHFreeFlight\Listeners
*/
class DeleteFlights extends Listener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
// Find all the flights
$flights = Flight::where('owner_type', CHFreeFlightProvider::class)->get();

// We're going to only delete flights that don't have a bid, or a pirep that's completed.

foreach ($flights as $flight) {
// if Pirep is in progress, then don't do anything.
$pirep = Pirep::where(['flight_id' => $flight->id, 'user_id' => $flight->user_id])->first();
if ($pirep->state == PirepState::IN_PROGRESS) {
continue;
}

// Check if there's a bid.
$bids = Bid::where('flight_id', $flight->id)->count();
if ($bids == 0) {
$flight->delete();
}
}
}
}
93 changes: 93 additions & 0 deletions Providers/CHFreeFlightProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Modules\CHFreeFlight\Providers;

use App\Contracts\Modules\ServiceProvider;

/**
* @package $NAMESPACE$
*/
class CHFreeFlightProvider extends ServiceProvider
{
private $moduleSvc;

protected $defer = false;

/**
* Boot the application events.
*/
public function boot(): void
{
$this->moduleSvc = app('App\Services\ModuleService');

$this->registerTranslations();
$this->registerConfig();
$this->registerViews();

$this->registerLinks();

// Uncomment this if you have migrations
// $this->loadMigrationsFrom(__DIR__ . '/../$MIGRATIONS_PATH$');
}

/**
* Register the service provider.
*/
public function register()
{
//
}

/**
* Add module links here
*/
public function registerLinks(): void
{
// Show this link if logged in
$this->moduleSvc->addFrontendLink('Free Flight', '/chfreeflight', '', $logged_in=true);

// Admin links:
//$this->moduleSvc->addAdminLink('CHFreeFlight', '/admin/chfreeflight');
}

/**
* Register config.
*/
protected function registerConfig()
{
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('chfreeflight.php'),
], 'chfreeflight');

$this->mergeConfigFrom(__DIR__.'/../Config/config.php', 'chfreeflight');
}

/**
* Register views.
*/
public function registerViews()
{
$viewPath = resource_path('views/modules/chfreeflight');
$sourcePath = __DIR__.'/../Resources/views';

$this->publishes([$sourcePath => $viewPath],'views');

$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/chfreeflight';
}, \Config::get('view.paths')), [$sourcePath]), 'chfreeflight');
}

/**
* Register translations.
*/
public function registerTranslations()
{
$langPath = resource_path('lang/modules/chfreeflight');

if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, 'chfreeflight');
} else {
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'chfreeflight');
}
}
}
25 changes: 25 additions & 0 deletions Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Modules\CHFreeFlight\Providers;

use App\Events\CronNightly;
use Modules\CHFreeFlight\Listeners\DeleteFlights;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*/
protected $listen = [
CronNightly::class => [DeleteFlights::class],
];

/**
* Register any events for your application.
*/
public function boot()
{
parent::boot();
}
}
Loading

0 comments on commit b6e02cc

Please sign in to comment.