| 37 | } |
| 38 | |
| 39 | export async function PATCH(req: NextRequest) { |
| 40 | const { searchParams } = new URL(req.url) |
| 41 | const id = searchParams.get('id') |
| 42 | const index = searchParams.get('index') |
| 43 | if (!id || !index || !(parseInt(index) >= 0 && parseInt(index) < 4)) { |
| 44 | return new Response('Missing id or index', { status: 400 }) |
| 45 | } |
| 46 | |
| 47 | const key = getKey(id) |
| 48 | |
| 49 | const { success } = await ratelimit.limit(key + `_${req.ip ?? ''}`) |
| 50 | if (!success) { |
| 51 | return new Response('Too Many Requests', { |
| 52 | status: 429, |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | let current = await redis.get<number[]>(key) |
| 57 | if (!current) { |
| 58 | current = [0, 0, 0, 0] |
| 59 | } |
| 60 | // increment the array value at the index |
| 61 | current[parseInt(index)] += 1 |
| 62 | |
| 63 | await redis.set(key, current) |
| 64 | |
| 65 | revalidateTag(key) |
| 66 | |
| 67 | return NextResponse.json({ |
| 68 | data: current, |
| 69 | }) |
| 70 | } |