| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\CheckIn;
- use Illuminate\Http\Request;
- use App\Models\GuestRegistration;
- class GuestRegistrationController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- $guestRegistrations = GuestRegistration::all();
- return response()->json($guestRegistrations);
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- // 檢查是否已存在相同 user_id 的記錄
- $existingRecord = GuestRegistration::where('user_id', $request->user_id)->first();
- if ($existingRecord) {
- // 如果已存在,返回錯誤響應
- return response()->json(['error' => 'User ID already exists'], 409); // 409 衝突
- }
- // 如果不存在相同的 user_id,則創建新記錄
- $data = $request->all();
- $result = GuestRegistration::create($data);
- if ($result) {
- CheckIn::where('user_id', $result->user_id)->update(['is_checked_in' => true]);
- }
- return $result;
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $guestRegistration = GuestRegistration::find($id);
- if ($guestRegistration) {
- return response()->json($guestRegistration);
- } else {
- return response()->json(['error' => 'Resource not found'], 404);
- }
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $guestRegistration = GuestRegistration::find($id);
- if ($guestRegistration) {
- $guestRegistration->update($request->all());
- return response()->json($guestRegistration);
- } else {
- return response()->json(['error' => 'Resource not found'], 404);
- }
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $guestRegistration = GuestRegistration::find($id);
- if ($guestRegistration) {
- $guestRegistration->delete();
- return response()->json(['message' => 'Deleted successfully']);
- } else {
- return response()->json(['error' => 'Resource not found'], 404);
- }
- }
- }
|