From d92c3b0a8f6e1e2b556e710ab827624e7c6f38a0 Mon Sep 17 00:00:00 2001 From: Joshua King Date: Sun, 2 Aug 2020 20:43:31 +0100 Subject: [PATCH] Tidying files --- client/src/App.js | 2 +- index.js | 10 +- models/activities.js | 271 ------------------------------------------- models/comments.js | 82 ------------- models/tags.js | 54 --------- routes/activities.js | 146 ----------------------- routes/comments.js | 61 ---------- routes/tags.js | 58 --------- 8 files changed, 5 insertions(+), 679 deletions(-) delete mode 100644 models/activities.js delete mode 100644 models/comments.js delete mode 100644 models/tags.js delete mode 100644 routes/activities.js delete mode 100644 routes/comments.js delete mode 100644 routes/tags.js diff --git a/client/src/App.js b/client/src/App.js index 504965b..b7ed2ba 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -15,7 +15,7 @@ class App extends Component { render() { return ( -
+
diff --git a/index.js b/index.js index 2effd7f..9d384dd 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,10 @@ const passport = require('koa-passport'); //const AccessControl = require('accesscontrol'); //import the Router we defined in articles.js -var activities = require('./routes/activities.js'); + var welcome = require('./routes/welcome.js'); -var comments = require('./routes/comments.js'); var admin = require('./routes/admin.js'); var users = require('./routes/users.js'); -var tags = require('./routes/tags.js'); var quiz = require('./routes/quiz.js'); var questions = require('./routes/questions.js'); var answers = require('./routes/answers.js'); @@ -29,12 +27,12 @@ app.use(passport.initialize()); //apply the routes as a middleware -app.use(activities.routes()); +//app.use(activities.routes()); app.use(welcome.routes()); app.use(admin.routes()); app.use(users.routes()); -app.use(comments.routes()); -app.use(tags.routes()); +//app.use(comments.routes()); +//app.use(tags.routes()); app.use(quiz.routes()); app.use(questions.routes()); app.use(answers.routes()); diff --git a/models/activities.js b/models/activities.js deleted file mode 100644 index 791ae42..0000000 --- a/models/activities.js +++ /dev/null @@ -1,271 +0,0 @@ -var mysql = require('promise-mysql'); -var info = require('../config'); - -exports.getById = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - let sql = `SELECT * FROM activities - WHERE id = ${id} - `; - - let data = await connection.query(sql); - - await connection.end() - return data; - } catch (error) { - console.log(error); - throw new Error(error); - } -} - -exports.getByUserId = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - - sql = `SELECT * - FROM activities - WHERE calendarId IN - (SELECT ID - FROM calendar - WHERE userId = ${id}) - `; - - let data = await connection.query(sql); - - await connection.end() - return data; - } catch (error) { - console.log(error); - throw new Error(error); - } -} - -exports.getByUserTagged = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - - sql = `SELECT * - FROM activities - WHERE ID IN - (SELECT calendarActivityId - FROM TaggedUsers - WHERE taggedUserId = ${id}) - `; - - let data = await connection.query(sql); - - await connection.end() - return data; - } catch (error) { - console.log(error); - throw new Error(error); - } -} - - -exports.getAll = async (page, limit, order)=> { - try { - - const connection = await mysql.createConnection(info.config); - - //this is the sql statement to execute - let sql = `SELECT * FROM activities - `; - let data = await connection.query(sql); - - await connection.end(); - return data; - - } catch (error) { - console.log(error); - throw new Error(error); - } -} - -exports.add = async (activity, calendar) => { - try { - - //Validation checks for POST - if(calendar.userId === undefined){ - throw {message:'a user id is essential', status:400}; - } - if(calendar.start === undefined){ - throw {message: 'calendar entry needs a start time', status:400}; - } - - if(calendar.end === undefined){ - throw {message: 'calendar entry needs an end time', status:400}; - } - - if(activity.title === undefined){ - throw {message:'title is required', status:400}; - } - - - //check for a valid website address - if(activity.url){ - var re = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/; - if(!re.test(String(activity.url).toLowerCase())){ - console.log("invalid web address format"); - throw {message:'invalid web address format', status:400}; - } - } - const connection = await mysql.createConnection(info.config); - - let sql = `INSERT INTO calendar - SET ? - `; - - await connection.query(sql, calendar); - - //Make sure that the calendars ID is set into the activity calendarId field - let calId = await connection.query(`SELECT LAST_INSERT_ID()`); - activity.calendarId = calId[0]['LAST_INSERT_ID()']; - - sql = `INSERT INTO activities - SET ? - `; - - - let data = await connection.query(sql, activity); - - await connection.end(); - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } -} - -exports.update = async (id, activity, calendar) => { - try { - - if(activity.url){ - //check for a valid website address if one is supplied - var re = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/; - if(!re.test(String(activity.url).toLowerCase())){ - console.log("invalid web address format"); - throw {message:'invalid web address format', status:400}; - } - } - - const connection = await mysql.createConnection(info.config); - - //Fill empty fields from current activity. - let sql = `SELECT * FROM activities - WHERE id = ${id} - `; - - dataCheck = await connection.query(sql); - - //Check record exists - if(!dataCheck.length){ - await connection.end(); - throw {message:'record not found', status:400}; - } - //Fill empty fields - for(var key in dataCheck[0]) { - var value = dataCheck[0][key]; - if(!activity[key]){ - activity[key] = value - } - } - - - // Extract the calendar entry to update - if(!activity.calendarId){ - await connection.end(); - throw {message:'invalid activity entry: no calendar id', status:400}; - } - let calId = activity.calendarId; - - //Fill empty fields from current calendar - sql = `SELECT * FROM calendar - WHERE id = ${calId} - `; - - dataCheck = await connection.query(sql); - - //Check record exists - if(!dataCheck.length){ - await connection.end(); - throw {message:'calendar record not found', status:400}; - } - //Fill empty fields - for(var key in dataCheck[0]) { - var value = dataCheck[0][key]; - if(!calendar[key]){ - calendar[key] = value - } - } - - - //Update activities - sql = `UPDATE activities - SET ? - WHERE id = ${id} - `; - data = await connection.query(sql, activity); - - //Update calendar - sql = `UPDATE calendar - SET ? - WHERE id = ${calId} - `; - data = await connection.query(sql, calendar); - - await connection.end(); - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } -} - -exports.del = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - //Find the associated calendar entry - let sql = `SELECT * FROM activities - WHERE id = ${id} - `; - - - let data = await connection.query(sql); - - // Extract the calendarId to delete - let calId = data[0].calendarId; - - sql = `DELETE FROM activities - WHERE id = ${id} - `; - - let sqlC = `DELETE FROM calendar - WHERE id = ${calId} - `; - - - data = await connection.query(sql); - data = await connection.query(sqlC); - await connection.end(); - - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } -} diff --git a/models/comments.js b/models/comments.js deleted file mode 100644 index 2885403..0000000 --- a/models/comments.js +++ /dev/null @@ -1,82 +0,0 @@ -var mysql = require('promise-mysql'); -var info = require('../config'); - -exports.add = async (comment) => { - try { - - const connection = await mysql.createConnection(info.config); - - //TODO: Validation checks - - let sql = `INSERT INTO comments - SET ? - `; - - let commentData = { - userId: comment.userId, - activityId: comment.activityId, - allText: comment.allText, - dateCreated: new Date(), - dateModified: new Date() - } - let data = await connection.query(sql, commentData); - - await connection.end(); - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } -} - - -exports.getById = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - let sql = `SELECT * FROM comments - WHERE id = ${id} - `; - - let data = await connection.query(sql); - - await connection.end() - return data; - } catch (error) { - console.log(error); - throw new Error(error); - } - -} - -exports.getAllByActivityId = async (id) => { - try { - - const connection = await mysql.createConnection(info.config); - - let sql = `SELECT * FROM comments - WHERE activityId = ${id} - `; - - let data = await connection.query(sql); - - await connection.end() - return data; - } catch (error) { - console.log(error); - throw new Error(error); - } - -} - - - - - - - - - diff --git a/models/tags.js b/models/tags.js deleted file mode 100644 index 8442cea..0000000 --- a/models/tags.js +++ /dev/null @@ -1,54 +0,0 @@ -'use strict'; - -var mysql = require('promise-mysql'); -var info = require('../config'); - - - -exports.add = async (tag) => { - try { - - const connection = await mysql.createConnection(info.config); - - //TODO: Validation checks - - let sql = `INSERT INTO TaggedUsers - SET ? - `; - - - let data = await connection.query(sql, tag); - - await connection.end(); - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } -} - - - -exports.update = async (id ,update) => { - try{ - - const connection = await mysql.createConnection(info.config); - - let sql = `UPDATE TaggedUsers - SET ? - WHERE id = ${id} - `; - - let data = await connection.query(sql, update); - await connection.end(); - return data; - - } catch (error) { - if(error.status === undefined) - error.status = 500; - throw error; - } - -} \ No newline at end of file diff --git a/routes/activities.js b/routes/activities.js deleted file mode 100644 index 593576f..0000000 --- a/routes/activities.js +++ /dev/null @@ -1,146 +0,0 @@ - - -var Router = require('koa-router'); -var model = require('../models/activities'); - -var router = Router({ - prefix: '/api/v1.0/activities' -}); - - -var bodyParser = require('koa-bodyparser'); - - -//GET an activity by its id -router.get('/:id([0-9]{1,})', async (cnx, next) =>{ - - let id = cnx.params.id; - let data = await model.getById(id); - - if(data.length === 0){ - cnx.response.status = 404; - cnx.body = {message:"activity not found"} - } - else - cnx.body = data; -}); - -//POST (create) new activity with validation -router.post('/', bodyParser(), async (cnx, next) =>{ - try{ - // - let newActivity = { - title:cnx.request.body.title, - description:cnx.request.body.description, - url:cnx.request.body.url, - Location:cnx.request.body.Location, - calendarId:cnx.request.body.calendarId, - }; - let newCalendarEntry = { - start:cnx.request.body.start, - end:cnx.request.body.end, - userId:cnx.request.body.userId, - //Calendar Location is the same as activity location - Location:cnx.request.body.Location - } - //Using model.add with a null id ensures it's a new entry - await model.add(newActivity, newCalendarEntry); - cnx.response.status = 201; - cnx.body = {message:"added successfully"}; - } catch(error) { - console.log(error); - cnx.response.status = error.status; - cnx.body = {message:error.message}; - } -}); - -//PUT (update) an activity by its id with validation -router.put('/:id([0-9]{1,})', bodyParser(), async (cnx, next) =>{ - try { - let id = cnx.params.id; - let updateActivity = { - title:cnx.request.body.title, - description:cnx.request.body.description, - url:cnx.request.body.url, - Location:cnx.request.body.Location, - calendarId:cnx.request.body.calendarId - }; - let updateCalendarEntry = { - start:cnx.request.body.start, - end:cnx.request.body.end, - userId:cnx.request.body.userId, - //Calendar Location is the same as activity location - Location:cnx.request.body.Location - } - //Using model.add with an id ensures it's an updated entry - await model.update(id, updateActivity, updateCalendarEntry); - cnx.response.status = 202; - cnx.body = {message:"record updated successfully"}; - - - } catch(error) { - console.log(error); - cnx.response.status = error.status; - cnx.body = {message:error.message}; - } -}); - -//DELETE an activity by id -router.del('/:id([0-9]{1,})', async (cnx, next) =>{ - try { - let id = cnx.params.id; - await model.del(id); - cnx.response.status = 410; - cnx.body = {message:"Record deleted"}; - } catch(error) { - cnx.response.status = error.status; - cnx.body = {message:"Didn't delete the file"}; - } - -}); - -//GET all activities that a user created -router.get('/by_user_created_id/:id([0-9]{1,})', async (cnx, next) =>{ - - let id = cnx.params.id; - let data = await model.getByUserId(id); - - if(data.length === 0){ - cnx.response.status = 404; - cnx.body = {message:"activity not found"} - } - else - cnx.body = data; - -}); - -//GET all activities that a user is tagged in -router.get('/by_user_tagged_id/:id([0-9]{1,})', async (cnx, next) =>{ - - let id = cnx.params.id; - let data = await model.getByUserTagged(id); - - if(data.length === 0){ - cnx.response.status = 404; - cnx.body = {message:"activity not found"} - } - else - cnx.body = data; - -}); - - - -//get all activities Not needed for project -router.get('/', async (cnx) => { - try{ - cnx.body = await model.getAll(); - } catch(error) { - cnx.response.status = error.status; - cnx.body = {message:"There are no activities to show"}; - } - }) - -module.exports = router; - - diff --git a/routes/comments.js b/routes/comments.js deleted file mode 100644 index 5491c94..0000000 --- a/routes/comments.js +++ /dev/null @@ -1,61 +0,0 @@ -'use strict'; - -var Router = require('koa-router'); -var model = require('../models/comments'); - -var router = Router({ - prefix: '/api/v1.0/comments' -}); - - -var bodyParser = require('koa-bodyparser'); - -//POST (create) a new comment -router.post('/', bodyParser(), async (cnx, next) =>{ - try{ - let newComment = {userId:cnx.request.body.userId, - activityId:cnx.request.body.activityId, - allText:cnx.request.body.allText - }; - await model.add(newComment); - cnx.response.status = 201; - cnx.body = {message:"comment added successfully"}; - - } catch(error){ - console.log(error); - cnx.response.status = error.status; - cnx.body = {message:error.message}; - } -}); - -//GET a comment by it's id -router.get('/:id([0-9]{1,})', async (cnx, next) => { - - let id = cnx.params.id; - let data = await model.getById(id); - - if(data.length === 0){ - cnx.response.status = 404; - cnx.body = {message:"comment not found"} - } - else - cnx.body = data; -}); - -//GET all comments on an activity -router.get('/by_activity/:id([0-9]{1,})', async (cnx, next) => { - - let id = cnx.params.id; - let data = await model.getAllByActivityId(id); - - if(data.length === 0){ - cnx.response.status = 404; - cnx.body = {message:"comment not found"} - } - else - cnx.body = data; - -}); - - -module.exports = router; \ No newline at end of file diff --git a/routes/tags.js b/routes/tags.js deleted file mode 100644 index 7e44901..0000000 --- a/routes/tags.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -var Router = require('koa-router'); -var model = require('../models/tags'); - - -var router = Router({ - prefix: '/api/v1.0/tags' -}); //Prefixed all routes with /api/v1.0/users - -var bodyParser = require('koa-bodyparser'); - -//POST a new tag for a user on an activity -router.post('/', bodyParser(), async (cnx, next) =>{ - - - let newTag = { - taggedUserId:cnx.request.body.taggedUserId, - taggedByUserId:cnx.request.body.taggedByUserId, - calendarActivityId:cnx.request.body.calendarActivityId, - accepted: cnx.request.body.accepted - } - - try{ - await model.add(newTag); - cnx.response.status = 201; - cnx.body = {message:"user has been tagged - ready to accept"}; - } - catch(error){ - cnx.response.status = error.status; - cnx.body = {error:error.status, - message:error.message}; - } - -}); - -//PUT (update) tag request -router.put('/:id([0-9]{1,})', bodyParser(), async (cnx, next) =>{ - try { - console.log("In tag router"); - let id = cnx.params.id; - let updateTag = {accepted: cnx.request.body.accepted - }; - - await model.update(id, updateTag); - cnx.response.status = 202; - cnx.body = {message:"tag accepted"}; - - - } catch(error) { - console.log(error); - cnx.response.status = error.status; - cnx.body = {message:error.message}; - } -}); - - -module.exports = router; \ No newline at end of file