128 lines
3.8 KiB
PHP
128 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
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
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id): JSONResponse
|
|
{
|
|
$profile = Profile::with('desaKelurahan')->where('user_id', Auth::id())->first();
|
|
return response()->json(new ProfileResource($profile));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id): JSONResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'nama' => ['required', 'string', 'max:255'],
|
|
'email' => ['required', 'email', 'max:255', 'unique:users,email,' . Auth::id()],
|
|
'telepon' => ['nullable', 'string'],
|
|
'alamat' => ['nullable', 'string'],
|
|
'kk' => ['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'],
|
|
]);
|
|
dd($validated);
|
|
$profile = Profile::where('user_id', Auth::id())->first();
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|