Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const helpers = require('./_helpers');
const { apis } = require('./routes')

const app = express()
const port = 3000
Expand All @@ -10,6 +14,8 @@ function authenticated(req, res, next){
};

app.get('/', (req, res) => res.send('Hello World!'))

app.use('/api', apis)
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

module.exports = app
7 changes: 7 additions & 0 deletions controllers/apis/admin-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const adminServices = require('../../services/admin-services')
const adminController = {
getTweets: (req, res, next) => {
adminServices.getTweets(req, (err, data) => err ? next(err) : res.json(data))
}
}
module.exports = adminController
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
account: {
type: Sequelize.STRING
},
email: {
type: Sequelize.STRING
},
Expand Down Expand Up @@ -36,7 +39,7 @@ module.exports = {
}
});
},
down: (queryInterface, Sequelize) => {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
2 changes: 1 addition & 1 deletion migrations/20190115071418-create-followship.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
}
});
},
down: (queryInterface, Sequelize) => {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Followships');
}
};
20 changes: 16 additions & 4 deletions migrations/20190115071419-create-like.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Likes', {
id: {
allowNull: false,
Expand All @@ -9,10 +9,22 @@ module.exports = {
type: Sequelize.INTEGER
},
UserId: {
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
//allowNull: false,
//defaultValue: 1
// references: {
// model: 'Users',
// key: 'id'
// }
},
TweetId: {
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
//allowNull: false,
//defaultValue: 1
// references: {
// model: 'Tweets',
// key: 'id'
// }
},
createdAt: {
allowNull: false,
Expand All @@ -24,7 +36,7 @@ module.exports = {
}
});
},
down: (queryInterface, Sequelize) => {
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Likes');
}
};
8 changes: 8 additions & 0 deletions models/followship.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ module.exports = (sequelize, DataTypes) => {
}, {});
Followship.associate = function(models) {
};
Followship.init({
followerId: DataTypes.INTEGER,
followingId: DataTypes.INTEGER
}, {
sequelize,
modelName: 'Followship',
tableName: 'Followships',
})
return Followship;
};
10 changes: 10 additions & 0 deletions models/like.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ module.exports = (sequelize, DataTypes) => {
const Like = sequelize.define('Like', {
}, {});
Like.associate = function(models) {
Like.belongsTo(models.User , { foreignKey: 'UserId' })
Like.belongsTo(models.Tweet, { foreignKey: 'TweetId' })
};
Like.init({
UserId: DataTypes.INTEGER,
TweetId: DataTypes.INTEGER
}, {
sequelize,
modelName: 'Like',
tableName: 'Likes',
})
return Like;
};
12 changes: 12 additions & 0 deletions models/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ module.exports = (sequelize, DataTypes) => {
const Reply = sequelize.define('Reply', {
}, {});
Reply.associate = function(models) {
Reply.belongsTo(models.User, { foreignKey: 'UserId' })
Reply.belongsTo(models.Tweet, { foreignKey: 'UserId' })
};
Reply.init({
UserId: DataTypes.INTEGER,
TweetId: DataTypes.INTEGER,
comment: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Reply',
tableName: 'Replies',

})
return Reply;
};
11 changes: 11 additions & 0 deletions models/tweet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ module.exports = (sequelize, DataTypes) => {
const Tweet = sequelize.define('Tweet', {
}, {});
Tweet.associate = function(models) {
Tweet.belongsTo(models.User , { foreignKey: 'UserId' })
Tweet.hasMany(models.Like, { foreignKey: 'TweetId' })
Tweet.hasMany(models.Reply, { foreignKey: 'TweetId' })
};
Tweet.init({
UserId: DataTypes.INTEGER,
description: DataTypes.TEXT,
}, {
sequelize,
modelName: 'Tweet',
tableName: 'Tweets',
})
return Tweet;
};
21 changes: 21 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
}, {});
User.associate = function(models) {
User.hasMany(models.Reply, { foreignKey: 'UserId' })
User.hasMany(models.Tweet, { foreignKey: 'UserId' })
User.hasMany(models.Like, { foreignKey: 'UserId' })
User.belongsToMany(User, {
through: models.Followship,
foreignKey: 'followingId',
as: 'Followers'
})
};
User.init({
account: DataTypes.STRING,
email: DataTypes.STRING,
name: DataTypes.STRING,
password: DataTypes.STRING,
avatar: DataTypes.STRING,
introduction: DataTypes.TEXT,
role: DataTypes.STRING,
}, {
sequelize,
modelName: 'User',
tableName: 'Users',
})
return User;
};
Loading