72 lines
1.3 KiB
JavaScript
72 lines
1.3 KiB
JavaScript
|
|
const express = require('express');
|
||
|
|
const { prisma } = require('../utils/db');
|
||
|
|
|
||
|
|
const router = express.Router();
|
||
|
|
|
||
|
|
// GET /api/events - List all events
|
||
|
|
router.get('/', async (req, res, next) => {
|
||
|
|
try {
|
||
|
|
const events = await prisma.event.findMany({
|
||
|
|
orderBy: {
|
||
|
|
startDate: 'asc',
|
||
|
|
},
|
||
|
|
select: {
|
||
|
|
id: true,
|
||
|
|
name: true,
|
||
|
|
location: true,
|
||
|
|
startDate: true,
|
||
|
|
endDate: true,
|
||
|
|
worldsdcId: true,
|
||
|
|
participantsCount: true,
|
||
|
|
description: true,
|
||
|
|
createdAt: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
res.json({
|
||
|
|
success: true,
|
||
|
|
count: events.length,
|
||
|
|
data: events,
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
next(error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// GET /api/events/:id - Get event by ID
|
||
|
|
router.get('/:id', async (req, res, next) => {
|
||
|
|
try {
|
||
|
|
const { id } = req.params;
|
||
|
|
|
||
|
|
const event = await prisma.event.findUnique({
|
||
|
|
where: {
|
||
|
|
id: parseInt(id),
|
||
|
|
},
|
||
|
|
include: {
|
||
|
|
chatRooms: true,
|
||
|
|
_count: {
|
||
|
|
select: {
|
||
|
|
matches: true,
|
||
|
|
},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!event) {
|
||
|
|
return res.status(404).json({
|
||
|
|
success: false,
|
||
|
|
error: 'Event not found',
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
res.json({
|
||
|
|
success: true,
|
||
|
|
data: event,
|
||
|
|
});
|
||
|
|
} catch (error) {
|
||
|
|
next(error);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = router;
|