This commit is contained in:
parent
ccfa28877c
commit
6cb7f0d59b
3
app.ts
3
app.ts
|
@ -29,7 +29,8 @@ app.use(express.json());
|
|||
// Configure CORS
|
||||
let allowedHosts = [
|
||||
'https://www.nachklang.art',
|
||||
'https://calendar.nachklang.art'
|
||||
'https://calendar.nachklang.art',
|
||||
'http://localhost:4200'
|
||||
];
|
||||
app.use(cors({
|
||||
origin: function (origin: any, callback: any) {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
export interface Event {
|
||||
event_id: number;
|
||||
calendar_id: number;
|
||||
eventId: number;
|
||||
calendarId: number;
|
||||
uuid: string;
|
||||
name: string;
|
||||
description: string;
|
||||
start_datetime: Date;
|
||||
end_datetime: Date;
|
||||
created_date: Date;
|
||||
startDateTime: Date;
|
||||
endDateTime: Date;
|
||||
createdDate: Date;
|
||||
location: string;
|
||||
created_by: string;
|
||||
createdBy: string;
|
||||
url: string;
|
||||
}
|
||||
|
|
|
@ -38,19 +38,19 @@ eventsRouter.get('/:calendar/json', async (req: Request, res: Response) => {
|
|||
let password: string = req.query.password as string ?? '';
|
||||
|
||||
if (calendarName.length < 1) {
|
||||
res.status(401).send({'message': 'Please state the name of the calendar you want events from.'});
|
||||
res.status(400).send({'message': 'Please state the name of the calendar you want events from.'});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Array.from(calendarNames.keys()).includes(calendarName)) {
|
||||
res.status(401).send({'message': 'Unknown calendar.'});
|
||||
res.status(400).send({'message': 'Unknown calendar.'});
|
||||
return;
|
||||
}
|
||||
|
||||
let calendarId: number = calendarNames.get(calendarName)!.id;
|
||||
|
||||
if (!CredentialService.hasAccess(calendarName, password)) {
|
||||
res.status(401).send({'message': 'You do not have access to the specified calendar.'});
|
||||
res.status(403).send({'message': 'You do not have access to the specified calendar.'});
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -72,19 +72,19 @@ eventsRouter.get('/:calendar/ical', async (req: Request, res: Response) => {
|
|||
let password: string = req.query.password as string ?? '';
|
||||
|
||||
if (calendarName.length < 1) {
|
||||
res.status(401).send({'message': 'Please state the name of the calendar you want events from.'});
|
||||
res.status(400).send({'message': 'Please state the name of the calendar you want events from.'});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Array.from(calendarNames.keys()).includes(calendarName)) {
|
||||
res.status(401).send({'message': 'Unknown calendar.'});
|
||||
res.status(400).send({'message': 'Unknown calendar.'});
|
||||
return;
|
||||
}
|
||||
|
||||
let calendarId: number = calendarNames.get(calendarName)!.id;
|
||||
|
||||
if (!CredentialService.hasAccess(calendarName, password)) {
|
||||
res.status(401).send({'message': 'You do not have access to the specified calendar.'});
|
||||
res.status(403).send({'message': 'You do not have access to the specified calendar.'});
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -125,21 +125,21 @@ eventsRouter.post('/', async (req: Request, res: Response) => {
|
|||
req.body.startDateTime === undefined ||
|
||||
req.body.endDateTime === undefined
|
||||
) {
|
||||
res.status(401).send({'message': 'Required parameters missing'});
|
||||
res.status(400).send({'message': 'Required parameters missing'});
|
||||
return;
|
||||
}
|
||||
|
||||
let event: Event = {
|
||||
event_id: -1,
|
||||
calendar_id: req.body.calendarId,
|
||||
eventId: -1,
|
||||
calendarId: req.body.calendarId,
|
||||
uuid: '',
|
||||
name: req.body.name,
|
||||
description: req.body.description ?? '',
|
||||
start_datetime: new Date(req.body.startDateTime),
|
||||
end_datetime: new Date(req.body.endDateTime),
|
||||
created_date: new Date(),
|
||||
startDateTime: new Date(req.body.startDateTime),
|
||||
endDateTime: new Date(req.body.endDateTime),
|
||||
createdDate: new Date(),
|
||||
location: req.body.location ?? '',
|
||||
created_by: req.body.createdBy ?? '',
|
||||
createdBy: req.body.createdBy ?? '',
|
||||
url: req.body.url ?? ''
|
||||
};
|
||||
|
||||
|
@ -174,28 +174,30 @@ eventsRouter.put('/:eventId', async (req: Request, res: Response) => {
|
|||
req.body.startDateTime === undefined ||
|
||||
req.body.endDateTime === undefined
|
||||
) {
|
||||
res.status(401).send({'message': 'Required parameters missing'});
|
||||
res.status(400).send({'message': 'Required parameters missing'});
|
||||
return;
|
||||
}
|
||||
|
||||
let event: Event = {
|
||||
event_id: parseInt(req.params.eventId, 10),
|
||||
calendar_id: req.body.calendarId,
|
||||
eventId: parseInt(req.params.eventId, 10),
|
||||
calendarId: req.body.calendarId,
|
||||
uuid: '',
|
||||
name: req.body.name,
|
||||
description: req.body.description ?? '',
|
||||
start_datetime: new Date(req.body.startDateTime),
|
||||
end_datetime: new Date(req.body.endDateTime),
|
||||
created_date: new Date(),
|
||||
startDateTime: new Date(req.body.startDateTime),
|
||||
endDateTime: new Date(req.body.endDateTime),
|
||||
createdDate: new Date(),
|
||||
location: req.body.location ?? '',
|
||||
created_by: req.body.createdBy ?? '',
|
||||
createdBy: req.body.createdBy ?? '',
|
||||
url: req.body.url ?? ''
|
||||
};
|
||||
|
||||
let success = await EventService.updateEvent(event);
|
||||
let successRows = await EventService.updateEvent(event);
|
||||
|
||||
if (success) {
|
||||
if (successRows === 1) {
|
||||
res.status(200).send({'message': 'Event was successfully updated'});
|
||||
} else if (successRows === 0) {
|
||||
res.status(200).send({'message': '0 rows were updated'});
|
||||
} else {
|
||||
res.status(500).send({'message': 'An error occurred during the update process. Please try again.'});
|
||||
}
|
||||
|
@ -223,21 +225,21 @@ eventsRouter.delete('/:eventId', async (req: Request, res: Response) => {
|
|||
if (
|
||||
req.params.eventId === undefined
|
||||
) {
|
||||
res.status(401).send({'message': 'Required parameters missing'});
|
||||
res.status(400).send({'message': 'Required parameters missing'});
|
||||
return;
|
||||
}
|
||||
|
||||
let event: Event = {
|
||||
event_id: parseInt(req.params.eventId, 10),
|
||||
calendar_id: -1,
|
||||
eventId: parseInt(req.params.eventId, 10),
|
||||
calendarId: -1,
|
||||
uuid: '',
|
||||
name: '',
|
||||
description: '',
|
||||
start_datetime: new Date(),
|
||||
end_datetime: new Date(),
|
||||
created_date: new Date(),
|
||||
startDateTime: new Date(),
|
||||
endDateTime: new Date(),
|
||||
createdDate: new Date(),
|
||||
location: '',
|
||||
created_by: '',
|
||||
createdBy: '',
|
||||
url: ''
|
||||
};
|
||||
|
||||
|
|
|
@ -18,7 +18,19 @@ export const getAllEvents = async (calendarId: number): Promise<Event[]> => {
|
|||
const eventsRes = await conn.query(eventsQuery, calendarId);
|
||||
|
||||
for (let row of eventsRes) {
|
||||
eventRows.push(row);
|
||||
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
|
||||
});
|
||||
}
|
||||
|
||||
return eventRows;
|
||||
|
@ -39,7 +51,7 @@ export const createEvent = async (event: Event): Promise<number> => {
|
|||
try {
|
||||
let eventUUID = Guid.create().toString();
|
||||
const eventsQuery = 'INSERT INTO events (calendar_id, uuid, name, description, start_datetime, end_datetime, location, created_by, url) VALUES (?,?,?,?,?,?,?,?,?) RETURNING event_id';
|
||||
const eventsRes = await conn.execute(eventsQuery, [event.calendar_id, eventUUID, event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url]);
|
||||
const eventsRes = await conn.execute(eventsQuery, [event.calendarId, eventUUID, event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url]);
|
||||
|
||||
return eventsRes[0].event_id;
|
||||
} catch (err) {
|
||||
|
@ -56,13 +68,13 @@ export const createEvent = async (event: Event): Promise<number> => {
|
|||
* Update the given event in the database
|
||||
* @param event The event to update
|
||||
*/
|
||||
export const updateEvent = async (event: Event): Promise<boolean> => {
|
||||
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 = ? WHERE event_id = ?';
|
||||
const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url, event.event_id]);
|
||||
const eventsRes = await conn.execute(eventsQuery, [event.name, event.description, event.startDateTime, event.endDateTime, event.location, event.createdBy, event.url, event.eventId]);
|
||||
|
||||
return eventsRes.affectedRows === 1;
|
||||
return eventsRes.affectedRows;
|
||||
} catch (err) {
|
||||
await conn.rollback();
|
||||
throw err;
|
||||
|
@ -81,7 +93,7 @@ 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.event_id]);
|
||||
const eventsRes = await conn.execute(eventsQuery, [event.eventId]);
|
||||
|
||||
return eventsRes.affectedRows === 1;
|
||||
} catch (err) {
|
||||
|
|
|
@ -85,10 +85,10 @@ const createIcalEvent = (event: Event): iCalEvent => {
|
|||
return {
|
||||
header: 'BEGIN:VEVENT\n',
|
||||
uid: event.uuid + '\n',
|
||||
created: formatDate(event.created_date) + 'Z\n',
|
||||
organizer: event.created_by + '\n',
|
||||
start: formatDate(event.start_datetime) + '\n',
|
||||
end: formatDate(event.end_datetime) + '\n',
|
||||
created: formatDate(event.createdDate) + 'Z\n',
|
||||
organizer: event.createdBy + '\n',
|
||||
start: formatDate(event.startDateTime) + '\n',
|
||||
end: formatDate(event.endDateTime) + '\n',
|
||||
summary: event.name + '\n',
|
||||
description: event.description + '\n',
|
||||
location: event.location + '\n',
|
||||
|
|
Loading…
Reference in New Issue
Block a user