diff --git a/src/db/schema/matches.js b/src/db/schema/matches.js index 822fc4e..37dbf75 100644 --- a/src/db/schema/matches.js +++ b/src/db/schema/matches.js @@ -1,17 +1,27 @@ +import { + pgEnum, + serial, + integer, + pgTable, + text, + timestamp, +} from "drizzle-orm/pg-core"; -import { pgEnum, serial , integer, pgTable, text,timestamp,} from "drizzle-orm/pg-core"; +export const matchStatusEnum = pgEnum("match_status", [ + "scheduled", + "live", + "finished", +]); -export const matchStatusEnum = pgEnum('match_status',['scheduled','live', 'finished']) - -export const matches = pgTable('matches', { - id: serial('id').primaryKey(), - sport: text('sport').notNull(), - homeTeam: text('home_team').notNull(), - awayTeam: text("away_team").notNull(), - status: matchStatusEnum('status').notNull().default('scheduled'), - startTime: timestamp('start_time'), - endTime: timestamp('end_time'), - homeScore: integer('home_score').notNull().default(0), - awayScore: integer('away_score').notNull().default(0), - createdAt: timestamp('created_at').notNull().defaultNow() -}) \ No newline at end of file +export const matches = pgTable("matches", { + id: serial("id").primaryKey(), + sport: text("sport").notNull(), + homeTeam: text("home_team").notNull(), + awayTeam: text("away_team").notNull(), + status: matchStatusEnum("status").notNull().default("scheduled"), + startTime: timestamp("start_time"), + endTime: timestamp("end_time"), + homeScore: integer("home_score").notNull().default(0), + awayScore: integer("away_score").notNull().default(0), + createdAt: timestamp("created_at").notNull().defaultNow(), +}); diff --git a/src/routes/matches.route.js b/src/routes/matches.route.js index 59c10ba..7db1b1c 100644 --- a/src/routes/matches.route.js +++ b/src/routes/matches.route.js @@ -1,43 +1,74 @@ import { Router } from "express"; -import { createMatchSchema, listMatchesQuerySchema } from "../../validation/matches.js"; +import { + createMatchSchema, + listMatchesQuerySchema, +} from "../../validation/matches.js"; import { db } from "../db/db.js"; import { matches } from "../db/schema/matches.js"; import { getMatchStatus } from "../utils/match-status.utlis.js"; +import { desc } from "drizzle-orm"; -export const matchRouter = Router() +export const matchRouter = Router(); +const MAX_LIMIT = 100; -matchRouter.get('/', (_,res)=>{ - const parsed = listMatchesQuerySchema.safeParse(req.query) - if(!parsed.success){ - return res.status(400).json({error: 'Invalid payload', details: JSON.stringify(parsed.error)}) - } +matchRouter.get("/", async (req, res) => { + const parsed = listMatchesQuerySchema.safeParse(req.query); + if (!parsed.success) { + return res.status(400).json({ + error: "Invalid query", + details: JSON.stringify(parsed.error), + }); + } - return res.status(200).json({message: 'Matches List'}) -}) + const limit = Math.min(parsed.data.limit ?? 50, MAX_LIMIT); -matchRouter.post('/', async (req,res)=>{ - const parsed = createMatchSchema.safeParse(req.body) + try { + const data = await db + .select() + .from(matches) + .orderBy(desc(matches.createdAt)) + .limit(limit); - const {data: {startTime, endTime, homeScore, awayScore}} = parsed + return res.status(200).json({ data: data }); + } catch (e) { + return res + .status(500) + .json({ error: "Failed to list matchs.", details: JSON.stringify(e) }); + } +}); - if(!parsed.success){ - return res.status(400).json({error: 'Invalid payload', details: JSON.stringify(parsed.error)}) - } +matchRouter.post("/", async (req, res) => { + const parsed = createMatchSchema.safeParse(req.body); - try { - const [event] = await db.insert(matches).values({ - ...parsed.data, - startTime: new Date(startTime), - endTime: new Date(endTime), - homeScore: homeScore ?? 0, - awayScore: awayScore ?? 0, - status: getMatchStatus(startTime, endTime) - }).returning() + const { + data: { startTime, endTime, homeScore, awayScore }, + } = parsed; - res.status(201).json({data: event}) - } catch (e) { + if (!parsed.success) { + return res.status(400).json({ + error: "Invalid payload", + details: JSON.stringify(parsed.error), + }); + } - return res.status(500).json({error: 'Failed to create match.', details: JSON.stringify(e)}) - } -}) \ No newline at end of file + try { + const [event] = await db + .insert(matches) + .values({ + ...parsed.data, + startTime: new Date(startTime), + endTime: new Date(endTime), + homeScore: homeScore ?? 0, + awayScore: awayScore ?? 0, + status: getMatchStatus(startTime, endTime), + }) + .returning(); + + res.status(201).json({ data: event }); + } catch (e) { + return res + .status(500) + .json({ error: "Failed to create match.", details: JSON.stringify(e) }); + } +});