matches added

This commit is contained in:
2026-02-05 19:46:43 -05:00
parent 515ddb24d6
commit 9eb19b0b9a
2 changed files with 85 additions and 44 deletions

View File

@@ -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(),
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()
})
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(),
});

View File

@@ -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);
const {
data: { startTime, endTime, homeScore, awayScore },
} = parsed;
if (!parsed.success) {
return res.status(400).json({
error: "Invalid payload",
details: JSON.stringify(parsed.error),
});
}
try {
const [event] = await db.insert(matches).values({
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()
status: getMatchStatus(startTime, endTime),
})
.returning();
res.status(201).json({data: event})
res.status(201).json({ data: event });
} catch (e) {
return res.status(500).json({error: 'Failed to create match.', details: JSON.stringify(e)})
return res
.status(500)
.json({ error: "Failed to create match.", details: JSON.stringify(e) });
}
})
});