API-19: Adding event endpoint
This commit is contained in:
parent
f716d24d51
commit
12333ebead
|
@ -106,3 +106,35 @@ dataRouter.get('/friendship/:isDevCall', async (req: Request, res: Response) =>
|
||||||
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
|
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
dataRouter.get('/event/:isDevCall', async (req: Request, res: Response) => {
|
||||||
|
try {
|
||||||
|
let userId = (req.query.userId ?? '').toString();
|
||||||
|
let sessionId = (req.query.sessionId ?? '').toString();
|
||||||
|
let sessionKey = (req.query.sessionKey ?? '').toString();
|
||||||
|
let useDev: boolean = (req.params.isDevCall ?? '') === 'dev'; // TBD
|
||||||
|
|
||||||
|
if (userId === '' || sessionId === '' || sessionKey === '') {
|
||||||
|
res.status(400).send({
|
||||||
|
'status': 'WRONG_PARAMS',
|
||||||
|
'message': 'Missing or wrong parameters'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!await UserService.checkSession(useDev, userId, sessionId, sessionKey)) {
|
||||||
|
res.status(403).send({
|
||||||
|
'status': 'INVALID_SESSION',
|
||||||
|
'message': 'The user or session could not be found or the session is invalid'
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = await DataService.getEventData(useDev, userId);
|
||||||
|
|
||||||
|
res.status(200).send(data);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error('Error handling a request: ' + e.message);
|
||||||
|
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -52,6 +52,42 @@ export interface Friendship {
|
||||||
friendUsername: string;
|
friendUsername: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in the getEventData method
|
||||||
|
*/
|
||||||
|
export interface Invite {
|
||||||
|
inviteId: string;
|
||||||
|
inviteKey: string;
|
||||||
|
validUntil: Date;
|
||||||
|
alreadyUsed: boolean;
|
||||||
|
invitedPersonName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in the getEventData method
|
||||||
|
*/
|
||||||
|
export interface Registration {
|
||||||
|
registrationId: string;
|
||||||
|
name: string;
|
||||||
|
registeredDate: Date;
|
||||||
|
takesPart: boolean;
|
||||||
|
comment: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used in the getEventData method as a return value
|
||||||
|
*/
|
||||||
|
export interface Event {
|
||||||
|
eventId: string;
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
takesPlaceDate: Date;
|
||||||
|
registrationUntilDate: Date;
|
||||||
|
maxParticipants: number;
|
||||||
|
invites: Invite[];
|
||||||
|
registrations: Registration[];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all data about the given user
|
* Returns all data about the given user
|
||||||
* @param useDev If the dev or prod database should be used
|
* @param useDev If the dev or prod database should be used
|
||||||
|
@ -135,7 +171,7 @@ export const getSessionData = async (useDev: boolean, userId: string): Promise<S
|
||||||
* Returns all friends of the given user
|
* Returns all friends of the given user
|
||||||
* @param useDev If the dev or prod database should be used
|
* @param useDev If the dev or prod database should be used
|
||||||
* @param userId The userId of the user to fetch the friends for
|
* @param userId The userId of the user to fetch the friends for
|
||||||
* @return Friendship A list of friends
|
* @return Friendship[] A list of friends
|
||||||
*/
|
*/
|
||||||
export const getFriendshipData = async (useDev: boolean, userId: string): Promise<Friendship[]> => {
|
export const getFriendshipData = async (useDev: boolean, userId: string): Promise<Friendship[]> => {
|
||||||
let conn;
|
let conn;
|
||||||
|
@ -169,3 +205,81 @@ export const getFriendshipData = async (useDev: boolean, userId: string): Promis
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all events of the given user
|
||||||
|
* @param useDev If the dev or prod database should be used
|
||||||
|
* @param userId The userId of the user to fetch the friends for
|
||||||
|
* @return Event[] A list of events
|
||||||
|
*/
|
||||||
|
export const getEventData = async (useDev: boolean, userId: string): Promise<Event[]> => {
|
||||||
|
let conn;
|
||||||
|
try {
|
||||||
|
if (useDev) {
|
||||||
|
conn = await dev_pool.getConnection();
|
||||||
|
} else {
|
||||||
|
conn = await prod_pool.getConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
let eventRows = await conn.query('SELECT event_id, name, description, takes_place_date, registration_until_date, max_participants FROM events WHERE creator_id = ?', userId);
|
||||||
|
|
||||||
|
let eventsMap = new Map<string, Event>();
|
||||||
|
let eventIds: string[] = [];
|
||||||
|
|
||||||
|
for (let row of eventRows) {
|
||||||
|
eventIds.push(row.event_id);
|
||||||
|
let event = {
|
||||||
|
eventId: row.event_id,
|
||||||
|
name: row.name,
|
||||||
|
description: row.description,
|
||||||
|
takesPlaceDate: row.takes_place_date,
|
||||||
|
registrationUntilDate: row.registration_until_date,
|
||||||
|
maxParticipants: row.max_participants,
|
||||||
|
invites: [],
|
||||||
|
registrations: []
|
||||||
|
};
|
||||||
|
eventsMap.set(row.event_id, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
let registrationRows = await conn.query('SELECT registration_id, name, registered_date, takes_part, comment, event_id FROM event_registration WHERE event_id IN (?)', eventIds);
|
||||||
|
|
||||||
|
for (let row of registrationRows) {
|
||||||
|
let event = eventsMap.get(row.event_id);
|
||||||
|
if (!event) continue;
|
||||||
|
event.registrations.push({
|
||||||
|
registrationId: row.registration_id,
|
||||||
|
name: row.name,
|
||||||
|
registeredDate: row.registered_date,
|
||||||
|
takesPart: row.takes_part,
|
||||||
|
comment: row.comment
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let inviteRows = await conn.query('SELECT invite_id, invite_key, valid_until, already_used, invited_person_name, event_id FROM invitations WHERE event_id IN (?)', eventIds);
|
||||||
|
|
||||||
|
for (let row of inviteRows) {
|
||||||
|
let event = eventsMap.get(row.event_id);
|
||||||
|
if (!event) continue;
|
||||||
|
event.invites.push({
|
||||||
|
inviteId: row.invite_id,
|
||||||
|
inviteKey: row.invite_key,
|
||||||
|
validUntil: row.valid_until,
|
||||||
|
alreadyUsed: row.already_used,
|
||||||
|
invitedPersonName: row.invited_person_name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let eventsList: Event[] = [];
|
||||||
|
for (let event of eventsMap.values()) {
|
||||||
|
eventsList.push(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return eventsList;
|
||||||
|
} catch (err) {
|
||||||
|
throw err;
|
||||||
|
} finally {
|
||||||
|
if (conn) {
|
||||||
|
conn.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user