File manager - Edit - /var/www/html/spdc/api/app/Http/Services/ScheduleService.php
Back
<?php namespace App\Http\Services; use App\Models\BDT; use App\Models\Schedule; use App\Enums\ScheduleType; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use App\Enums\ScheduleAgentType; use Illuminate\Http\JsonResponse; use App\Classes\BaseServiceResponse; use App\Enums\ScheduleRequestStatus; use App\Http\Requests\ScheduleRequest; use App\Http\Resources\ScheduleResource; class ScheduleService extends BaseService { public function index(Request $request): BaseServiceResponse { $query = Schedule::query(); // if ($request->has('category')) { // $query = $query->where('category', strtoupper($request->category)); // } // if ($request->has('search_term')) { // $searchTerm = trim(strtolower($request->search_term)); // $query = $query->whereRaw("LOWER(model) LIKE ?", ['%' . $searchTerm . '%']); // } $schedules = $query->paginate(15); return $this->response( ScheduleResource::collection($schedules) ); } public function store(ScheduleRequest $request): BaseServiceResponse { $lastSchedule = Schedule::where('driver_id', $request->driver_id)->where('date', $request->date)->orderBy('created_at', 'desc')->first(); if (isset($lastSchedule)) { if ($lastSchedule->transportation_id != $request->transportation_id) { return $this->response( null, JsonResponse::HTTP_BAD_REQUEST, 'Este motorista já está alocado com o tranporte ' . $lastSchedule->transportation->model . ' neste dia e não pode ser alocado em outro tranposte.' ); } } $schedule = Schedule::create($request->all()); /** * tratando os tipos */ if ($schedule->type === ScheduleType::PADRAO->value) { $schedule->occurrencies()->attach($request->occurrencies); } elseif ($schedule->type === ScheduleType::EXTRA->value) { $schedule->extra()->create([ 'name' => $request->name ]); } /** * tratando agents */ foreach($request->agents as $agent) { if ($agent['type'] === ScheduleAgentType::INTERNO->value) { $schedule->agents()->create([ 'user_id' => $agent['id'], 'type' => $agent['type'] ]); } else { $schedule->agents()->create([ 'external_agent_id' => $agent['id'] ]); } } /** * criando a requisição */ $schedule->request()->create([ 'status' => ScheduleRequestStatus::PENDENTE->value, 'requester_id' => $request->user['id'], ]); /** * criando BDT */ $schedule->bdt()->create([ 'finished' => false, ]); $relationships = [ 'transportation', 'occurrencies', 'extra', 'agents', 'request', 'bdt' ]; return $this->response( new ScheduleResource($schedule->load($relationships)), JsonResponse::HTTP_CREATED ); } public function show(Schedule $schedule): BaseServiceResponse { $relationships = [ 'transportation', 'occurrencies.address', 'occurrencies.forms', 'occurrencies.fields', 'occurrencies.category', 'occurrencies.occurrencyTypes', 'occurrencies.inspection', 'occurrencies.execution', 'occurrencies.requester', 'extra', 'agents', 'request' ]; return $this->response( new ScheduleResource($schedule->load($relationships)), JsonResponse::HTTP_OK ); } public function update(ScheduleRequest $request, Schedule $schedule): BaseServiceResponse { /** * updating schedule info */ $schedule->fill($request->all()); $schedule->save(); /** * tratando os tipos */ if ($schedule->type === ScheduleType::PADRAO->value) { $schedule->occurrencies()->sync($request->occurrencies); } elseif ($schedule->type === ScheduleType::EXTRA->value) { $schedule->extra->fill([ 'name' => $request->name ])->save(); } /** * tratando agents */ foreach($request->agents as $agent) { $schedule->agents()->firstOrCreate([ 'user_id' => $agent ]); } $relationships = [ 'transportation', 'occurrencies', 'extra', 'agents', 'request' ]; return $this->response( new ScheduleResource($schedule->load($relationships)), JsonResponse::HTTP_CREATED ); } public function destroy(Schedule $schedule): BaseServiceResponse { $schedule->delete(); return $this->response( null, JsonResponse::HTTP_NO_CONTENT ); } public function destroyMany(Request $request): BaseServiceResponse { Schedule::destroy(treatArrayInput($request->schedules)); return $this->response( null, JsonResponse::HTTP_NO_CONTENT ); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.08 |
proxy
|
phpinfo
|
Settings