Remove schedule and task repositories

This commit is contained in:
Lance Pioch 2024-03-16 19:20:55 -04:00
parent 1813e6f549
commit 60c32cdb81
7 changed files with 8 additions and 143 deletions

View File

@ -1,21 +0,0 @@
<?php
namespace App\Contracts\Repository;
use App\Models\Schedule;
use Illuminate\Support\Collection;
interface ScheduleRepositoryInterface extends RepositoryInterface
{
/**
* Return all the schedules for a given server.
*/
public function findServerSchedules(int $server): Collection;
/**
* Return a schedule model with all the associated tasks as a relationship.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getScheduleWithTasks(int $schedule): Schedule;
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Contracts\Repository;
use App\Models\Task;
interface TaskRepositoryInterface extends RepositoryInterface
{
/**
* Get a task and the server relationship for that task.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getTaskForJobProcess(int $id): Task;
/**
* Returns the next task in a schedule.
*/
public function getNextTask(int $schedule, int $index): ?Task;
}

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api\Client\Servers; namespace App\Http\Controllers\Api\Client\Servers;
use Carbon\Carbon; use Carbon\Carbon;
use Exception;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use App\Models\Server; use App\Models\Server;
@ -11,7 +12,6 @@ use Illuminate\Http\JsonResponse;
use App\Facades\Activity; use App\Facades\Activity;
use App\Helpers\Utilities; use App\Helpers\Utilities;
use App\Exceptions\DisplayException; use App\Exceptions\DisplayException;
use App\Repositories\Eloquent\ScheduleRepository;
use App\Services\Schedules\ProcessScheduleService; use App\Services\Schedules\ProcessScheduleService;
use App\Transformers\Api\Client\ScheduleTransformer; use App\Transformers\Api\Client\ScheduleTransformer;
use App\Http\Controllers\Api\Client\ClientApiController; use App\Http\Controllers\Api\Client\ClientApiController;
@ -27,7 +27,7 @@ class ScheduleController extends ClientApiController
/** /**
* ScheduleController constructor. * ScheduleController constructor.
*/ */
public function __construct(private ScheduleRepository $repository, private ProcessScheduleService $service) public function __construct(private ProcessScheduleService $service)
{ {
parent::__construct(); parent::__construct();
} }
@ -53,7 +53,7 @@ class ScheduleController extends ClientApiController
public function store(StoreScheduleRequest $request, Server $server): array public function store(StoreScheduleRequest $request, Server $server): array
{ {
/** @var \App\Models\Schedule $model */ /** @var \App\Models\Schedule $model */
$model = $this->repository->create([ $model = Schedule::query()->create([
'server_id' => $server->id, 'server_id' => $server->id,
'name' => $request->input('name'), 'name' => $request->input('name'),
'cron_day_of_week' => $request->input('day_of_week'), 'cron_day_of_week' => $request->input('day_of_week'),
@ -121,7 +121,7 @@ class ScheduleController extends ClientApiController
$data['is_processing'] = false; $data['is_processing'] = false;
} }
$this->repository->update($schedule->id, $data); $schedule->update($data);
Activity::event('server:schedule.update') Activity::event('server:schedule.update')
->subject($schedule) ->subject($schedule)
@ -153,7 +153,7 @@ class ScheduleController extends ClientApiController
*/ */
public function delete(DeleteScheduleRequest $request, Server $server, Schedule $schedule): JsonResponse public function delete(DeleteScheduleRequest $request, Server $server, Schedule $schedule): JsonResponse
{ {
$this->repository->delete($schedule->id); $schedule->delete();
Activity::event('server:schedule.delete')->subject($schedule)->property('name', $schedule->name)->log(); Activity::event('server:schedule.delete')->subject($schedule)->property('name', $schedule->name)->log();
@ -175,7 +175,7 @@ class ScheduleController extends ClientApiController
$request->input('month'), $request->input('month'),
$request->input('day_of_week') $request->input('day_of_week')
); );
} catch (\Exception $exception) { } catch (Exception) {
throw new DisplayException('The cron data provided does not evaluate to a valid expression.'); throw new DisplayException('The cron data provided does not evaluate to a valid expression.');
} }
} }

View File

@ -10,7 +10,6 @@ use Illuminate\Http\JsonResponse;
use App\Facades\Activity; use App\Facades\Activity;
use App\Models\Permission; use App\Models\Permission;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use App\Repositories\Eloquent\TaskRepository;
use App\Exceptions\Http\HttpForbiddenException; use App\Exceptions\Http\HttpForbiddenException;
use App\Transformers\Api\Client\TaskTransformer; use App\Transformers\Api\Client\TaskTransformer;
use App\Http\Requests\Api\Client\ClientApiRequest; use App\Http\Requests\Api\Client\ClientApiRequest;
@ -26,7 +25,6 @@ class ScheduleTaskController extends ClientApiController
*/ */
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private TaskRepository $repository
) { ) {
parent::__construct(); parent::__construct();
} }
@ -72,7 +70,7 @@ class ScheduleTaskController extends ClientApiController
$sequenceId = $requestSequenceId; $sequenceId = $requestSequenceId;
} }
return $this->repository->create([ return Task::query()->create([
'schedule_id' => $schedule->id, 'schedule_id' => $schedule->id,
'sequence_id' => $sequenceId, 'sequence_id' => $sequenceId,
'action' => $request->input('action'), 'action' => $request->input('action'),
@ -128,7 +126,7 @@ class ScheduleTaskController extends ClientApiController
->decrement('sequence_id'); ->decrement('sequence_id');
} }
$this->repository->update($task->id, [ $task->update([
'sequence_id' => $sequenceId, 'sequence_id' => $sequenceId,
'action' => $request->input('action'), 'action' => $request->input('action'),
'payload' => $request->input('payload') ?? '', 'payload' => $request->input('payload') ?? '',

View File

@ -5,25 +5,21 @@ namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use App\Repositories\Eloquent\EggRepository; use App\Repositories\Eloquent\EggRepository;
use App\Repositories\Eloquent\NodeRepository; use App\Repositories\Eloquent\NodeRepository;
use App\Repositories\Eloquent\TaskRepository;
use App\Repositories\Eloquent\ApiKeyRepository; use App\Repositories\Eloquent\ApiKeyRepository;
use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SessionRepository;
use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\SubuserRepository;
use App\Repositories\Eloquent\DatabaseRepository; use App\Repositories\Eloquent\DatabaseRepository;
use App\Repositories\Eloquent\ScheduleRepository;
use App\Repositories\Eloquent\SettingsRepository; use App\Repositories\Eloquent\SettingsRepository;
use App\Repositories\Eloquent\AllocationRepository; use App\Repositories\Eloquent\AllocationRepository;
use App\Contracts\Repository\EggRepositoryInterface; use App\Contracts\Repository\EggRepositoryInterface;
use App\Repositories\Eloquent\EggVariableRepository; use App\Repositories\Eloquent\EggVariableRepository;
use App\Contracts\Repository\NodeRepositoryInterface; use App\Contracts\Repository\NodeRepositoryInterface;
use App\Contracts\Repository\TaskRepositoryInterface;
use App\Repositories\Eloquent\DatabaseHostRepository; use App\Repositories\Eloquent\DatabaseHostRepository;
use App\Contracts\Repository\ApiKeyRepositoryInterface; use App\Contracts\Repository\ApiKeyRepositoryInterface;
use App\Repositories\Eloquent\ServerVariableRepository; use App\Repositories\Eloquent\ServerVariableRepository;
use App\Contracts\Repository\SessionRepositoryInterface; use App\Contracts\Repository\SessionRepositoryInterface;
use App\Contracts\Repository\SubuserRepositoryInterface; use App\Contracts\Repository\SubuserRepositoryInterface;
use App\Contracts\Repository\DatabaseRepositoryInterface; use App\Contracts\Repository\DatabaseRepositoryInterface;
use App\Contracts\Repository\ScheduleRepositoryInterface;
use App\Contracts\Repository\SettingsRepositoryInterface; use App\Contracts\Repository\SettingsRepositoryInterface;
use App\Contracts\Repository\AllocationRepositoryInterface; use App\Contracts\Repository\AllocationRepositoryInterface;
use App\Contracts\Repository\EggVariableRepositoryInterface; use App\Contracts\Repository\EggVariableRepositoryInterface;
@ -45,11 +41,9 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(EggRepositoryInterface::class, EggRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::class);
$this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class); $this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class);
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class); $this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
$this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class);
$this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class); $this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class);
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::class); $this->app->bind(SessionRepositoryInterface::class, SessionRepository::class);
$this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class);
$this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class); $this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class);
$this->app->bind(TaskRepositoryInterface::class, TaskRepository::class);
} }
} }

View File

@ -1,42 +0,0 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Schedule;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Exceptions\Repository\RecordNotFoundException;
use App\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleRepository extends EloquentRepository implements ScheduleRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Schedule::class;
}
/**
* Return all the schedules for a given server.
*/
public function findServerSchedules(int $server): Collection
{
return $this->getBuilder()->withCount('tasks')->where('server_id', '=', $server)->get($this->getColumns());
}
/**
* Return a schedule model with all the associated tasks as a relationship.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getScheduleWithTasks(int $schedule): Schedule
{
try {
return $this->getBuilder()->with('tasks')->findOrFail($schedule, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
}

View File

@ -1,44 +0,0 @@
<?php
namespace App\Repositories\Eloquent;
use App\Models\Task;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Contracts\Repository\TaskRepositoryInterface;
use App\Exceptions\Repository\RecordNotFoundException;
class TaskRepository extends EloquentRepository implements TaskRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Task::class;
}
/**
* Get a task and the server relationship for that task.
*
* @throws \App\Exceptions\Repository\RecordNotFoundException
*/
public function getTaskForJobProcess(int $id): Task
{
try {
return $this->getBuilder()->with('server.user', 'schedule')->findOrFail($id, $this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Returns the next task in a schedule.
*/
public function getNextTask(int $schedule, int $index): ?Task
{
return $this->getBuilder()->where('schedule_id', '=', $schedule)
->orderBy('sequence_id')
->where('sequence_id', '>', $index)
->first($this->getColumns());
}
}