update api
This commit is contained in:
@@ -6,6 +6,10 @@ use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Profile;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
@@ -33,6 +37,46 @@ class AuthController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function signup(Request $request): JSONResponse
|
||||
{
|
||||
$credentials = $request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email'],
|
||||
'password' => ['required', 'string', 'min:8', 'confirmed'],
|
||||
'role' => ['string'],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $credentials['name'],
|
||||
'email' => $credentials['email'],
|
||||
'password' => Hash::make($credentials['password']),
|
||||
'role' => $credentials['role'],
|
||||
]);
|
||||
|
||||
$profile = Profile::create([
|
||||
'nama' => $credentials['name'],
|
||||
'email' => $credentials['email'],
|
||||
'role' => $credentials['role'],
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
if (!Auth::attempt($credentials)) {
|
||||
return response()->json([
|
||||
'message' => 'Email dan/atau password tidak sesuai'
|
||||
], 401);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
$user->tokens()->delete();
|
||||
|
||||
$token = $user->createToken('mobile-token')->plainTextToken;
|
||||
|
||||
return response()->json([
|
||||
'access_token' => $token,
|
||||
'token_type' => 'Bearer',
|
||||
]);
|
||||
}
|
||||
|
||||
public function me(Request $request): JSONResponse
|
||||
{
|
||||
return response()->json($request->user());
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
use App\Models\Map\Lahan;
|
||||
|
||||
class LahanController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
$listLahan = Lahan::where('profile_id', $profile->id)->get();
|
||||
|
||||
return response()->json($listLahan);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
$validated = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['required', 'string'],
|
||||
'desa_kelurahan_id' => ['numeric'],
|
||||
'path' => ['string'],
|
||||
'luas_lahan' => ['numeric'],
|
||||
]);
|
||||
|
||||
$lahan = Lahan::create([
|
||||
'nama' => $validated['nama'],
|
||||
'status_kepemilikan' => $validated['status_kepemilikan'],
|
||||
'profile_id' => $profile->id,
|
||||
'desa_kelurahan_id' => $validated['desa_kelurahan_id'] ?? null,
|
||||
'path' => $validated['path'] ?? null,
|
||||
'luas_lahan' => $validated['luas_lahan'] ?? null,
|
||||
]);
|
||||
|
||||
return response()->json($lahan, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
|
||||
return response()->json($lahan);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
|
||||
$validated = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'status_kepemilikan' => ['required', 'string'],
|
||||
'desa_kelurahan_id' => ['numeric'],
|
||||
'path' => ['string'],
|
||||
'luas_lahan' => ['numeric'],
|
||||
]);
|
||||
|
||||
$lahan->update($validated);
|
||||
|
||||
return response()->json($lahan);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
|
||||
$lahan = Lahan::findOrFail($id);
|
||||
$lahan->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
|
||||
public function batchUpsert(Request $request): JsonResponse
|
||||
{
|
||||
Gate::authorize('petani');
|
||||
$user = Auth::user();
|
||||
$profile = $user->profile;
|
||||
|
||||
$validated = $request->validate([
|
||||
'lahans' => ['required', 'array'],
|
||||
'lahans.*.id' => ['numeric'],
|
||||
'lahans.*.nama' => ['required', 'string', 'max:255'],
|
||||
'lahans.*.status_kepemilikan' => ['required', 'numeric'],
|
||||
'lahans.*.desa_kelurahan_id' => ['numeric'],
|
||||
'lahans.*.path' => ['string'],
|
||||
'lahans.*.luas_lahan' => ['numeric'],
|
||||
]);
|
||||
|
||||
$lahans = [];
|
||||
foreach ($validated['lahans'] as $lahan) {
|
||||
$lahans[] = [
|
||||
'id' => $lahan['id'] ?? null,
|
||||
'nama' => $lahan['nama'],
|
||||
'status_kepemilikan' => $lahan['status_kepemilikan'],
|
||||
'profile_id' => $profile->id,
|
||||
'desa_kelurahan_id' => $lahan['desa_kelurahan_id'] ?? null,
|
||||
'path' => $lahan['path'] ?? null,
|
||||
'luas_lahan' => $lahan['luas_lahan'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
Lahan::upsert($lahans, ['id'], ['nama', 'status_kepemilikan', 'profile_id', 'desa_kelurahan_id', 'path', 'luas_lahan']);
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Profile;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
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()
|
||||
{
|
||||
$profile = Profile::where('user_id', Auth::id())->first();
|
||||
return response()->json($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)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'nama' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'email'],
|
||||
'telepon' => ['nullable', 'string'],
|
||||
'alamat' => ['nullable', 'string'],
|
||||
]);
|
||||
|
||||
$profile = Profile::where('user_id', Auth::id())->first();
|
||||
$profile->update($validated);
|
||||
|
||||
return response()->json($profile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user