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": "from_scratch",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"database": "from_scratch",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"database": "from_scratch",
"host": "127.0.0.1",
"dialect": "postgres"
}
}
31 changes: 31 additions & 0 deletions controllers/CommentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { Comment, User } = require('../models')
const { Op, literal, fn, col } = require('sequelize')


const GetPopularComments = async (req, res) => {
try {
const popular = await Comments.findAll({ order: [['likes', 'DESC']],
attributes: [
'id',
'content',
'likes'
],
where: { likes: { [Op.gt]: 3000 } },
include: [
{ model: User, as: 'owner', attributes:['name', 'id'] },
{ model: Comment, as: 'comments', attributes: [] }
],
group: ['Comments.id', 'owner.id']
})
res.send(popular)
} catch (error) {
throw error
}
}
// Work Here
// Work Here

// Dont forget to export your functions
module.exports = {
GetPopularComments
}
91 changes: 91 additions & 0 deletions controllers/PostController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { User, Posts, Comment } = require('../models')
const { Op, literal, fn, col } = require('sequelize')

const GetPopularPosts = async (req, res) => {
try {
const popular = await Posts.findAll({
order: [['likes', 'DESC']],
attributes: [
'id',
'content',
'likes',
[fn('COUNT', col('comments.id')), 'commentCount']
],
where: { likes: { [Op.gt]: 3000 } },
include: [
{ model: User, as: 'user', attributes: ['name', 'id'] },
{ model: Comment, as: 'comments', attributes: [] }
],
group: ['Posts.id', 'user.id']
})
res.send(popular)
} catch (error) {
throw error
}
}

const GetRecentPosts = async (req, res) => {
try {
const recents = await Posts.findAll({ order: [['created_at', 'DESC']] })
res.send(recents)
} catch (error) {
throw error
}
}

const GetPostDetails = async (req, res) => {
try {
const Post = await Twerts.findByPk(req.params.twert_id)
res.send(twert)
} catch (error) {
throw error
}
}


const CreatePost = async (req, res) => {
try {
let userId = parseInt(req.params.user_id)
let postBody = {
userId,
...req.body
}

let post = await Post.create(postBody)
res.send(post)
} catch (error){
throw error
}
}

const UpdatePost = async (req, res) => {
try{
let postId = parseInt(req.params.post_id)
let updatedPost = await Posts.update(req.body, {
where: { id: postId },
returning: true
})
res.send(updatedPost)
} catch(error) {
throw error
}
}

const DeletePost = async (req, res) => {
try {
let postId = parseInt(req.params.post_id)
await Post.destroy({ where: { id: postId}})
res.send({ message: `deleted post of ${postId}`})
} catch (error){
throw error
}
}

module.exports = {
GetPopularPosts,
GetRecentPosts,
GetPostDetails,
CreatePost,
UpdatePost,
DeletePost
}
26 changes: 26 additions & 0 deletions controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { User, Posts } = require('../models')

const GetUser = async (req, res) => {
try {
const users = await User.findAll()
res.send(users)
} catch (error) {
throw error
}
}

const GetUserPost = async (req, res) => {
try {
const userAndPosts = await User.findByPk(req.params.user_id, {
include: [{ model: Posts, as: 'posts' }]
})
res.send(userAndPosts)
} catch (error) {
throw error
}
}

module.exports = {
GetUser,
GetUserPost
}
27 changes: 27 additions & 0 deletions migrations/20220412182548-create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userName: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('users');
}
};
39 changes: 39 additions & 0 deletions migrations/20220412183717-create-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('posts', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
ownerId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'users',
key:'id'
// primaryKey: true,
}
},
content: {
type: Sequelize.STRING
},
createdAt: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('posts');
}
};
39 changes: 39 additions & 0 deletions migrations/20220412183902-create-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('comments', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
ownerId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'users',
key:'id'
// primaryKey: true,
}
},
content: {
type: Sequelize.STRING
},
createdAt: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('comments');
}
};
35 changes: 35 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Comments 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
Comments.belongsTo(models.User, {foreignKey:'ownerId'})
}
}
Comments.init({
// id: DataTypes.INTEGER,
content: DataTypes.STRING,
// createdAt: DataTypes.STRING,
ownerId: {
type:DataTypes.INTEGER,
references: {
model: 'users',
key:'id'
// primaryKey: true
}
}
}, {
sequelize,
modelName: 'Comment',
tableName: 'comments'
});
return Comments;
};
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;
37 changes: 37 additions & 0 deletions models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Post 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
Post.belongsTo(models.User, {foreignKey:'ownerId'})
}
}
Post.init(
{
// id: DataTypes.INTEGER,
content: DataTypes.STRING,
// createdAt: DataTypes.STRING,
ownerId: {
type:DataTypes.INTEGER,
references: {
model: 'users',
key:'id'
// primaryKey: true,
}
}
},
{
sequelize,
modelName: 'Post',
tableName: 'posts'
});
return Post;
};
Loading