Skip to content

Commit bf6f526

Browse files
committed
small update
1 parent 071d82e commit bf6f526

9 files changed

+184
-20
lines changed

package-lock.json

+45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ndse-homeworks",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "index.js",
5+
"main": "src/index.js",
66
"scripts": {
77
"start": "nodemon --watch src src/index.js",
88
"lint": "standard --fix"
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"ejs": "^3.1.8",
1414
"express": "^4.18.2",
15+
"method-override": "^3.0.0",
1516
"multer": "^1.4.5-lts.1",
1617
"nodemon": "^2.0.20",
1718
"uuid": "^9.0.0"

src/book/book.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
const { router } = require('./bookController.js')
22
const { repository: bookRepository } = require('./bookRepository.js')
33
const { model: Book } = require('./bookModel.js')
4-
const { faker } = require('@faker-js/faker')
4+
const { books } = require('./bookFixture')
55

6-
for (const _ of Array.from({ length: 10 })) { // eslint-disable-line no-unused-vars
7-
const book = new Book({
8-
title: faker.random.words(2),
9-
authors: faker.name.fullName(),
10-
description: faker.random.words(10)
11-
})
12-
bookRepository.add(book)
6+
for (const book of books) { // eslint-disable-line no-unused-vars
7+
const bookModel = new Book(book)
8+
bookRepository.add(bookModel)
139
}
1410

1511
module.exports = {

src/book/bookController.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ router.post('/api/books/', upload.single('fileBook'), (req, res) => {
2929
})
3030

3131
router.put('/api/books/:id', upload.single('fileBook'), (req, res) => {
32+
console.log('update book')
3233
const { id } = req.params
3334
if (bookRepository.has(id)) {
34-
const book = new Book({
35+
const book = bookRepository.getById(id)
36+
const updatedBook = bookRepository.update({
37+
...book,
3538
...req.body,
39+
id,
3640
file: req.file
3741
})
38-
bookRepository.update(book)
39-
res.json(book)
42+
res.json(updatedBook)
4043
} else {
4144
res.status(404)
4245
res.json({ status: 404, message: '404 | страница не найдена' })

src/book/bookFixture.js

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// const { faker } = require('@faker-js/faker')
2+
3+
// for (const _ of Array.from({ length: 10 })) { // eslint-disable-line no-unused-vars
4+
// const book = new Book({
5+
// title: faker.random.words(2),
6+
// authors: faker.name.fullName(),
7+
// description: faker.random.words(10)
8+
// })
9+
// bookRepository.add(book)
10+
// }
11+
12+
const books = [
13+
{
14+
id: 'ec528594-ce54-4b06-9979-73856da9a874',
15+
title: 'Volkswagen female',
16+
description: 'coolly Security North green Estate payment Response 24/365 East Reichert',
17+
authors: 'Jody Kunze',
18+
favorite: '',
19+
fileCover: '',
20+
fileName: '',
21+
fileBook: ''
22+
},
23+
{
24+
id: '0254fd03-8cd4-413e-8c18-9c23e5fe301a',
25+
title: 'Metal Wagon',
26+
description: 'port digital associate Cruces below synthesize Rubber Expanded strategize Rand',
27+
authors: 'Jennifer Howell',
28+
favorite: '',
29+
fileCover: '',
30+
fileName: '',
31+
fileBook: ''
32+
},
33+
{
34+
id: '48b81d38-a9b6-4752-83af-2a5e8d168d23',
35+
title: 'Usability middleware',
36+
description: 'perforate morph but Honda Market South Hybrid Northeast tired Coordinator',
37+
authors: 'Robin Lebsack',
38+
favorite: '',
39+
fileCover: '',
40+
fileName: '',
41+
fileBook: ''
42+
},
43+
{
44+
id: '4a86319d-2bbc-4240-bc9d-4dbfa59ad362',
45+
title: 'Steel Francesco',
46+
description: 'compress exploit Frozen Connecticut Wooden Soap male Ball Chips bountiful',
47+
authors: 'Elaine Mills',
48+
favorite: '',
49+
fileCover: '',
50+
fileName: '',
51+
fileBook: ''
52+
},
53+
{
54+
id: 'e0aee0e3-0d1f-4c6c-8ae6-a0cff37b77f0',
55+
title: 'Accountability yuck',
56+
description: 'carbonise Ruthenium yowza gasp program Southeast Southeast Cis Customer farad',
57+
authors: 'Philip Wiza',
58+
favorite: '',
59+
fileCover: '',
60+
fileName: '',
61+
fileBook: ''
62+
},
63+
{
64+
id: '2f85b881-58b0-4be0-a78f-3a1975874dd7',
65+
title: 'whether Bigender',
66+
description: 'bold Mountain impactful haptic statistic supposing 24/7 AI Wooden health',
67+
authors: 'Michael Ruecker',
68+
favorite: '',
69+
fileCover: '',
70+
fileName: '',
71+
fileBook: ''
72+
},
73+
{
74+
id: 'e1f7a140-d1fc-4c69-9c2a-7d6e37c01286',
75+
title: 'steradian over',
76+
description: 'indexing Soap in COM hacking South Web programming Chicken ultimately',
77+
authors: 'Irene Pacocha',
78+
favorite: '',
79+
fileCover: '',
80+
fileName: '',
81+
fileBook: ''
82+
},
83+
{
84+
id: '07b25af0-01d2-472b-9267-5ca119dca46c',
85+
title: 'ipsum Shoes',
86+
description: 'Southeast Games Hybrid South Operative Osinski female tourism JBOD invoice',
87+
authors: 'Anthony Homenick',
88+
favorite: '',
89+
fileCover: '',
90+
fileName: '',
91+
fileBook: ''
92+
},
93+
{
94+
id: '4cd98396-906b-4bde-b06a-404a2c95d2ba',
95+
title: 'roughly deposit',
96+
description: 'Coordinator um Steel Borders female duel schemas Transexual Ferrari Malawi',
97+
authors: 'Randy Boehm II',
98+
favorite: '',
99+
fileCover: '',
100+
fileName: '',
101+
fileBook: ''
102+
},
103+
{
104+
id: '021d1c02-d526-4b8f-a25c-1404f1816b2b',
105+
title: 'Weimann programming',
106+
description: 'North female disprove meantime whoever Northwest Pound volt online harum',
107+
authors: 'Marjorie Jerde',
108+
favorite: '',
109+
fileCover: '',
110+
fileName: '',
111+
fileBook: ''
112+
}
113+
]
114+
115+
module.exports = {
116+
books
117+
}

src/book/bookRepository.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class BookStore {
1919

2020
update (book) {
2121
this.books.set(book.id, book)
22+
return this.books.get(book.id)
2223
}
2324

2425
remove (id) {

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const express = require('express')
2+
const methodOverride = require('method-override')
23
const { router: bookController } = require('./book/book')
34
const { router: userController } = require('./user/user')
45
const app = express()
56
const port = 3000
67

78
app.use(express.urlencoded())
9+
app.use(methodOverride('_method'))
810
app.set('view engine', 'ejs')
911

1012
app.use('/', bookController)

views/book/update.ejs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<%- include('../layouts/layoutStart.ejs') %>
2-
<form action="/api/books/<%= book.id %>" method="POST">
3-
<input type="hidden" name="_method" value="put" />
2+
<form action="/api/books/<%= book.id %>?_method=put" method="POST">
43
<input type="hidden" name="id" value="<%= book.id %>" />
54

65
<h3>Просмотр книги</h3>

views/book/view.ejs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
<form action="/api/books" method="post">
33
<h3>Просмотр книги</h3>
44
<p>
5-
<input type="hidden" name="id" value="<%= book.id %>" />
5+
<input type="hidden" name="id" value="<%= book.id %>" readonly />
66
</p>
77
<p>
8-
<input type="text" placeholder="Название" name="title" value="<%= book.title %>" />
8+
<input type="text" placeholder="Название" name="title" value="<%= book.title %>" readonly />
99
</p>
1010
<p>
11-
<textarea placeholder="Описание" name="description"><%= book.description %></textarea>
11+
<textarea placeholder="Описание" name="description" readonly><%= book.description %></textarea>
1212
</p>
1313
<p>
14-
<input type="text" placeholder="Авторы" name="authors" value="<%= book.authors %>" />
14+
<input type="text" placeholder="Авторы" name="authors" value="<%= book.authors %>" readonly />
1515
</p>
1616
<p>
1717
<span>Обложка: <%= book.fileCover %></span><br>
18-
<input type="file" placeholder="Обложка" name="fileCover" />
18+
<input type="file" placeholder="Обложка" name="fileCover" readonly />
1919
</p>
2020
<p>
2121
<span>Книга: <%= book.fileBook %></span><br>
22-
<input type="file" placeholder="Книга" name="fileBook" />
22+
<input type="file" placeholder="Книга" name="fileBook" readonly />
2323
</p>
2424
</form>
2525
<a href="/books/update/<%= book.id %>">Изменить</a>

0 commit comments

Comments
 (0)