File manager - Edit - /var/www/html/pcadigital/api/app/Http/Services/HistoricService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Enums\OccurrenceStatusEnum; use App\Http\Requests\HistoricRequest; use App\Http\Resources\HistoricResource; use App\Models\Category; use App\Models\Historic; use App\Models\Occurrence; use App\Models\SubCategory; use Carbon\Carbon; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class HistoricService extends BaseService { private NotificationService $notificationService; public function __construct() { $this->notificationService = new NotificationService(); } public function index(): BaseServiceResponse { $historics = Historic::all(); return $this->success(HistoricResource::collection($historics)); } public function show(Historic $historic): BaseServiceResponse { return $this->success(['historic' => new HistoricResource($historic)]); } public function store(HistoricRequest $request): BaseServiceResponse { $data = $request->all(); $occurrence = Occurrence::find($data['occurrence_id']); $historic = new Historic(); $historic->occurrence()->associate($occurrence); $historic->observation = $data['observation'] ?? null; $historic->final_comment = $data['final_comment'] ?? null; $historic->created_by = $data['created_by']; $historic->status = $data['status']; $historic->is_send_to_citizen = $data['is_send_to_citizen']; if ($data['category_id']) { $category = Category::find($data['category_id']); $historic->category()->associate($category); } if ($data['sub_category_id']) { $subCategory = SubCategory::find($data['sub_category_id']); $historic->subCategory()->associate($subCategory); $historic->organ_name = $subCategory->organ_name; $historic->organ_id = $subCategory->organ_id; } $historicImages = []; if ($data['images'] > 0) { foreach ($data['images'] as $fileBase64) { $size_in_bytes = (int) (strlen(rtrim($fileBase64, '=')) * 3 / 4); $size_in_kb = $size_in_bytes / 1024; $size_in_mb = $size_in_kb / 1024; if ($size_in_mb > 10) { return $this->error( [ 'message' => __('validation.validation_error'), 'data' => 'Image cannot be size more than 10mb.', ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } $types = ['jpg', 'png', 'jpeg']; $extension = explode('/', explode(':', substr($fileBase64, 0, strpos($fileBase64, ';')))[1])[1]; if (in_array($extension, $types) == false) { return $this->error( [ 'message' => __('validation.validation_error'), 'data' => 'Only jpg, jpeg and png images can be saved.', ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR ); } $replace = substr($fileBase64, 0, strpos($fileBase64, ',') + 1); $file = str_replace($replace, '', $fileBase64); $file = str_replace(' ', '+', $file); $fileName = Carbon::now()->format('d-m-Y') . '_' . uniqid() . '.' . $extension; Storage::disk('public')->put($fileName, base64_decode($file), ['visibility' => 'public', 'directory_visibility' => 'public']); $historicImages[] = "/storage/" . $fileName; } } $historic->images_url = json_encode($historicImages); $historic->save(); $this->notificationService->createWhenHasNewHistoric($historic, $request->bearerToken()); return $this->success(['historic' => new HistoricResource($historic)]); } public function historicStatus(): BaseServiceResponse { return $this->success(['status' => OccurrenceStatusEnum::cases()]); } public function setSendToCitizen(Request $request): BaseServiceResponse { $data = $request->all(); $historic = Historic::find(['id' => $data['historic_id']])->first(); $historic->is_send_to_citizen = $data['status']; $historic->update(); return $this->success([], JsonResponse::HTTP_OK); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings