Add JSON Endpoint for events
This commit is contained in:
parent
3996e37682
commit
96f04c6de4
|
@ -4,12 +4,15 @@
|
||||||
import express, {Request, Response} from 'express';
|
import express, {Request, Response} from 'express';
|
||||||
import {Guid} from 'guid-typescript';
|
import {Guid} from 'guid-typescript';
|
||||||
import logger from '../../middleware/logger';
|
import logger from '../../middleware/logger';
|
||||||
|
import {eventsRouter} from './events/events.router';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Router Definition
|
* Router Definition
|
||||||
*/
|
*/
|
||||||
export const calendarRouter = express.Router();
|
export const calendarRouter = express.Router();
|
||||||
|
|
||||||
|
calendarRouter.use('/events', eventsRouter);
|
||||||
|
|
||||||
|
|
||||||
calendarRouter.get('/', async (req: Request, res: Response) => {
|
calendarRouter.get('/', async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
export interface Event {
|
||||||
|
event_id: number;
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
/**
|
||||||
|
* Required External Modules and Interfaces
|
||||||
|
*/
|
||||||
|
|
||||||
|
import express, {Request, Response} from 'express';
|
||||||
|
import * as EventService from './events.service';
|
||||||
|
import {Event} from './event.interface';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Router Definition
|
||||||
|
*/
|
||||||
|
|
||||||
|
export const eventsRouter = express.Router();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller Definitions
|
||||||
|
*/
|
||||||
|
|
||||||
|
// POST users/register
|
||||||
|
eventsRouter.get('/all', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
// Get request params
|
||||||
|
let calendarId: number = parseInt(req.query.calendar as string ?? '', 10);
|
||||||
|
|
||||||
|
// Get events
|
||||||
|
let events = await EventService.getAllEvents(calendarId);
|
||||||
|
|
||||||
|
// Send the session details back to the user
|
||||||
|
res.status(200).send(events);
|
||||||
|
} catch (e: any) {
|
||||||
|
console.log('Error handling a request: ' + e.message);
|
||||||
|
res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'}));
|
||||||
|
}
|
||||||
|
});
|
|
@ -0,0 +1,32 @@
|
||||||
|
import * as dotenv from 'dotenv';
|
||||||
|
import * as bcrypt from 'bcrypt';
|
||||||
|
import {Guid} from 'guid-typescript';
|
||||||
|
import {Event} from './event.interface';
|
||||||
|
import {NachklangCalendarDB} from '../Calendar.db';
|
||||||
|
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all events for the given calendar
|
||||||
|
* @param calendarId The calendar Id
|
||||||
|
*/
|
||||||
|
export const getAllEvents = async (calendarId: number): Promise<Event[]> => {
|
||||||
|
let conn = await NachklangCalendarDB.getConnection();
|
||||||
|
let eventRows: Event[] = [];
|
||||||
|
try {
|
||||||
|
const eventsQuery = 'SELECT * FROM events WHERE calendar_id = ?';
|
||||||
|
const eventsRes = await conn.query(eventsQuery, calendarId);
|
||||||
|
|
||||||
|
for(let row of eventsRes) {
|
||||||
|
eventRows.push(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventRows;
|
||||||
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
// Return connection
|
||||||
|
await conn.end();
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user