Skip to content

Commit

Permalink
feat: add events endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanzitting committed Jan 11, 2022
1 parent 64d2647 commit aee8136
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import fastifyTypeorm from 'fastify-typeorm-plugin';

import { dbConfig } from './database';
import { jobs } from './routes/jobs';
import { events } from './routes/events';

export function build(opts: FastifyServerOptions = {}): FastifyInstance {
const app = fastify(opts);

app.register(fastifyTypeorm, dbConfig);

app.register(jobs, { prefix: '/jobs' });
app.register(events, { prefix: '/events'});

return app;
}
29 changes: 29 additions & 0 deletions src/routes/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { FastifyPluginAsync } from 'fastify';
import type { FromSchema } from 'json-schema-to-ts';
import { Event } from '../entities/Event';

const findEventParams = {
type: 'object',
properties: {
eventId: { type: 'number' },
},
required: ['eventId'],
} as const;

export const events: FastifyPluginAsync = async (app) => {
app.get('/', async (req, reply) => {
const events = await app.orm.manager.find(Event);
return events;
});

app.route<{ Params: FromSchema<typeof findEventParams> }>({
url: '/:eventId',
method: 'GET',
schema: { params: findEventParams },
handler: async (req, reply) => {
const eventId = req.params.eventId;
const event = await app.orm.manager.findOne(Event,eventId);
return event;
},
});
};

0 comments on commit aee8136

Please sign in to comment.