-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11f3557
commit 5d09f8d
Showing
19 changed files
with
2,888 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require('dotenv').config(); | ||
|
||
module.exports = { | ||
development: { | ||
username: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD, | ||
database: 'blogs_api', | ||
host: process.env.HOSTNAME, | ||
dialect: 'mysql', | ||
}, | ||
test: { | ||
username: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD, | ||
database: 'blogs_api', | ||
host: process.env.HOSTNAME, | ||
dialect: 'mysql', | ||
}, | ||
production: { | ||
username: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD, | ||
database: 'blogs_api', | ||
host: process.env.HOSTNAME, | ||
dialect: 'mysql', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const express = require('express'); | ||
const userSchema = require('./schemas/userSchema'); | ||
const UserService = require('../services/UserService'); | ||
|
||
const userRouter = express.Router(); | ||
|
||
const validateUser = (body) => { | ||
const { error } = userSchema.validate(body); | ||
|
||
if (error) throw error; | ||
}; | ||
|
||
const create = async (req, res, next) => { | ||
try { | ||
const { | ||
displayName, | ||
email, | ||
password, | ||
image, | ||
} = req.body; | ||
|
||
validateUser(req.body); | ||
|
||
const token = await UserService.create({ displayName, email, password, image }); | ||
|
||
return res.status(201).json({ token }); | ||
} catch (e) { | ||
return next(e); | ||
} | ||
}; | ||
|
||
/* ROUTES */ | ||
userRouter.post('/', create); | ||
|
||
module.exports = userRouter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const statusCodes = { | ||
'User already registered': 409, | ||
}; | ||
|
||
module.exports = (err, _req, res, next) => { | ||
if (err.message) { | ||
const statusCode = statusCodes[err.message] || 400; | ||
|
||
return res.status(statusCode).json({ message: err.message }); | ||
} | ||
|
||
return next(err); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const inputError = require('./inputError'); | ||
const domainError = require('./domainError'); | ||
const serverError = require('./serverError'); | ||
|
||
module.exports = { | ||
inputError, | ||
domainError, | ||
serverError, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = (err, _req, res, next) => { | ||
if (err.isJoi) { | ||
console.log(err); | ||
const { details } = err; | ||
const { message } = details[0]; | ||
|
||
return res.status(400).json({ message }); | ||
} | ||
|
||
return next(err); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = (_err, _req, res, _next) => ( | ||
res.status(500).json({ message: 'Internal server error' }) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const userRouter = require('./UserController'); | ||
|
||
module.exports = { | ||
userRouter, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const Joi = require('joi'); | ||
|
||
const userSchema = Joi.object({ | ||
displayName: Joi.string().min(8).not().empty() | ||
.required(), | ||
email: Joi.string().email().not().empty() | ||
.required(), | ||
password: Joi.string().min(6).required() | ||
.messages({ | ||
'string.min': '"password" length must be 6 characters long', | ||
}), | ||
image: Joi.string(), | ||
}); | ||
|
||
module.exports = userSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('Users', { | ||
id: { | ||
primaryKey: true, | ||
autoIncrement: true, | ||
allowNull: false, | ||
type: Sequelize.INTEGER, | ||
}, | ||
displayName: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
}, | ||
email: { | ||
allowNull: false, | ||
unique: true, | ||
type: Sequelize.STRING, | ||
}, | ||
password: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
}, | ||
image: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
} | ||
}); | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable('Users'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('Categories', { | ||
id: { | ||
primaryKey: true, | ||
allowNull: false, | ||
autoIncrement: true, | ||
type: Sequelize.INTEGER, | ||
}, | ||
name: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
}, | ||
}); | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable('Categories'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('BlogPosts', { | ||
id: { | ||
primaryKey: true, | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
autoIncrement: true, | ||
}, | ||
title: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
}, | ||
content: { | ||
allowNull: false, | ||
type: Sequelize.STRING, | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
}, | ||
published: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), | ||
}, | ||
updated: { | ||
type: Sequelize.DATE, | ||
allowNull: false, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), | ||
} | ||
}); | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable('BlogPosts'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
await queryInterface.createTable('PostsCategories', { | ||
postId: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
references: { | ||
model: 'BlogPosts', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
}, | ||
categoryId: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
references: { | ||
model: 'Categories', | ||
key: 'id', | ||
}, | ||
onDelete: 'CASCADE', | ||
} | ||
}) | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable('PostsCategories'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const User = sequelize.define('User', { | ||
displayName: DataTypes.STRING, | ||
email: { | ||
type: DataTypes.STRING, | ||
unique: true, | ||
}, | ||
password: DataTypes.STRING, | ||
image: DataTypes.STRING, | ||
}, { | ||
timestamps: false, | ||
tableName: 'Users', | ||
}); | ||
|
||
// User.associate = (models) => { | ||
// User.hasMany(models.BlogPost, { | ||
// foreignKey: 'userId', as: 'blogPosts', | ||
// }); | ||
// }; | ||
|
||
return User; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.js')[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; |
Oops, something went wrong.