Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
17 changes: 17 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"development": {
"database": "sequelize_api_hw_development",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"database": "sequelize_api_hw_test",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "sequelize_api_hw_production",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
62 changes: 62 additions & 0 deletions controllers/ReplyController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const {Reply} = require('../models/reply')

const GetReplies = async (req, res) => {
try{
const replies = await Reply.findAll({where: {questionId: (req.params.question_id)}})
res.send(replies)
} catch (error) {
throw error
}
}

const CreateReply = async (req, res) => {
try{
let questionId = parseInt(req.params.question_id)
let userId = parseInt(req.params.user_id)

let replyBody = {
questionId,
userId,
...req.body
}
let reply = await Reply.create(replyBody)
res.send(reply)

} catch (error) {
throw error
}
}

const UpdateReply = async (req, res) => {
try{
let replyId = parseInt(req.params.reply_id)

let updatedReply = await Reply.update(req.body, {
where: { id: replyId },
returning: true
})

res.send(updatedReply)

} catch (error) {
throw error
}
}

const DeleteReply = async (req, res) => {
try{
let replyId = parseInt(req.params.reply_id)
await Reply.destroy({where: { id: replyId }})
res.send({message: `Deleted a reply with an id of ${replyId}`})

} catch (error) {
throw error
}
}

module.exports = {
GetReplies,
CreateReply,
UpdateReply,
DeleteReply
}
43 changes: 43 additions & 0 deletions migrations/20220413015353-create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
username: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
},
createdAt: {
field: 'created_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('users');
}
};
39 changes: 39 additions & 0 deletions migrations/20220413015513-create-question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize, DataTypes) {
await queryInterface.createTable('questions', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
content: {
type: Sequelize.STRING
},
userId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id'
}
},
createdAt: {
field: 'created_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('questions');
}
};
47 changes: 47 additions & 0 deletions migrations/20220413015743-create-reply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize, DataTypes) {
await queryInterface.createTable('replies', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
content: {
type: Sequelize.STRING
},
questionId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'questions',
key: 'id'
}
},
userId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id'
}
},
createdAt: {
field: 'created_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
},
updatedAt: {
field: 'updated_at',
allowNull: true,
type: Sequelize.DATE,
defaultValue: new Date()
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('replies');
}
};
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
44 changes: 44 additions & 0 deletions models/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Question extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Question.belongsTo(models.User, {
foreignKey: 'userId',
as: 'user',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'

})
Question.hasMany(models.Reply, {
foreignKey: 'questionId',
as: 'replies'

})
}
}
Question.init({
content: DataTypes.STRING,
userId: {
type: DataTypes.STRING,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id'
}
}
}, {
sequelize,
modelName: 'Question',
tableName: 'questions'
});
return Question;
};
54 changes: 54 additions & 0 deletions models/reply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
const {
Model
} = require('sequelize');
const user = require('./user');
module.exports = (sequelize, DataTypes) => {
class Reply extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
Reply.belongsTo(models.User, {
foreignKey: 'userId',
as: 'user',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})
Reply.belongsTo(models.Question, {
foreignKey: 'questionId',
as: 'question',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
})

}
}
Reply.init({
content: DataTypes.STRING,
questionId: {
type: DataTypes.STRING,
onDelete: 'CASCADE',
references: {
model: 'questions',
key: 'id'
}
},
userId: {
type: DataTypes.STRING,
onDelete: 'CASCADE',
references: {
model: 'users',
key: 'id'
}
}
}, {
sequelize,
modelName: 'Reply',
tableName: 'replies'
});
return Reply;
};
Loading