route.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import dbConnect from "../../lib/dbConnect";
  2. import NewActivity from "../../models/NewActivities";
  3. import { NextResponse } from "next/server";
  4. import { setCORSHeaders, handleError } from "../../lib/apiUtils";
  5. import { withAuth } from "../../middleware/authMiddleware";
  6. export async function GET(request) {
  7. await dbConnect();
  8. console.log("GET request received");
  9. try {
  10. const isFromFrontend = request.headers.get("x-from-frontend") === "true";
  11. let activities;
  12. let message;
  13. if (isFromFrontend) {
  14. activities = await NewActivity.find({ isActive: true }).sort({
  15. order: 1,
  16. });
  17. message = activities.length > 0 ? "成功获取活跃活动" : "当前没有活跃活动";
  18. } else {
  19. // 其他请求,返回所有记录
  20. activities = await NewActivity.find().sort({ order: 1 });
  21. message = activities.length > 0 ? "成功获取所有活动" : "当前没有任何活动";
  22. }
  23. const response = NextResponse.json(
  24. {
  25. success: true,
  26. data: activities,
  27. message,
  28. total: activities.length,
  29. },
  30. { status: 200 }
  31. );
  32. return setCORSHeaders(response);
  33. } catch (error) {
  34. console.error("Error in GET /api/new-activities:", error);
  35. return handleError(error);
  36. }
  37. }
  38. export const POST = withAuth(async (request) => {
  39. await dbConnect();
  40. try {
  41. const { title, link, icon, order, isActive } = await request.json();
  42. const newActivity = new NewActivity({
  43. title,
  44. link,
  45. icon,
  46. order: order || 0,
  47. isActive: isActive !== undefined ? isActive : true,
  48. });
  49. await newActivity.save();
  50. const response = NextResponse.json(
  51. {
  52. success: true,
  53. message: "Activity created successfully",
  54. data: newActivity,
  55. },
  56. { status: 201 }
  57. );
  58. return setCORSHeaders(response);
  59. } catch (error) {
  60. console.error("Error creating activity:", error);
  61. return handleError(error);
  62. }
  63. });
  64. export const PUT = withAuth(async (request) => {
  65. await dbConnect();
  66. try {
  67. const { id, ...updateData } = await request.json();
  68. console.log(id, updateData);
  69. const updatedActivity = await NewActivity.findByIdAndUpdate(
  70. id,
  71. updateData,
  72. {
  73. new: true,
  74. runValidators: true,
  75. }
  76. );
  77. if (!updatedActivity) {
  78. return NextResponse.json(
  79. { success: false, error: "Activity not found" },
  80. { status: 404 }
  81. );
  82. }
  83. const response = NextResponse.json(
  84. { success: true, data: updatedActivity },
  85. { status: 200 }
  86. );
  87. return setCORSHeaders(response);
  88. } catch (error) {
  89. return handleError(error);
  90. }
  91. });
  92. export const DELETE = withAuth(async (request) => {
  93. await dbConnect();
  94. try {
  95. const url = new URL(request.url);
  96. const id = url.searchParams.get("id");
  97. if (!id) {
  98. return NextResponse.json(
  99. { success: false, error: "Activity ID is required" },
  100. { status: 400 }
  101. );
  102. }
  103. const deletedActivity = await NewActivity.findByIdAndDelete(id);
  104. if (!deletedActivity) {
  105. return NextResponse.json(
  106. { success: false, error: "Activity not found" },
  107. { status: 404 }
  108. );
  109. }
  110. const response = NextResponse.json(
  111. { success: true, message: "Activity deleted successfully" },
  112. { status: 200 }
  113. );
  114. return setCORSHeaders(response);
  115. } catch (error) {
  116. return handleError(error);
  117. }
  118. });
  119. export async function OPTIONS() {
  120. const response = new NextResponse(null, { status: 204 });
  121. return setCORSHeaders(response);
  122. }