Skip to content

Commit 1f9616c

Browse files
articles with categories
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 6f24039 commit 1f9616c

File tree

7 files changed

+109
-17
lines changed

7 files changed

+109
-17
lines changed

Lecture17/blog-project/controllers/articles.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const { Article, User } = require('../db/models')
1+
const { db, Article, User, Category } = require('../db/models')
22

33
async function getAllArticles() {
4-
return await Article.findAll()
4+
return await Article.findAll({
5+
include: [{ model: User, as: 'author' }, { model: Category }],
6+
})
57
}
68
async function getArticleById(id) {
79
return await Article.findOne({
810
include: [{ model: User, as: 'author' }],
9-
where: { id }
11+
where: { id },
1012
})
1113
}
1214

@@ -18,12 +20,21 @@ async function getArticleByAuthorEmail(email) {
1820

1921
return article
2022
}
21-
async function createArticle(authorId, title, subtitle, body) {
22-
const article = await Article.create({
23-
authorId,
24-
title,
25-
subtitle,
26-
body,
23+
async function createArticle(authorId, title, subtitle, body, categoryIds) {
24+
let article;
25+
await db.transaction(async (t) => {
26+
27+
article = await Article.create({
28+
authorId,
29+
title,
30+
subtitle,
31+
body,
32+
}, {transaction: t})
33+
34+
if (article) {
35+
await article.setCategories(categoryIds, {transaction: t})
36+
}
37+
2738
})
2839

2940
return article
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { Category } = require('../db/models')
2+
3+
async function addCategory(catName) {
4+
return await Category.create({name: catName})
5+
}
6+
7+
async function getAllCategories() {
8+
return await Category.findAll()
9+
}
10+
11+
async function getAllCategoriesAsArray() {
12+
return (await Category.findAll()).map(cat => cat.name)
13+
}
14+
15+
16+
module.exports = {
17+
addCategory,
18+
getAllCategories,
19+
getAllCategoriesAsArray
20+
}

Lecture17/blog-project/db/models.js

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ const Comment = db.define('comment', {
4141
},
4242
})
4343

44+
const Category = db.define('category', {
45+
name: {
46+
type: Sequelize.STRING(30),
47+
allowNull: false,
48+
unique: true
49+
},
50+
})
51+
4452
Article.belongsTo(User, { as: 'author' })
4553
User.hasMany(Article, { as: 'author' })
4654

@@ -50,9 +58,13 @@ User.hasMany(Comment, { as: 'commentor' })
5058
Comment.belongsTo(Article)
5159
Article.hasMany(Comment)
5260

61+
Article.belongsToMany(Category, {through: 'article_category'})
62+
Category.belongsToMany(Article, {through: 'article_category'})
63+
5364
module.exports = {
5465
db,
5566
Article,
5667
User,
5768
Comment,
69+
Category
5870
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { Router } = require('express')
2+
const { getAllCategories, addCategory } = require('../../controllers/categories')
3+
4+
const route = Router()
5+
6+
route.get('/', async (req, res) => {
7+
const categories = await getAllCategories()
8+
return res.status(200).send(categories)
9+
})
10+
11+
route.post('/', async (req, res) => {
12+
try {
13+
const category = await addCategory(req.body.name)
14+
15+
return res.status(201).send(category)
16+
} catch (e) {
17+
return res.status(500).send({
18+
error: e,
19+
})
20+
}
21+
})
22+
23+
module.exports = route

Lecture17/blog-project/routes/api/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ const route = Router()
55
route.use('/articles', require('./articles'))
66
route.use('/comments', require('./comments'))
77
route.use('/users', require('./users'))
8+
route.use('/categories', require('./categories'))
89

910
module.exports = route

Lecture17/blog-project/routes/pages/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
const { Router } = require('express')
22
const { getAllArticles, createArticle } = require('../../controllers/articles')
33
const { getAllUsers } = require('../../controllers/users')
4+
const { getAllCategories } = require('../../controllers/categories')
45

56
const route = Router()
67

78
route.get('/', async (req, res) => {
89

910
const articles = await getAllArticles()
1011
const users = await getAllUsers()
12+
const categories = await getAllCategories()
1113

1214
res.render('home', {
1315
title: 'Home',
14-
articles, users
16+
articles, users, categories
1517
})
1618
})
1719

1820
route.post('/articles', async (req, res) => {
1921
try {
20-
const article = await createArticle(req.body.author_id, req.body.title, req.body.subtitle, req.body.body)
22+
const article = await createArticle(
23+
req.body.author_id,
24+
req.body.title,
25+
req.body.subtitle,
26+
req.body.body,
27+
req.body.category
28+
)
2129
return res.redirect('/')
2230
} catch (e) {
31+
throw e
2332
return res.status(500).send({
2433
error: e,
2534
})

Lecture17/blog-project/views/partials/new_article_form.hbs

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,40 @@
22
<div class="form-group col-12">
33
<label for="title">Heading</label>
44
<input name="title"
5-
type="text" class="form-control"
6-
id="title" placeholder="Heading">
5+
type="text" class="form-control"
6+
id="title" placeholder="Heading">
77
</div>
88

99
<div class="form-group col-12">
1010
<label for="subtitle">Article Subheading</label>
1111
<input name="subtitle"
12-
type="text" class="form-control"
13-
id="subtitle" placeholder="Article Subheading">
12+
type="text" class="form-control"
13+
id="subtitle" placeholder="Article Subheading">
1414
</div>
1515

1616
<div class="form-group col-12">
1717
<label for="body">Article Body</label>
1818
<textarea name="body"
19-
class="form-control" id="body"
20-
placeholder="Write your article here"></textarea>
19+
class="form-control" id="body"
20+
placeholder="Write your article here"></textarea>
2121
</div>
2222

23+
<div class="form-group col-12">
24+
<div>Categories</div>
25+
{{#each categories as |category|}}
26+
<div class="p-2" style="display: inline-block;">
27+
<input name="category" value="{{category.id}}" type="checkbox">
28+
<label class="text-sm-left">{{category.name}}</label>
29+
</div>
30+
31+
{{/each}}
32+
<div class="p-2" style="display: inline-block;">
33+
<input name="category" value="10" type="checkbox">
34+
<label class="text-sm-left">wrong</label>
35+
</div>
36+
</div>
37+
38+
2339
<div class="form-group col-10">
2440
<div class="row">
2541
<label for="author" class="col-2">Author</label>

0 commit comments

Comments
 (0)