Skip to content

Commit 3e507cf

Browse files
articles and users controllers
Signed-off-by: Arnav Gupta <[email protected]>
1 parent cd00d16 commit 3e507cf

File tree

10 files changed

+881
-0
lines changed

10 files changed

+881
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { Article, User } = require('../db/models')
2+
3+
async function getArticleById(id) {
4+
return await Article.findOne({
5+
include: [{ model: User, as: 'author' }],
6+
where: { id }
7+
})
8+
}
9+
10+
async function getArticleByAuthorEmail(email) {
11+
const article = await Article.findOne({
12+
include: [{ model: User, as: 'author' }],
13+
where: { '$author.email$': email },
14+
})
15+
16+
return article
17+
}
18+
async function createArticle(authorId, title, subtitle, body) {
19+
const article = await Article.create({
20+
authorId,
21+
title,
22+
subtitle,
23+
body,
24+
})
25+
26+
return article
27+
}
28+
29+
module.exports = {
30+
getArticleById,
31+
getArticleByAuthorEmail,
32+
createArticle,
33+
}

Lecture17/blog-project/controllers/comments.js

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { User } = require('../db/models')
2+
3+
async function getAllUsers() {
4+
return await User.findAll()
5+
}
6+
7+
async function getUserById(id) {
8+
return await User.findOne({
9+
where: {
10+
id: id,
11+
},
12+
})
13+
}
14+
15+
async function createUser(name, email) {
16+
const user = await User.create({
17+
name,
18+
email,
19+
})
20+
return user
21+
}
22+
23+
module.exports = {
24+
getAllUsers,
25+
createUser,
26+
getUserById,
27+
}

Lecture17/blog-project/db/models.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const Sequelize = require('sequelize')
2+
3+
const db = new Sequelize({
4+
dialect: 'mysql',
5+
database: 'blogdb',
6+
username: 'bloguser',
7+
password: 'blogs',
8+
})
9+
10+
const User = db.define('user', {
11+
name: {
12+
type: Sequelize.STRING,
13+
allowNull: false,
14+
},
15+
email: {
16+
type: Sequelize.STRING,
17+
unique: true,
18+
},
19+
})
20+
21+
const Article = db.define('article', {
22+
title: {
23+
type: Sequelize.STRING(100),
24+
allowNull: false,
25+
},
26+
subtitle: Sequelize.STRING(150),
27+
body: {
28+
type: Sequelize.TEXT,
29+
allowNull: false,
30+
},
31+
})
32+
33+
const Comment = db.define('comment', {
34+
title: {
35+
type: Sequelize.STRING(50),
36+
allowNull: false,
37+
},
38+
message: {
39+
type: Sequelize.TEXT,
40+
allowNull: false,
41+
},
42+
})
43+
44+
Article.belongsTo(User, { as: 'author' })
45+
User.hasMany(Article, { as: 'author' })
46+
47+
Comment.belongsTo(User, { as: 'commentor' })
48+
User.hasMany(Comment, { as: 'commentor' })
49+
50+
Comment.belongsTo(Article)
51+
Article.hasMany(Comment)
52+
53+
module.exports = {
54+
db,
55+
Article,
56+
User,
57+
Comment,
58+
}

0 commit comments

Comments
 (0)