From a8f7189cb3e9dc6152fae0eefa7f20dc0f278a82 Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Sun, 25 Dec 2022 15:43:37 +0100 Subject: [PATCH] git add . is a difficult command to execute --- src/models/calendar/events/events.router.ts | 54 +++++++++++++++++++- src/models/calendar/events/events.service.ts | 32 ++++++++++-- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/models/calendar/events/events.router.ts b/src/models/calendar/events/events.router.ts index a18e6c1..aef0ddf 100644 --- a/src/models/calendar/events/events.router.ts +++ b/src/models/calendar/events/events.router.ts @@ -194,8 +194,60 @@ eventsRouter.put('/:eventId', async (req: Request, res: Response) => { let success = await EventService.updateEvent(event); + if (success) { + res.status(200).send({'message': 'Event was successfully updated'}); + } else { + res.status(500).send({'message': 'An error occurred during the update process. Please try again.'}); + } + } catch (e: any) { + let errorGuid = Guid.create().toString(); + logger.error('Error handling a request: ' + e.message, {reference: errorGuid}); + res.status(500).send({ + 'status': 'PROCESSING_ERROR', + 'message': 'Internal Server Error. Try again later.', + 'reference': errorGuid + }); + } +}); - res.status(200).send({'message': 'Event was successfully updated'}); +eventsRouter.delete('/:eventId', async (req: Request, res: Response) => { + try { + // Get params + let password = req.body.password; + + if (!CredentialService.checkAdminPrivileges(password)) { + res.status(403).send({'message': 'Insufficient privileges.'}); + return; + } + + if ( + req.params.eventId === undefined + ) { + res.status(401).send({'message': 'Required parameters missing'}); + return; + } + + let event: Event = { + event_id: parseInt(req.params.eventId, 10), + calendar_id: -1, + uuid: '', + name: '', + description: '', + start_datetime: new Date(), + end_datetime: new Date(), + created_date: new Date(), + location: '', + created_by: '', + url: '' + }; + + let success = await EventService.deleteEvent(event); + + if (success) { + res.status(200).send({'message': 'Event was successfully updated'}); + } else { + res.status(500).send({'message': 'An error occurred during deletion. Please try again.'}); + } } catch (e: any) { let errorGuid = Guid.create().toString(); logger.error('Error handling a request: ' + e.message, {reference: errorGuid}); diff --git a/src/models/calendar/events/events.service.ts b/src/models/calendar/events/events.service.ts index d422482..aab1aa8 100644 --- a/src/models/calendar/events/events.service.ts +++ b/src/models/calendar/events/events.service.ts @@ -39,13 +39,15 @@ export const createEvent = async (event: Event): Promise => { 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.query(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.calendar_id, eventUUID, event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url]); return eventsRes[0].event_id; } catch (err) { + await conn.rollback(); throw err; } finally { // Return connection + await conn.commit(); await conn.end(); } }; @@ -57,17 +59,37 @@ export const createEvent = async (event: Event): Promise => { export const updateEvent = async (event: Event): Promise => { let conn = await NachklangCalendarDB.getConnection(); try { - let eventUUID = Guid.create().toString(); const eventsQuery = 'UPDATE events SET name = ?, description = ?, start_datetime = ?, end_datetime = ?, location = ?, created_by = ?, url = ? WHERE event_id = ?'; - const eventsRes = await conn.query(eventsQuery, [event.name, event.description, event.start_datetime, event.end_datetime, event.location, event.created_by, event.url, event.event_id]); - - console.log(eventsRes); + 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]); return eventsRes.affectedRows === 1; } 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 => { + let conn = await NachklangCalendarDB.getConnection(); + try { + const eventsQuery = 'DELETE FROM events WHERE event_id = ?'; + const eventsRes = await conn.execute(eventsQuery, [event.event_id]); + + return eventsRes.affectedRows === 1; + } catch (err) { + await conn.rollback(); + throw err; + } finally { + // Return connection + await conn.commit(); await conn.end(); } };