GuestRegistrationController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CheckIn;
  4. use Illuminate\Http\Request;
  5. use App\Models\GuestRegistration;
  6. class GuestRegistrationController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. //
  16. $guestRegistrations = GuestRegistration::all();
  17. return response()->json($guestRegistrations);
  18. }
  19. /**
  20. * Store a newly created resource in storage.
  21. *
  22. * @param \Illuminate\Http\Request $request
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function store(Request $request)
  26. {
  27. // 檢查是否已存在相同 user_id 的記錄
  28. $existingRecord = GuestRegistration::where('user_id', $request->user_id)->first();
  29. if ($existingRecord) {
  30. // 如果已存在,返回錯誤響應
  31. return response()->json(['error' => 'User ID already exists'], 409); // 409 衝突
  32. }
  33. // 如果不存在相同的 user_id,則創建新記錄
  34. $data = $request->all();
  35. $result = GuestRegistration::create($data);
  36. if ($result) {
  37. CheckIn::where('user_id', $result->user_id)->update(['is_checked_in' => true]);
  38. }
  39. return $result;
  40. }
  41. /**
  42. * Display the specified resource.
  43. *
  44. * @param int $id
  45. * @return \Illuminate\Http\Response
  46. */
  47. public function show($id)
  48. {
  49. $guestRegistration = GuestRegistration::find($id);
  50. if ($guestRegistration) {
  51. return response()->json($guestRegistration);
  52. } else {
  53. return response()->json(['error' => 'Resource not found'], 404);
  54. }
  55. }
  56. /**
  57. * Update the specified resource in storage.
  58. *
  59. * @param \Illuminate\Http\Request $request
  60. * @param int $id
  61. * @return \Illuminate\Http\Response
  62. */
  63. public function update(Request $request, $id)
  64. {
  65. $guestRegistration = GuestRegistration::find($id);
  66. if ($guestRegistration) {
  67. $guestRegistration->update($request->all());
  68. return response()->json($guestRegistration);
  69. } else {
  70. return response()->json(['error' => 'Resource not found'], 404);
  71. }
  72. }
  73. /**
  74. * Remove the specified resource from storage.
  75. *
  76. * @param int $id
  77. * @return \Illuminate\Http\Response
  78. */
  79. public function destroy($id)
  80. {
  81. $guestRegistration = GuestRegistration::find($id);
  82. if ($guestRegistration) {
  83. $guestRegistration->delete();
  84. return response()->json(['message' => 'Deleted successfully']);
  85. } else {
  86. return response()->json(['error' => 'Resource not found'], 404);
  87. }
  88. }
  89. }