Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions server/controller/comments.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
/* eslint-disable eol-last */
// import CommentsModel from '../models/comments';
import QuestionModel from '../models/question';
import CommentsModel from '../models/comments';
import {
errorResponse,
successResponse,
} from '../utilities/responseformat';


const commentsController = {

async createComment(req, res) {
const comment = req.body;
const checkquestion = await QuestionModel.getcommentById(comment.commentId);
if (!checkquestion) {
res.status(404).json({
status: 404,
error: 'comment does not exist',
});
const newComment = req.body;

comment.title = checkquestion.title;
comment.body = checkquestion.body;
comment.userid = req.user.id;
const checkquestion = await QuestionModel.getQuestionById(newComment.questionId);

const newComment = new CommentsModel(comment);
const createdComment = await CommentsModel.createComment(newComment);
if (!checkquestion) {
return errorResponse(res, 404, 'Question does not exist');
}

newComment.userId = req.user.id;
newComment.comment = newComment.comment.replace(/[^A-Z0-9]/ig, '');

const latestComment = new CommentsModel(newComment);
const createdComment = await latestComment.createComment();
return successResponse(res, 201, 'Comment posted', createdComment);
},
};

Expand Down
4 changes: 2 additions & 2 deletions server/controller/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export default {
const {
id,
} = req.user;
req.body.userid = id;
req.body.userId = id;

const question = new Question(req.body);

const meetupExists = await Meetup.retrieveSingleMeetup(question.meetupid);
const meetupExists = await Meetup.retrieveSingleMeetup(question.meetupId);
if (!meetupExists) {
return errorResponse(res, 404, 'Meetup not found');
}
Expand Down
2 changes: 1 addition & 1 deletion server/controller/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const UserController = {

const createdUser = await newUser.newUserSignUp();

return successResponse(res, 201, 'User Created', createdUser);
return successResponse(res, 201, 'You have created an account', createdUser);
},

async loginUser(req, res) {
Expand Down
64 changes: 31 additions & 33 deletions server/db/migrationsCreateTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ console.log('Creating tables...');
console.log('Creating users table...');
await pool.query(`CREATE TABLE IF NOT EXISTS users(
id SERIAL PRIMARY KEY,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
username VARCHAR(50) UNIQUE NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
user_name VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
phonenumber VARCHAR(11) NOT NULL,
phone_number VARCHAR(11) NOT NULL,
password VARCHAR(100) NOT NULL,
admin BOOLEAN DEFAULT FALSE,
createdon TIMESTAMPTZ DEFAULT NOW())`);
created_on TIMESTAMPTZ DEFAULT NOW())`);

const password = bcrypt.hashPassword(process.env.ADMIN_PASSWORD);
const Admin = {
Expand All @@ -32,8 +32,8 @@ console.log('Creating tables...');
phonenumber: '08135266484',
};

const queryPlaceholder = `INSERT INTO users (firstname, lastname, username, email, password, phonenumber)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, firstname, lastname, username, email, phonenumber`;
const queryPlaceholder = `INSERT INTO users (first_name, last_name, user_name, email, password, phone_number)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING id, first_name, last_name, user_name, email, phone_number`;
const values = [Admin.firstname, Admin.lastname, Admin.username,
Admin.email, Admin.password, Admin.phonenumber,
];
Expand All @@ -50,53 +50,51 @@ console.log('Creating tables...');
id SERIAL PRIMARY KEY,
topic VARCHAR(255) NOT NULL,
location TEXT NOT NULL,
happeningon VARCHAR(50) NOT NULL,
happening_on VARCHAR(50) NOT NULL,
image VARCHAR(50),
createdon TIMESTAMPTZ DEFAULT NOW())`);
created_on TIMESTAMPTZ DEFAULT NOW())`);

console.log('Creating questions table...');
await pool.query(`CREATE TABLE IF NOT EXISTS questions(
id SERIAL PRIMARY KEY,
meetupid INT NOT NULL,
meetup_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
upvotes INT DEFAULT 0,
downvotes INT DEFAULT 0,
userid INT NOT NULL,
createdon TIMESTAMPTZ DEFAULT NOW(),
FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE)`);
up_votes INT DEFAULT 0,
down_votes INT DEFAULT 0,
user_id INT NOT NULL,
created_on TIMESTAMPTZ DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (meetup_id) REFERENCES meetups (id) ON DELETE CASCADE)`);

console.log('Creating rsvps table...');
await pool.query(`CREATE TABLE IF NOT EXISTS rsvps(
id SERIAL,
meetupid INT NOT NULL,
userid INT NOT NULL,
meetup_id INT NOT NULL,
user_id INT NOT NULL,
response VARCHAR(5) NOT NULL,
PRIMARY KEY(meetupid, userid),
FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE,
FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE)`);
PRIMARY KEY(meetup_id, user_id),
FOREIGN KEY (meetup_id) REFERENCES meetups (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE)`);

console.log('Creating comments table...');
await pool.query(`CREATE TABLE IF NOT EXISTS comments(
id SERIAL PRIMARY KEY,
meetupid INT NOT NULL,
questionid INT NOT NULL,
body TEXT NOT NULL,
userid INT NOT NULL,
createdon TIMESTAMPTZ DEFAULT NOW(),
FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (questionid) REFERENCES questions (id) ON DELETE CASCADE,
FOREIGN KEY (meetupid) REFERENCES meetups (id) ON DELETE CASCADE)`);
question_id INT NOT NULL,
comment TEXT NOT NULL,
user_id INT NOT NULL,
created_on TIMESTAMPTZ DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (question_id) REFERENCES questions (id) ON DELETE CASCADE)`);

console.log('Creating votes table...');
await pool.query(`CREATE TABLE IF NOT EXISTS votes(
id SERIAL PRIMARY KEY,
userid INT NOT NULL,
questionid INT NOT NULL,
user_id INT NOT NULL,
question_id INT NOT NULL,
vote VARCHAR(8) NOT NULL,
FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (questionid) REFERENCES questions (id) ON DELETE CASCADE)`);
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (question_id) REFERENCES questions (id) ON DELETE CASCADE)`);
} catch (error) {
console.log(error);
}
Expand Down
1 change: 1 addition & 0 deletions server/db/migrationsDropTables.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable eol-last */
import pool from './index';

console.log('Dropping tables...');
Expand Down
25 changes: 25 additions & 0 deletions server/middleware/validatecomment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable eol-last */
import Validator from 'validatorjs';
import {
errorResponse,
} from '../utilities/responseformat';
import customErrorMessages from '../utilities/errorresponses';


export default class CommentValidation {
static validComment(req, res, next) {
const comments = req.body;

const commentProperties = {
questionId: 'required|numeric',
comment: 'required|string|max:500',
};

const validator = new Validator(comments, commentProperties, customErrorMessages);
validator.passes(() => next());
validator.fails(() => {
const errors = validator.errors.all();
return errorResponse(res, 400, errors);
});
}
}
17 changes: 17 additions & 0 deletions server/middleware/validateid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable eol-last */
import {
errorResponse,
} from '../utilities/responseformat';

export default (req, res, next) => {
const {
id,
} = req.params;
const newId = parseInt(id, 10);

if (!newId) {
return errorResponse(res, 422, 'Invalid id.');
}
req.params.id = newId;
return next();
};
6 changes: 3 additions & 3 deletions server/middleware/validatemeetups.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class MeetupValidation {
const meetupProperties = {
topic: 'required|string|min:1|max:255',
location: 'required|string|min:1',
happeningon: ['required', 'date', 'regex:/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}/'],
happeningOn: ['required', 'date', 'regex:/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}/'],
images: 'url',
};

Expand All @@ -26,10 +26,10 @@ export default class MeetupValidation {

static checkDate(req, res, next) {
const {
happeningon,
happeningOn,
} = req.body;
const currentDate = new Date(Date.now());
const meetupDate = new Date(happeningon);
const meetupDate = new Date(happeningOn);

if (currentDate > meetupDate) {
return errorResponse(res, 400, `Your Date should be greater than ${currentDate}.`);
Expand Down
2 changes: 1 addition & 1 deletion server/middleware/validatequestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class QuestionValidation {
const question = req.body;

const questionProperties = {
meetupid: 'required|numeric',
meetupId: 'required|numeric',
title: 'required|string|max:200',
body: 'required|string|max:500',
};
Expand Down
46 changes: 0 additions & 46 deletions server/models/admin.js

This file was deleted.

53 changes: 26 additions & 27 deletions server/models/comments.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
// import pool from '../db/index';
/* eslint-disable eol-last */
import pool from '../db/index';

// export default class Comment {
// constructor(usercomment) {
// this.questionid = usercomment.questionid;
// this.title = usercomment.title;
// this.body = usercomment.body;
// this.comment = usercomment.comment;
// this.userid = usercomment.userid;
// }
export default class Comment {
constructor(userComment) {
this.questionId = userComment.questionId;
this.comment = userComment.comment;
this.userId = userComment.userId;
}

// async createComment() {
// const queryPlaceholder = `INSERT INTO comments (questionid, title, body, comment, userid)
// queryValues ($1, $2, $3, $4, $5) RETURNING *`;
// const queryValues = [this.questionid, this.title, this.body, this.comment, this.userid];
// const {
// rows,
// } = await pool.query(queryPlaceholder, queryValues);
// return rows[0];
// }
async createComment() {
const queryPlaceholder = `INSERT INTO comments (question_id, comment, user_id)
VALUES ($1, $2, $3) RETURNING *`;
const queryValues = [this.questionId, this.comment, this.userId];
const {
rows,
} = await pool.query(queryPlaceholder, queryValues);
return rows[0];
}

// static async getCommentsByQuestion(id) {
// const queryPlaceholder = 'SELECT * FROM comments WHERE questionid = $1';
// const queryValues = [id];
// const {
// rows,
// } = await pool.query(queryPlaceholder, queryValues);
// return rows;
// }
// }
static async getCommentsByQuestion(id) {
const queryPlaceholder = 'SELECT * FROM comments WHERE questionid = $1';
const queryValues = [id];
const {
rows,
} = await pool.query(queryPlaceholder, queryValues);
return rows;
}
}
8 changes: 4 additions & 4 deletions server/models/meetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class Meetup {
constructor(meetup) {
this.topic = meetup.topic;
this.location = meetup.location;
this.happeningon = meetup.happeningon;
this.happeningOn = meetup.happeningOn;
this.image = meetup.image;
}

async createMeetup() {
const queryPlaceholder = `INSERT INTO meetups (topic, location, happeningon,
const queryPlaceholder = `INSERT INTO meetups (topic, location, happening_on,
image) VALUES ($1, $2, $3, $4) RETURNING *`;
const entryValues = [this.topic, this.location, this.happeningon,
const entryValues = [this.topic, this.location, this.happeningOn,
this.image,
];
const {
Expand All @@ -40,7 +40,7 @@ class Meetup {
}

static async retrieveUpcomingMeetups(currentDate) {
const queryPlaceholder = 'SELECT * FROM meetups WHERE happeningon > $1 ORDER BY happeningon';
const queryPlaceholder = 'SELECT * FROM meetups WHERE happening_on > $1 ORDER BY happening_on';
const queryValues = [currentDate];
const {
rows,
Expand Down
Loading