update api scramble telescope

This commit is contained in:
2026-03-17 09:58:11 +07:00
parent 3a9f3bd813
commit 106283903e
21 changed files with 797 additions and 61 deletions
+54 -7
View File
@@ -6,6 +6,11 @@ use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Profile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\JsonResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use App\Http\Resources\ProfileResource;
class ProfileController extends Controller
{
@@ -36,10 +41,10 @@ class ProfileController extends Controller
/**
* Display the specified resource.
*/
public function show()
public function show(string $id): JSONResponse
{
$profile = Profile::where('user_id', Auth::id())->first();
return response()->json($profile);
$profile = Profile::with('desaKelurahan')->where('user_id', Auth::id())->first();
return response()->json(new ProfileResource($profile));
}
/**
@@ -53,19 +58,44 @@ class ProfileController extends Controller
/**
* Update the specified resource in storage.
*/
public function update(Request $request)
public function update(Request $request, string $id): JSONResponse
{
$validated = $request->validate([
'nama' => ['required', 'string', 'max:255'],
'email' => ['required', 'email'],
'email' => ['required', 'email', 'max:255', 'unique:users,email,' . Auth::id()],
'telepon' => ['nullable', 'string'],
'alamat' => ['nullable', 'string'],
'kk' => ['nullable', 'string'],
'ktp' => ['nullable', 'string']
'ktp' => ['nullable', 'string'],
'file_kk' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:2048'],
'file_ktp' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:2048'],
'desa_kelurahan_id' => ['nullable', 'exists:master_desa_kelurahan,id'],
]);
$profile = Profile::where('user_id', Auth::id())->first();
$profile->update($validated);
if ($request->hasFile('file_kk')) {
$validated['file_kk'] = $request->file('file_kk')->getClientOriginalName();
$kkPath = $request->file('file_kk')->store('uploads/profile/' . $profile->id, 'public');
$validated['path_kk'] = $kkPath;
}
if ($request->hasFile('file_ktp')) {
$validated['file_ktp'] = $request->file('file_ktp')->getClientOriginalName();
$ktpPath = $request->file('file_ktp')->store('uploads/profile/' . $profile->id, 'public');
$validated['path_ktp'] = $ktpPath;
}
$validated['updated_by'] = Auth::id();
$user = Auth::user();
DB::transaction(function () use ($profile, $user, $validated) {
$profile->update($validated);
$user->update([
'name' => $validated['nama'],
'email' => $validated['email'],
'updated_by' => $user->id,
]);
});
return response()->json($profile);
}
@@ -77,4 +107,21 @@ class ProfileController extends Controller
{
//
}
public function downloadFile(string $id, string $type): JsonResponse | BinaryFileResponse
{
$profile = Profile::where('user_id', Auth::id())->first();
if (!$profile) {
return response()->json(['message' => 'Profile tidak ditemukan'], 404);
}
if ($type === 'kk' && $profile->path_kk) {
return response()->file(storage_path('app/public/' . $profile->path_kk));
} elseif ($type === 'ktp' && $profile->path_ktp) {
return response()->file(storage_path('app/public/' . $profile->path_ktp));
} else {
return response()->json(['message' => 'File tidak ditemukan'], 404);
}
}
}