|
| 1 | +import Koa from "koa"; |
| 2 | +import Router from "koa-router"; |
| 3 | +import bodyParser from "koa-bodyparser"; |
| 4 | +import { getTeam } from "../helpers/tba"; |
| 5 | +import { addListing, getListings, deleteListing } from "../helpers/trading"; |
| 6 | +import { addAPIHeaders } from "../helpers/utils"; |
| 7 | + |
| 8 | +const router = new Router<Koa.DefaultState, Koa.Context>(); |
| 9 | + |
| 10 | +router.get("/team/:number", async (ctx) => { |
| 11 | + addAPIHeaders(ctx); |
| 12 | + try { |
| 13 | + const teamNumber = parseInt(ctx.params.number); |
| 14 | + if (!teamNumber || teamNumber < 1) { |
| 15 | + ctx.body = { success: false, error: "Invalid team number" }; |
| 16 | + return; |
| 17 | + } |
| 18 | + const team = await getTeam(teamNumber); |
| 19 | + if (team && (team as any).nickname) { |
| 20 | + ctx.body = { |
| 21 | + success: true, |
| 22 | + body: { |
| 23 | + team: teamNumber, |
| 24 | + teamName: (team as any).nickname |
| 25 | + } |
| 26 | + }; |
| 27 | + } else { |
| 28 | + ctx.body = { success: false, error: "Team not found" }; |
| 29 | + } |
| 30 | + } catch (err) { |
| 31 | + ctx.body = { success: false, error: "Could not look up team" }; |
| 32 | + } |
| 33 | +}); |
| 34 | + |
| 35 | +router.get("/listings", async (ctx) => { |
| 36 | + addAPIHeaders(ctx); |
| 37 | + try { |
| 38 | + const query = ctx.query as any; |
| 39 | + const listings = await getListings({ |
| 40 | + type: query.type, |
| 41 | + category: query.category, |
| 42 | + event: query.event |
| 43 | + }); |
| 44 | + ctx.body = { |
| 45 | + success: true, |
| 46 | + body: { listings } |
| 47 | + }; |
| 48 | + } catch (err) { |
| 49 | + ctx.body = { |
| 50 | + success: false, |
| 51 | + error: "Could not fetch listings" |
| 52 | + }; |
| 53 | + } |
| 54 | +}); |
| 55 | + |
| 56 | +router.post("/listings", bodyParser(), async (ctx) => { |
| 57 | + addAPIHeaders(ctx); |
| 58 | + try { |
| 59 | + const body = ctx.request.body as any; |
| 60 | + |
| 61 | + if ( |
| 62 | + !body.type || |
| 63 | + !body.team || |
| 64 | + !body.item || |
| 65 | + !body.category || |
| 66 | + !body.contact |
| 67 | + ) { |
| 68 | + ctx.body = { |
| 69 | + success: false, |
| 70 | + error: "Missing required fields: type, team, item, category, contact" |
| 71 | + }; |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (!["offer", "request"].includes(body.type)) { |
| 76 | + ctx.body = { |
| 77 | + success: false, |
| 78 | + error: "Type must be 'offer' or 'request'" |
| 79 | + }; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const listing = await addListing({ |
| 84 | + type: body.type, |
| 85 | + team: parseInt(body.team), |
| 86 | + teamName: body.teamName || "", |
| 87 | + item: body.item, |
| 88 | + category: body.category, |
| 89 | + quantity: parseInt(body.quantity) || 1, |
| 90 | + description: body.description || "", |
| 91 | + contact: body.contact, |
| 92 | + event: body.event || "general" |
| 93 | + }); |
| 94 | + |
| 95 | + ctx.body = { |
| 96 | + success: true, |
| 97 | + body: { |
| 98 | + listing |
| 99 | + } |
| 100 | + }; |
| 101 | + } catch (err) { |
| 102 | + ctx.body = { |
| 103 | + success: false, |
| 104 | + error: "Could not create listing" |
| 105 | + }; |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +router.delete("/listings/:id", bodyParser(), async (ctx) => { |
| 110 | + addAPIHeaders(ctx); |
| 111 | + try { |
| 112 | + const body = ctx.request.body as any; |
| 113 | + if (!body.team) { |
| 114 | + ctx.body = { |
| 115 | + success: false, |
| 116 | + error: "Team number required" |
| 117 | + }; |
| 118 | + return; |
| 119 | + } |
| 120 | + await deleteListing(ctx.params.id, parseInt(body.team)); |
| 121 | + ctx.body = { success: true, body: { message: "Listing deleted" } }; |
| 122 | + } catch (err) { |
| 123 | + ctx.body = { |
| 124 | + success: false, |
| 125 | + error: err.message || "Could not delete listing" |
| 126 | + }; |
| 127 | + } |
| 128 | +}); |
| 129 | + |
| 130 | +export default router; |
0 commit comments