Skip to content

Commit 6f24039

Browse files
creating articles
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 7c03d3b commit 6f24039

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed
+18-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
const { Router } = require('express')
2-
const { getAllArticles } = require('../../controllers/articles')
2+
const { getAllArticles, createArticle } = require('../../controllers/articles')
3+
const { getAllUsers } = require('../../controllers/users')
34

45
const route = Router()
56

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

89
const articles = await getAllArticles()
10+
const users = await getAllUsers()
11+
912
res.render('home', {
1013
title: 'Home',
11-
articles,
14+
articles, users
1215
})
1316
})
1417

18+
route.post('/articles', async (req, res) => {
19+
try {
20+
const article = await createArticle(req.body.author_id, req.body.title, req.body.subtitle, req.body.body)
21+
return res.redirect('/')
22+
} catch (e) {
23+
return res.status(500).send({
24+
error: e,
25+
})
26+
}
27+
})
28+
1529
route.get('/article', (req, res) => {
1630
res.render('article', { title: 'Article' })
1731
})
1832

33+
route.use('/users', require('./users'))
34+
1935
module.exports = route
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { Router } = require('express')
2+
const { getAllUsers, createUser } = require('../../controllers/users')
3+
4+
const route = Router()
5+
6+
route.get('/', async (req, res) => {
7+
8+
const users = await getAllUsers()
9+
res.render('users', {
10+
title: 'Users',
11+
users
12+
})
13+
})
14+
15+
route.post('/', async (req, res) => {
16+
try {
17+
const user = await createUser(req.body.name, req.body.email)
18+
return res.redirect('/users')
19+
} catch (e) {
20+
return res.status(500).send({
21+
error: e,
22+
})
23+
}
24+
})
25+
26+
module.exports = route

Lecture17/blog-project/views/home.hbs

+8-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
<div class="container">
99

10-
<h1 class="text-center"> Articles </h1>
10+
<h1 class="text-center"> {{title}} </h1>
1111

12+
13+
{{!-- New Article Form --}}
14+
<h2>Add an Article</h2>
15+
{{> new_article_form}}
16+
17+
{{!-- Article List --}}
18+
<h2> Read Articles </h2>
1219
{{#each articles as |article|}}
1320
{{> article_card article}}
1421
{{/each}}
1522

1623
</div>
17-
18-
<p>This is a blog</p>
1924
</body>
2025
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<form class="row" action="/articles" method="POST">
2+
<div class="form-group col-12">
3+
<label for="title">Heading</label>
4+
<input name="title"
5+
type="text" class="form-control"
6+
id="title" placeholder="Heading">
7+
</div>
8+
9+
<div class="form-group col-12">
10+
<label for="subtitle">Article Subheading</label>
11+
<input name="subtitle"
12+
type="text" class="form-control"
13+
id="subtitle" placeholder="Article Subheading">
14+
</div>
15+
16+
<div class="form-group col-12">
17+
<label for="body">Article Body</label>
18+
<textarea name="body"
19+
class="form-control" id="body"
20+
placeholder="Write your article here"></textarea>
21+
</div>
22+
23+
<div class="form-group col-10">
24+
<div class="row">
25+
<label for="author" class="col-2">Author</label>
26+
<select name="author_id" class="custom-select col-8" id="author">
27+
{{#each users as |user|}}
28+
<option value={{user.id}}>{{user.name}}</option>
29+
{{/each}}
30+
</select>
31+
</div>
32+
33+
</div>
34+
35+
36+
<div class="col-2" style="display: flex; align-items: end">
37+
<button type="submit" class="btn btn-primary mb-3">Submit</button>
38+
</div>
39+
40+
41+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<form class="row" action="/users" method="POST">
2+
<div class="form-group col-5">
3+
<label for="name">Enter a name</label>
4+
<input name="name" type="text" class="form-control" id="name" placeholder="User's Name">
5+
</div>
6+
7+
<div class="form-group col-5">
8+
<label for="email">Email address</label>
9+
<input name="email" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
10+
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
11+
</div>
12+
13+
<div class="col" style="display: flex; align-items: center">
14+
<button type="submit" class="btn btn-primary mb-2">Submit</button>
15+
</div>
16+
17+
</form>
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
{{> head}}
4+
<body>
5+
6+
{{> navbar}}
7+
8+
<div class="container">
9+
10+
<h1 class="text-center"> {{title}} </h1>
11+
12+
{{!-- New User Form --}}
13+
{{> new_user_form}}
14+
15+
{{!-- List of Users --}}
16+
<div class="row">
17+
<table class="table">
18+
<thead>
19+
<tr>
20+
<th>ID</th>
21+
<th>Name</th>
22+
<th>Email</th>
23+
</tr>
24+
</thead>
25+
{{#each users as |user|}}
26+
<tr>
27+
<td>{{user.id}}</td>
28+
<td>{{user.name}}</td>
29+
<td>{{user.email}}</td>
30+
</tr>
31+
{{/each}}
32+
</table>
33+
</div>
34+
35+
</div>
36+
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)