Skip to content

Commit

Permalink
Added event types to api.
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-maddock committed Dec 24, 2023
1 parent bb928cf commit 72291ad
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 8 deletions.
17 changes: 17 additions & 0 deletions app/Filters/EventTypeFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Filters;

use Illuminate\Database\Eloquent\Builder;

class EventTypeFilters extends QueryFilter
{
public function name(?string $value = null): Builder
{
if (isset($value)) {
return $this->builder->where('name', $value);
} else {
return $this->builder;
}
}
}
228 changes: 228 additions & 0 deletions app/Http/Controllers/Api/EventTypesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php

namespace App\Http\Controllers\Api;

use App\Filters\EventTypeFilters;
use App\Models\Activity;
use App\Http\Controllers\Controller;
use App\Http\Requests\EventTypeRequest;
use App\Http\ResultBuilder\ListEntityResultBuilder;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Models\EventType;
use App\Services\SessionStore\ListParameterSessionStore;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;

class EventTypesController extends Controller
{
protected int $defaultLimit;

protected string $defaultSort;

protected string $defaultSortDirection;

protected array $defaultSortCriteria;

protected int $limit;

protected string $sort;

protected string $sortDirection;

protected array $filters;

protected bool $hasFilter;

protected string $prefix;

protected EventTypeFilters $filter;

public function __construct(EventTypeFilters $filter)
{
// TODO Handle API auth
// $this->middleware('auth', ['except' => ['index', 'show']]);
$this->filter = $filter;

// prefix for session storage
$this->prefix = 'app.event-types.';

// default list variables
$this->defaultLimit = 10;
$this->defaultSort = 'name';
$this->defaultSortDirection = 'asc';
$this->defaultSortCriteria = ['eventTypes.name' => 'asc'];

$this->limit = $this->defaultLimit;
$this->sort = $this->defaultSort;
$this->sortDirection = $this->defaultSortDirection;

$this->hasFilter = false;
parent::__construct();
}

/**
* Display a listing of the resource.
*
*
* @throws \Throwable
*/
public function index(
Request $request,
ListParameterSessionStore $listParamSessionStore,
ListEntityResultBuilder $listEntityResultBuilder
): JsonResponse {
// initialized listParamSessionStore with baseindex key
$listParamSessionStore->setBaseIndex('internal_event_type');
$listParamSessionStore->setKeyPrefix('internal_event_type_index');

// set the index tab in the session
$listParamSessionStore->setIndexTab(action([EventTypesController::class, 'index']));

// create the base query including any required joins; needs select to make sure only event entities are returned
$baseQuery = EventType::query()->select('event_types.*');

$listEntityResultBuilder
->setFilter($this->filter)
->setQueryBuilder($baseQuery)
->setDefaultSort(['event_types.name' => 'asc']);

// get the result set from the builder
$listResultSet = $listEntityResultBuilder->listResultSetFactory();

// get the query builder
$query = $listResultSet->getList();

// get the entities
$eventTypes = $query
->paginate($listResultSet->getLimit());

return response()->json($eventTypes);
}


/**
* Reset the rpp, sort, order
*
* @throws \Throwable
*/
public function rppReset(
Request $request,
ListParameterSessionStore $listParamSessionStore
): RedirectResponse {
// set the rpp, sort, direction only to default values
$keyPrefix = $request->get('key') ?? 'internal_event_type_index';
$listParamSessionStore->setBaseIndex('internal_event_type');
$listParamSessionStore->setKeyPrefix($keyPrefix);

// clear
$listParamSessionStore->clearSort();

return redirect()->route('event-types.index');
}

/**
* Reset the filtering of entities.
*
* @return Response
*
* @throws \Throwable
*/
public function reset(
Request $request,
ListParameterSessionStore $listParamSessionStore
): Response {
// set filters and list controls to default values
$keyPrefix = $request->get('key') ?? 'internal_event_type_index';
$listParamSessionStore->setBaseIndex('internal_event_type');
$listParamSessionStore->setKeyPrefix($keyPrefix);

// clear
$listParamSessionStore->clearFilter();
$listParamSessionStore->clearSort();

return redirect()->route('event-types.index');
}

/**
* Builds the criteria from the session.
*/
public function buildCriteria(Request $request): Builder
{
// base criteria
$query = EventType::orderBy($this->sort, $this->sortDirection);

return $query;
}

/**
* Store a newly created resource in storage.
*
*/
public function store(EventTypeRequest $request): JsonResponse
{
$input = $request->all();

$eventType = EventType::create($input);

return response()->json($eventType);
}

/**
* Display the specified resource.
*/
public function show(EventType $eventType): JsonResponse
{
return response()->json($eventType);
}


/**
* Update the specified resource in storage.
*
*/
public function update(EventType $eventType, EventTypeRequest $request): JsonResponse
{
$eventType->fill($request->input())->save();

return response()->json($eventType);
}

/**
* Remove the specified resource from storage.
*/
public function destroy(EventType $eventType): JsonResponse
{
$name = $eventType->name;

try {
$eventType->delete();
} catch (Exception $e) {
Log::error(sprintf('Could not delete the event type %s', $name));
};

// add to activity log
Activity::log($eventType, $this->user, 3);

return response()->json([], 204);
}

protected function getFilterOptions(): array
{
return [
];
}

protected function getListControlOptions(): array
{
return [
'limitOptions' => [5 => 5, 10 => 10, 25 => 25, 100 => 100, 1000 => 1000],
'sortOptions' => ['event_types.name' => 'Name', 'event_types.created_at' => 'Created At'],
'directionOptions' => ['asc' => 'asc', 'desc' => 'desc']
];
}
}
9 changes: 9 additions & 0 deletions app/Http/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,15 @@ public function store(EventRequest $request, Event $event, ImageHandler $imageHa
$event->tags()->attach($syncArray);
$event->entities()->attach($request->input('entity_list'));

// also attach the venue and promoter if set
if ($request->input('venue_id')) {
$event->entities()->attach($request->input('venue_id'));
}

if ($request->input('promoter_id')) {
$event->entities()->attach($request->input('promoter_id'));
}

// add to activity log
Activity::log($event, $this->user, 1);

Expand Down
6 changes: 3 additions & 3 deletions app/Http/Requests/EntityTypeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function authorize()
public function rules()
{
return [
'name' => 'required|min:3',
'slug' => 'required',
'short' => 'required',
'name' => 'required|min:3:max:255',
'slug' => 'required|min:3|max:255',
'short' => 'required|min:3|max:255',
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Requests/EventTypeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Requests;

class EventTypeRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|min:3|max:255',
];
}
}
Loading

0 comments on commit 72291ad

Please sign in to comment.