109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
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({
|
|
eventId: row.event_id,
|
|
calendarId: row.calendar_id,
|
|
uuid: row.uuid,
|
|
name: row.name,
|
|
description: row.description,
|
|
startDateTime: row.start_datetime,
|
|
endDateTime: row.end_datetime,
|
|
createdDate: row.created_date,
|
|
location: row.location,
|
|
createdBy: row.created_by,
|
|
url: row.url,
|
|
wholeDay: row.whole_day
|
|
});
|
|
}
|
|
|
|
return eventRows;
|
|
} catch (err) {
|
|
throw err;
|
|
} finally {
|
|
// Return connection
|
|
await conn.end();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Create the given event in the database
|
|
* @param event The event to create
|
|
*/
|
|
export const createEvent = async (event: Event): Promise<number> => {
|
|
let conn = await NachklangCalendarDB.getConnection();
|
|
try {
|
|
let eventUUID = Guid.create().toString();
|
|
const eventsQuery = 'INSERT INTO events (calendar_id, uuid, name, description, start_datetime, end_datetime, location, created_by, url, whole_day) VALUES (?,?,?,?,?,?,?,?,?,?) RETURNING event_id';
|
|
const eventsRes = await conn.execute(eventsQuery, [event.calendarId, eventUUID, event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url, event.wholeDay]);
|
|
|
|
return eventsRes[0].event_id;
|
|
} catch (err) {
|
|
await conn.rollback();
|
|
throw err;
|
|
} finally {
|
|
// Return connection
|
|
await conn.commit();
|
|
await conn.end();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Update the given event in the database
|
|
* @param event The event to update
|
|
*/
|
|
export const updateEvent = async (event: Event): Promise<number> => {
|
|
let conn = await NachklangCalendarDB.getConnection();
|
|
try {
|
|
const eventsQuery = 'UPDATE events SET name = ?, description = ?, start_datetime = ?, end_datetime = ?, location = ?, created_by = ?, url = ?, whole_day = ? WHERE event_id = ?';
|
|
const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url, event.wholeDay, event.eventId]);
|
|
|
|
return eventsRes.affectedRows;
|
|
} catch (err) {
|
|
await conn.rollback();
|
|
throw err;
|
|
} finally {
|
|
// Return connection
|
|
await conn.commit();
|
|
await conn.end();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Deletes the given event from the database
|
|
* @param event The event to delete
|
|
*/
|
|
export const deleteEvent = async (event: Event): Promise<boolean> => {
|
|
let conn = await NachklangCalendarDB.getConnection();
|
|
try {
|
|
const eventsQuery = 'DELETE FROM events WHERE event_id = ?';
|
|
const eventsRes = await conn.execute(eventsQuery, [event.eventId]);
|
|
|
|
return eventsRes.affectedRows === 1;
|
|
} catch (err) {
|
|
await conn.rollback();
|
|
throw err;
|
|
} finally {
|
|
// Return connection
|
|
await conn.commit();
|
|
await conn.end();
|
|
}
|
|
};
|