matches added
This commit is contained in:
@@ -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(),
|
||||||
export const matches = pgTable('matches', {
|
sport: text("sport").notNull(),
|
||||||
id: serial('id').primaryKey(),
|
homeTeam: text("home_team").notNull(),
|
||||||
sport: text('sport').notNull(),
|
|
||||||
homeTeam: text('home_team').notNull(),
|
|
||||||
awayTeam: text("away_team").notNull(),
|
awayTeam: text("away_team").notNull(),
|
||||||
status: matchStatusEnum('status').notNull().default('scheduled'),
|
status: matchStatusEnum("status").notNull().default("scheduled"),
|
||||||
startTime: timestamp('start_time'),
|
startTime: timestamp("start_time"),
|
||||||
endTime: timestamp('end_time'),
|
endTime: timestamp("end_time"),
|
||||||
homeScore: integer('home_score').notNull().default(0),
|
homeScore: integer("home_score").notNull().default(0),
|
||||||
awayScore: integer('away_score').notNull().default(0),
|
awayScore: integer("away_score").notNull().default(0),
|
||||||
createdAt: timestamp('created_at').notNull().defaultNow()
|
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||||
})
|
});
|
||||||
|
|||||||
@@ -1,43 +1,74 @@
|
|||||||
import { Router } from "express";
|
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 { db } from "../db/db.js";
|
||||||
import { matches } from "../db/schema/matches.js";
|
import { matches } from "../db/schema/matches.js";
|
||||||
import { getMatchStatus } from "../utils/match-status.utlis.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)=>{
|
matchRouter.get("/", async (req, res) => {
|
||||||
const parsed = listMatchesQuerySchema.safeParse(req.query)
|
const parsed = listMatchesQuerySchema.safeParse(req.query);
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
return res.status(400).json({error: 'Invalid payload', details: JSON.stringify(parsed.error)})
|
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)=>{
|
try {
|
||||||
const parsed = createMatchSchema.safeParse(req.body)
|
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) });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
matchRouter.post("/", async (req, res) => {
|
||||||
|
const parsed = createMatchSchema.safeParse(req.body);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: { startTime, endTime, homeScore, awayScore },
|
||||||
|
} = parsed;
|
||||||
|
|
||||||
if (!parsed.success) {
|
if (!parsed.success) {
|
||||||
return res.status(400).json({error: 'Invalid payload', details: JSON.stringify(parsed.error)})
|
return res.status(400).json({
|
||||||
|
error: "Invalid payload",
|
||||||
|
details: JSON.stringify(parsed.error),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [event] = await db.insert(matches).values({
|
const [event] = await db
|
||||||
|
.insert(matches)
|
||||||
|
.values({
|
||||||
...parsed.data,
|
...parsed.data,
|
||||||
startTime: new Date(startTime),
|
startTime: new Date(startTime),
|
||||||
endTime: new Date(endTime),
|
endTime: new Date(endTime),
|
||||||
homeScore: homeScore ?? 0,
|
homeScore: homeScore ?? 0,
|
||||||
awayScore: awayScore ?? 0,
|
awayScore: awayScore ?? 0,
|
||||||
status: getMatchStatus(startTime, endTime)
|
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)})
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
.returning();
|
||||||
|
|
||||||
|
res.status(201).json({ data: event });
|
||||||
|
} catch (e) {
|
||||||
|
return res
|
||||||
|
.status(500)
|
||||||
|
.json({ error: "Failed to create match.", details: JSON.stringify(e) });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user