route.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import dbConnect from "../../lib/dbConnect";
  2. import ExchangeItem from "../../models/ExchangeItem";
  3. import { NextResponse } from "next/server";
  4. import { setCORSHeaders, handleError } from "../../lib/apiUtils";
  5. export async function GET(request) {
  6. await dbConnect();
  7. try {
  8. let exchangeItems;
  9. let message;
  10. exchangeItems = await ExchangeItem.find().sort({
  11. order: 1,
  12. });
  13. message =
  14. exchangeItems.length > 0
  15. ? "成功获取所有兑换项目"
  16. : "当前没有任何兑换项目";
  17. const response = NextResponse.json(
  18. {
  19. success: true,
  20. data: exchangeItems,
  21. message,
  22. total: exchangeItems.length,
  23. },
  24. { status: 200 }
  25. );
  26. return setCORSHeaders(response);
  27. } catch (error) {
  28. console.error("Error in GET /api/exchange-items:", error);
  29. return handleError(error);
  30. }
  31. }
  32. export async function POST(request) {
  33. await dbConnect();
  34. try {
  35. const { type, logo, title, points, order } = await request.json();
  36. const newExchangeItem = new ExchangeItem({
  37. type,
  38. logo,
  39. title,
  40. points,
  41. order,
  42. });
  43. await newExchangeItem.save();
  44. const response = NextResponse.json(
  45. {
  46. success: true,
  47. message: "兑换项目创建成功",
  48. data: newExchangeItem,
  49. },
  50. { status: 201 }
  51. );
  52. return setCORSHeaders(response);
  53. } catch (error) {
  54. console.error("创建兑换项目时出错:", error);
  55. return handleError(error);
  56. }
  57. }
  58. export async function PUT(request) {
  59. await dbConnect();
  60. try {
  61. const { id, ...updateData } = await request.json();
  62. console.log(id, updateData);
  63. const updatedExchangeItem = await ExchangeItem.findByIdAndUpdate(
  64. id,
  65. updateData,
  66. {
  67. new: true,
  68. runValidators: true,
  69. }
  70. );
  71. if (!updatedExchangeItem) {
  72. return NextResponse.json(
  73. { success: false, error: "未找到兑换项目" },
  74. { status: 404 }
  75. );
  76. }
  77. const response = NextResponse.json(
  78. { success: true, data: updatedExchangeItem },
  79. { status: 200 }
  80. );
  81. return setCORSHeaders(response);
  82. } catch (error) {
  83. return handleError(error);
  84. }
  85. }
  86. export async function DELETE(request) {
  87. await dbConnect();
  88. try {
  89. const url = new URL(request.url);
  90. const id = url.searchParams.get("id");
  91. if (!id) {
  92. return NextResponse.json(
  93. { success: false, error: "需要提供兑换项目ID" },
  94. { status: 400 }
  95. );
  96. }
  97. const deletedExchangeItem = await ExchangeItem.findByIdAndDelete(id);
  98. if (!deletedExchangeItem) {
  99. return NextResponse.json(
  100. { success: false, error: "未找到兑换项目" },
  101. { status: 404 }
  102. );
  103. }
  104. const response = NextResponse.json(
  105. { success: true, message: "兑换项目删除成功" },
  106. { status: 200 }
  107. );
  108. return setCORSHeaders(response);
  109. } catch (error) {
  110. return handleError(error);
  111. }
  112. }
  113. export async function OPTIONS() {
  114. const response = new NextResponse(null, { status: 204 });
  115. return setCORSHeaders(response);
  116. }