Endpoints are separated into distinct packages and interfaces are extracted into files Co-authored-by: Patrick Müller <patrick@mueller-patrick.tech> Reviewed-on: #12 Co-authored-by: Patrick Müller <patrick@plutodev.de> Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit was merged in pull request #12.
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
import {Event} from './event.interface';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const mariadb = require('mariadb');
|
||||
const prod_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_PROD_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
const dev_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_DEV_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user