71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Receptionist;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class BranchReceptionistController extends Controller
|
|
{
|
|
/**
|
|
* Get all receptionists for a branch.
|
|
*/
|
|
public function show($branchId)
|
|
{
|
|
$receptionists = Receptionist::where('branch_id', $branchId)->get();
|
|
return response()->json($receptionists);
|
|
}
|
|
|
|
/**
|
|
* Create or update a receptionist.
|
|
*/
|
|
public function store(Request $request, $branchId)
|
|
{
|
|
$validated = $request->validate([
|
|
'id' => 'nullable|exists:receptionists,id',
|
|
'name' => 'required|string|max:255',
|
|
'email' => [
|
|
'required',
|
|
'email',
|
|
Rule::unique('receptionists')->ignore($request->id),
|
|
],
|
|
'password' => $request->id ? 'nullable|min:6|confirmed' : 'required|min:6|confirmed',
|
|
]);
|
|
|
|
if ($request->id) {
|
|
$receptionist = Receptionist::findOrFail($request->id);
|
|
$receptionist->name = $validated['name'];
|
|
$receptionist->email = $validated['email'];
|
|
if (!empty($validated['password'])) {
|
|
$receptionist->password = $validated['password'];
|
|
}
|
|
$receptionist->save();
|
|
return response()->json($receptionist);
|
|
} else {
|
|
$new = Receptionist::create([
|
|
'branch_id' => $branchId,
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'],
|
|
'password' => $validated['password'],
|
|
]);
|
|
return response()->json($new, 201);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a receptionist account.
|
|
*/
|
|
public function destroy($branchId, $receptionistId = null)
|
|
{
|
|
$id = $receptionistId ?: request()->receptionist_id;
|
|
|
|
if ($id) {
|
|
Receptionist::where('branch_id', $branchId)->where('id', $id)->delete();
|
|
}
|
|
|
|
return response()->json(['message' => 'Receptionist account deleted.']);
|
|
}
|
|
}
|