|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | + |
| 3 | +export async function GET( |
| 4 | + request: Request, |
| 5 | + { params }: { params: { id: string } }, |
| 6 | +) { |
| 7 | + try { |
| 8 | + // Get the authorization header from the incoming request |
| 9 | + const authHeader = request.headers.get('authorization'); |
| 10 | + |
| 11 | + const headers: HeadersInit = { |
| 12 | + 'Content-Type': 'application/json', |
| 13 | + }; |
| 14 | + |
| 15 | + // Add the authorization header if it exists |
| 16 | + if (authHeader) { |
| 17 | + headers['Authorization'] = authHeader; |
| 18 | + } |
| 19 | + |
| 20 | + const response = await fetch( |
| 21 | + `${process.env.NEXT_PUBLIC_API_URL}/api/orders/${params.id}`, |
| 22 | + { |
| 23 | + method: 'GET', |
| 24 | + headers, |
| 25 | + }, |
| 26 | + ); |
| 27 | + |
| 28 | + const data = await response.json(); |
| 29 | + return NextResponse.json(data); |
| 30 | + } catch (error) { |
| 31 | + return NextResponse.json( |
| 32 | + { error: 'Failed to fetch order' }, |
| 33 | + { status: 500 }, |
| 34 | + ); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export async function PUT( |
| 39 | + request: Request, |
| 40 | + { params }: { params: { id: string } }, |
| 41 | +) { |
| 42 | + try { |
| 43 | + const body = await request.json(); |
| 44 | + |
| 45 | + // Get the authorization header from the incoming request |
| 46 | + const authHeader = request.headers.get('authorization'); |
| 47 | + |
| 48 | + const headers: HeadersInit = { |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + }; |
| 51 | + |
| 52 | + // Add the authorization header if it exists |
| 53 | + if (authHeader) { |
| 54 | + headers['Authorization'] = authHeader; |
| 55 | + } |
| 56 | + |
| 57 | + const response = await fetch( |
| 58 | + `${process.env.NEXT_PUBLIC_API_URL}/api/orders/${params.id}`, |
| 59 | + { |
| 60 | + method: 'PUT', |
| 61 | + headers, |
| 62 | + body: JSON.stringify(body), |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + const data = await response.json(); |
| 67 | + |
| 68 | + if (!response.ok) { |
| 69 | + return NextResponse.json( |
| 70 | + { error: 'Failed to update order', details: data }, |
| 71 | + { status: response.status }, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + return NextResponse.json(data); |
| 76 | + } catch (error) { |
| 77 | + return NextResponse.json( |
| 78 | + { error: 'Failed to update order' }, |
| 79 | + { status: 500 }, |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export async function DELETE( |
| 85 | + request: Request, |
| 86 | + { params }: { params: { id: string } }, |
| 87 | +) { |
| 88 | + try { |
| 89 | + // Get the authorization header from the incoming request |
| 90 | + const authHeader = request.headers.get('authorization'); |
| 91 | + |
| 92 | + const headers: HeadersInit = { |
| 93 | + 'Content-Type': 'application/json', |
| 94 | + }; |
| 95 | + |
| 96 | + // Add the authorization header if it exists |
| 97 | + if (authHeader) { |
| 98 | + headers['Authorization'] = authHeader; |
| 99 | + } |
| 100 | + |
| 101 | + const response = await fetch( |
| 102 | + `${process.env.NEXT_PUBLIC_API_URL}/api/orders/${params.id}`, |
| 103 | + { |
| 104 | + method: 'DELETE', |
| 105 | + headers, |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + if (!response.ok) { |
| 110 | + const data = await response.json(); |
| 111 | + return NextResponse.json( |
| 112 | + { error: 'Failed to delete order', details: data }, |
| 113 | + { status: response.status }, |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return NextResponse.json( |
| 118 | + { message: 'Order deleted successfully' }, |
| 119 | + { status: 200 }, |
| 120 | + ); |
| 121 | + } catch (error) { |
| 122 | + return NextResponse.json( |
| 123 | + { error: 'Failed to delete order' }, |
| 124 | + { status: 500 }, |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments