diff --git a/backend/apis/nodejs/api.js b/backend/apis/nodejs/api.js index a3c4ac4..560b371 100644 --- a/backend/apis/nodejs/api.js +++ b/backend/apis/nodejs/api.js @@ -23,6 +23,7 @@ app.get('/blinkapi/person/:id', api_controller.verifyToken, api_controller.getPe app.post('/blinkapi/organization', api_controller.verifyToken, api_controller.createOrganization); // Create organization app.delete('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.deleteOrganization); // Delete organization app.post('/blinkapi/organization/post', api_controller.verifyToken, api_controller.createOrganizationPost); // Create a organization's post +app.delete('/blinkapi/organization/post/:id', api_controller.verifyToken, api_controller.deleteOrganizationPost); // Delete a organization's post // Start the server app.listen(process.env.API_SERVER_PORT, () => { diff --git a/backend/apis/nodejs/api_controller.js b/backend/apis/nodejs/api_controller.js index 9321bb9..e09ec4f 100644 --- a/backend/apis/nodejs/api_controller.js +++ b/backend/apis/nodejs/api_controller.js @@ -12,7 +12,7 @@ */ const bcrypt = require('bcrypt'); const crypto = require('crypto'); -const pgp = require('pg-promise')(); +const pgp = require('pg-promise')(); // In the future I'd like to use knex.js to avoid raw sql const jwt = require('jsonwebtoken'); require('dotenv').config(); @@ -116,11 +116,11 @@ async function getPerson(req, res){ return res.status(200).send(user); } } - return res.status(403); + return res.status(403).json("Forbidden"); } catch (error) { console.log(error); - return res.status(500); + return res.status(500).json("Internal server error"); } } @@ -184,7 +184,7 @@ async function deleteOrganization(req, res){ } catch (error) { console.error(error); - return res.status(500); + return res.status(500).json("Internal server error"); } } @@ -216,6 +216,30 @@ async function createOrganizationPost(req, res){ } } +// DELETE +async function deleteOrganizationPost(req, res){ + const organizationPostIdToDelete = req.params.id; + try { + if(await db.oneOrNone(' SELECT *' + + ' FROM "OrganizationPost"' + + ' JOIN "OrganizationAdministrator"' + + ' ON "OrganizationPost".organization_id = "OrganizationAdministrator".id_organization' + + ' WHERE "OrganizationPost".id = $1 and "OrganizationAdministrator".id_person = $2', + [organizationPostIdToDelete, req.jwt.person_id])){ + await db.none('DELETE FROM "OrganizationPost" WHERE id = $1', [organizationPostIdToDelete]); + return res.status(200).json("Ok"); + } + else{ + return res.status(403).json("Forbidden"); + } + + } + catch (error) { + console.error(error); + return res.status(500).json("Internal server error"); + } +} + // ======== END API ENDPOINTS ======== async function checkUserCredentials(email, password){ @@ -285,5 +309,6 @@ module.exports = { verifyToken, createOrganization, deleteOrganization, - createOrganizationPost + createOrganizationPost, + deleteOrganizationPost }; \ No newline at end of file