123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- import dbConnect from "../../lib/dbConnect";
- import ExchangeItem from "../../models/ExchangeItem";
- import { NextResponse } from "next/server";
- import { setCORSHeaders, handleError } from "../../lib/apiUtils";
- export async function GET(request) {
- await dbConnect();
- try {
- let exchangeItems;
- let message;
- exchangeItems = await ExchangeItem.find().sort({
- order: 1,
- });
- message =
- exchangeItems.length > 0
- ? "成功获取所有兑换项目"
- : "当前没有任何兑换项目";
- const response = NextResponse.json(
- {
- success: true,
- data: exchangeItems,
- message,
- total: exchangeItems.length,
- },
- { status: 200 }
- );
- return setCORSHeaders(response);
- } catch (error) {
- console.error("Error in GET /api/exchange-items:", error);
- return handleError(error);
- }
- }
- export async function POST(request) {
- await dbConnect();
- try {
- const { type, logo, title, points, order } = await request.json();
- const newExchangeItem = new ExchangeItem({
- type,
- logo,
- title,
- points,
- order,
- });
- await newExchangeItem.save();
- const response = NextResponse.json(
- {
- success: true,
- message: "兑换项目创建成功",
- data: newExchangeItem,
- },
- { status: 201 }
- );
- return setCORSHeaders(response);
- } catch (error) {
- console.error("创建兑换项目时出错:", error);
- return handleError(error);
- }
- }
- export async function PUT(request) {
- await dbConnect();
- try {
- const { id, ...updateData } = await request.json();
- console.log(id, updateData);
- const updatedExchangeItem = await ExchangeItem.findByIdAndUpdate(
- id,
- updateData,
- {
- new: true,
- runValidators: true,
- }
- );
- if (!updatedExchangeItem) {
- return NextResponse.json(
- { success: false, error: "未找到兑换项目" },
- { status: 404 }
- );
- }
- const response = NextResponse.json(
- { success: true, data: updatedExchangeItem },
- { status: 200 }
- );
- return setCORSHeaders(response);
- } catch (error) {
- return handleError(error);
- }
- }
- export async function DELETE(request) {
- await dbConnect();
- try {
- const url = new URL(request.url);
- const id = url.searchParams.get("id");
- if (!id) {
- return NextResponse.json(
- { success: false, error: "需要提供兑换项目ID" },
- { status: 400 }
- );
- }
- const deletedExchangeItem = await ExchangeItem.findByIdAndDelete(id);
- if (!deletedExchangeItem) {
- return NextResponse.json(
- { success: false, error: "未找到兑换项目" },
- { status: 404 }
- );
- }
- const response = NextResponse.json(
- { success: true, message: "兑换项目删除成功" },
- { status: 200 }
- );
- return setCORSHeaders(response);
- } catch (error) {
- return handleError(error);
- }
- }
- export async function OPTIONS() {
- const response = new NextResponse(null, { status: 204 });
- return setCORSHeaders(response);
- }
|