mongoosedbcc 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/comp/README.md +70 -0
- package/comp/bin/cli.js +38 -0
- package/comp/package-lock.json +16503 -0
- package/comp/package.json +45 -0
- package/comp/public/favicon.ico +0 -0
- package/comp/public/index.html +43 -0
- package/comp/public/logo192.png +0 -0
- package/comp/public/logo512.png +0 -0
- package/comp/public/manifest.json +25 -0
- package/comp/public/robots.txt +3 -0
- package/comp/src/App.css +251 -0
- package/comp/src/App.js +30 -0
- package/comp/src/index.css +13 -0
- package/comp/src/index.js +13 -0
- package/comp/src/pages/BookForm.js +115 -0
- package/comp/src/pages/BookList.js +77 -0
- package/comp/src/server.js +43 -0
- package/library-frontend/src/App.js +14 -10
- package/package.json +1 -1
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dbl-mongodb",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"bin": {
|
|
5
|
+
"dbl-mongodb": "./bin/cli.js"
|
|
6
|
+
},
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@testing-library/dom": "^10.4.1",
|
|
9
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
10
|
+
"@testing-library/react": "^16.3.2",
|
|
11
|
+
"@testing-library/user-event": "^13.5.0",
|
|
12
|
+
"cors": "^2.8.6",
|
|
13
|
+
"express": "^5.2.1",
|
|
14
|
+
"mongoose": "^9.3.1",
|
|
15
|
+
"react": "^19.2.4",
|
|
16
|
+
"react-dom": "^19.2.4",
|
|
17
|
+
"react-router-dom": "^7.13.1",
|
|
18
|
+
"react-scripts": "5.0.1",
|
|
19
|
+
"web-vitals": "^2.1.4"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"start": "react-scripts start",
|
|
23
|
+
"build": "react-scripts build",
|
|
24
|
+
"test": "react-scripts test",
|
|
25
|
+
"eject": "react-scripts eject"
|
|
26
|
+
},
|
|
27
|
+
"eslintConfig": {
|
|
28
|
+
"extends": [
|
|
29
|
+
"react-app",
|
|
30
|
+
"react-app/jest"
|
|
31
|
+
]
|
|
32
|
+
},
|
|
33
|
+
"browserslist": {
|
|
34
|
+
"production": [
|
|
35
|
+
">0.2%",
|
|
36
|
+
"not dead",
|
|
37
|
+
"not op_mini all"
|
|
38
|
+
],
|
|
39
|
+
"development": [
|
|
40
|
+
"last 1 chrome version",
|
|
41
|
+
"last 1 firefox version",
|
|
42
|
+
"last 1 safari version"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
7
|
+
<meta name="theme-color" content="#000000" />
|
|
8
|
+
<meta
|
|
9
|
+
name="description"
|
|
10
|
+
content="Web site created using create-react-app"
|
|
11
|
+
/>
|
|
12
|
+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
|
|
13
|
+
<!--
|
|
14
|
+
manifest.json provides metadata used when your web app is installed on a
|
|
15
|
+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
|
16
|
+
-->
|
|
17
|
+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
|
18
|
+
<!--
|
|
19
|
+
Notice the use of %PUBLIC_URL% in the tags above.
|
|
20
|
+
It will be replaced with the URL of the `public` folder during the build.
|
|
21
|
+
Only files inside the `public` folder can be referenced from the HTML.
|
|
22
|
+
|
|
23
|
+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
|
24
|
+
work correctly both with client-side routing and a non-root public URL.
|
|
25
|
+
Learn how to configure a non-root public URL by running `npm run build`.
|
|
26
|
+
-->
|
|
27
|
+
<title>React App</title>
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<noscript>You need to enable JavaScript to run this app.</noscript>
|
|
31
|
+
<div id="root"></div>
|
|
32
|
+
<!--
|
|
33
|
+
This HTML file is a template.
|
|
34
|
+
If you open it directly in the browser, you will see an empty page.
|
|
35
|
+
|
|
36
|
+
You can add webfonts, meta tags, or analytics to this file.
|
|
37
|
+
The build step will place the bundled scripts into the <body> tag.
|
|
38
|
+
|
|
39
|
+
To begin the development, run `npm start` or `yarn start`.
|
|
40
|
+
To create a production bundle, use `npm run build` or `yarn build`.
|
|
41
|
+
-->
|
|
42
|
+
</body>
|
|
43
|
+
</html>
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"short_name": "React App",
|
|
3
|
+
"name": "Create React App Sample",
|
|
4
|
+
"icons": [
|
|
5
|
+
{
|
|
6
|
+
"src": "favicon.ico",
|
|
7
|
+
"sizes": "64x64 32x32 24x24 16x16",
|
|
8
|
+
"type": "image/x-icon"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"src": "logo192.png",
|
|
12
|
+
"type": "image/png",
|
|
13
|
+
"sizes": "192x192"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"src": "logo512.png",
|
|
17
|
+
"type": "image/png",
|
|
18
|
+
"sizes": "512x512"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"start_url": ".",
|
|
22
|
+
"display": "standalone",
|
|
23
|
+
"theme_color": "#000000",
|
|
24
|
+
"background_color": "#ffffff"
|
|
25
|
+
}
|
package/comp/src/App.css
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/* --- CSS Fonts --- */
|
|
2
|
+
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
|
|
3
|
+
|
|
4
|
+
:root {
|
|
5
|
+
--primary-color: #000000;
|
|
6
|
+
--primary-hover: #272323;
|
|
7
|
+
--secondary-color: #f59e0b;
|
|
8
|
+
--danger-color: #ef4444;
|
|
9
|
+
--text-dark: #1f2937;
|
|
10
|
+
--text-light: #ffffff;
|
|
11
|
+
--border-color: #e5e7eb;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
* {
|
|
15
|
+
box-sizing: border-box;
|
|
16
|
+
margin: 0;
|
|
17
|
+
padding: 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* --- CSS Background & CSS Fonts --- */
|
|
21
|
+
body {
|
|
22
|
+
font-family: 'Poppins', sans-serif;
|
|
23
|
+
/* Minimal background gradient */
|
|
24
|
+
background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
|
|
25
|
+
color: var(--text-dark);
|
|
26
|
+
min-height: 100vh;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.App {
|
|
30
|
+
max-width: 900px;
|
|
31
|
+
margin: 0 auto;
|
|
32
|
+
padding: 20px 15px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.header {
|
|
36
|
+
text-align: center;
|
|
37
|
+
margin-bottom: 25px;
|
|
38
|
+
padding-bottom: 15px;
|
|
39
|
+
border-bottom: 2px solid var(--border-color);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.header h1 {
|
|
43
|
+
color: var(--primary-color);
|
|
44
|
+
font-weight: 600;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.container {
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-direction: column;
|
|
50
|
+
gap: 25px;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.form-container,
|
|
54
|
+
.list-container {
|
|
55
|
+
background: var(--text-light);
|
|
56
|
+
padding: 20px;
|
|
57
|
+
border-radius: 10px;
|
|
58
|
+
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
h2 {
|
|
62
|
+
margin-bottom: 15px;
|
|
63
|
+
color: #374151;
|
|
64
|
+
font-weight: 600;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.book-form {
|
|
68
|
+
display: flex;
|
|
69
|
+
flex-direction: column;
|
|
70
|
+
gap: 12px;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.book-form input {
|
|
74
|
+
padding: 10px 14px;
|
|
75
|
+
font-family: 'Poppins', sans-serif;
|
|
76
|
+
font-size: 15px;
|
|
77
|
+
border: 1px solid var(--border-color);
|
|
78
|
+
border-radius: 6px;
|
|
79
|
+
outline: none;
|
|
80
|
+
transition: border-color 0.2s;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.book-form input:focus {
|
|
84
|
+
border-color: var(--primary-color);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.form-actions {
|
|
88
|
+
display: flex;
|
|
89
|
+
gap: 10px;
|
|
90
|
+
margin-top: 5px;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
button {
|
|
94
|
+
font-family: 'Poppins', sans-serif;
|
|
95
|
+
padding: 10px 16px;
|
|
96
|
+
font-size: 14px;
|
|
97
|
+
font-weight: 600;
|
|
98
|
+
border: none;
|
|
99
|
+
border-radius: 6px;
|
|
100
|
+
cursor: pointer;
|
|
101
|
+
transition: all 0.2s ease;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.submit-btn {
|
|
105
|
+
background-color: var(--primary-color);
|
|
106
|
+
color: var(--text-light);
|
|
107
|
+
flex: 1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.submit-btn:hover {
|
|
111
|
+
background-color: var(--primary-hover);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.cancel-btn {
|
|
115
|
+
background-color: #d1d5db;
|
|
116
|
+
color: var(--text-dark);
|
|
117
|
+
flex: 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.cancel-btn:hover {
|
|
121
|
+
background-color: #9ca3af;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* --- CSS Tables --- */
|
|
125
|
+
.table-responsive {
|
|
126
|
+
width: 100%;
|
|
127
|
+
overflow-x: auto;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.books-table {
|
|
131
|
+
width: 100%;
|
|
132
|
+
border-collapse: collapse;
|
|
133
|
+
margin-top: 10px;
|
|
134
|
+
background-color: var(--text-light);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.books-table th,
|
|
138
|
+
.books-table td {
|
|
139
|
+
padding: 12px 15px;
|
|
140
|
+
text-align: left;
|
|
141
|
+
border-bottom: 1px solid var(--border-color);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.books-table th {
|
|
145
|
+
background-color: #f9fafb;
|
|
146
|
+
font-weight: 600;
|
|
147
|
+
color: #4b5563;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.books-table tr:hover {
|
|
151
|
+
background-color: #f3f4f6;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.action-cell {
|
|
155
|
+
display: flex;
|
|
156
|
+
gap: 8px;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.edit-btn {
|
|
160
|
+
background-color: var(--secondary-color);
|
|
161
|
+
color: var(--text-dark);
|
|
162
|
+
padding: 6px 12px;
|
|
163
|
+
font-size: 13px;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.edit-btn:hover {
|
|
167
|
+
background-color: #d97706;
|
|
168
|
+
color: var(--text-light);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.delete-btn {
|
|
172
|
+
background-color: var(--danger-color);
|
|
173
|
+
color: var(--text-light);
|
|
174
|
+
padding: 6px 12px;
|
|
175
|
+
font-size: 13px;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.delete-btn:hover {
|
|
179
|
+
background-color: #b91c1c;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/* --- CSS Links --- */
|
|
183
|
+
.footer {
|
|
184
|
+
text-align: center;
|
|
185
|
+
margin-top: 30px;
|
|
186
|
+
font-size: 14px;
|
|
187
|
+
color: #6b7280;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.custom-link {
|
|
191
|
+
color: var(--primary-color);
|
|
192
|
+
text-decoration: none;
|
|
193
|
+
font-weight: 600;
|
|
194
|
+
transition: color 0.2s ease, text-decoration 0.2s ease;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
.custom-link:hover {
|
|
198
|
+
color: var(--primary-hover);
|
|
199
|
+
text-decoration: underline;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* --- Responsive Web Design --- */
|
|
203
|
+
@media (max-width: 600px) {
|
|
204
|
+
.books-table thead {
|
|
205
|
+
display: none;
|
|
206
|
+
/* Hide table headers on small screens */
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
.books-table,
|
|
210
|
+
.books-table tbody,
|
|
211
|
+
.books-table tr,
|
|
212
|
+
.books-table td {
|
|
213
|
+
display: block;
|
|
214
|
+
width: 100%;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
.books-table tr {
|
|
218
|
+
margin-bottom: 15px;
|
|
219
|
+
border: 1px solid var(--border-color);
|
|
220
|
+
border-radius: 8px;
|
|
221
|
+
padding: 10px;
|
|
222
|
+
background: #fff;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
.books-table td {
|
|
226
|
+
text-align: right;
|
|
227
|
+
padding: 8px;
|
|
228
|
+
position: relative;
|
|
229
|
+
border-bottom: none;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* Add data labels before each cell content on mobile */
|
|
233
|
+
.books-table td::before {
|
|
234
|
+
content: attr(data-label);
|
|
235
|
+
position: absolute;
|
|
236
|
+
left: 10px;
|
|
237
|
+
width: calc(50% - 20px);
|
|
238
|
+
text-align: left;
|
|
239
|
+
font-weight: 600;
|
|
240
|
+
color: #4b5563;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.action-cell {
|
|
244
|
+
justify-content: flex-end;
|
|
245
|
+
margin-top: 5px;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
.App {
|
|
249
|
+
padding: 10px;
|
|
250
|
+
}
|
|
251
|
+
}
|
package/comp/src/App.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
|
3
|
+
import './App.css';
|
|
4
|
+
|
|
5
|
+
import BookList from './pages/BookList';
|
|
6
|
+
import BookForm from './pages/BookForm';
|
|
7
|
+
|
|
8
|
+
function App() {
|
|
9
|
+
return (
|
|
10
|
+
<BrowserRouter>
|
|
11
|
+
<div className="App">
|
|
12
|
+
<header className="header" style={{ borderBottom: 'none', marginBottom: '10px' }}>
|
|
13
|
+
<Link to="/" style={{ textDecoration: 'none' }}>
|
|
14
|
+
<h1>Library</h1>
|
|
15
|
+
</Link>
|
|
16
|
+
</header>
|
|
17
|
+
|
|
18
|
+
<main className="container">
|
|
19
|
+
<Routes>
|
|
20
|
+
<Route path="/" element={<BookList />} />
|
|
21
|
+
<Route path="/add" element={<BookForm />} />
|
|
22
|
+
<Route path="/edit/:id" element={<BookForm />} />
|
|
23
|
+
</Routes>
|
|
24
|
+
</main>
|
|
25
|
+
</div>
|
|
26
|
+
</BrowserRouter>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default App;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
body {
|
|
2
|
+
margin: 0;
|
|
3
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
|
|
4
|
+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
|
|
5
|
+
sans-serif;
|
|
6
|
+
-webkit-font-smoothing: antialiased;
|
|
7
|
+
-moz-osx-font-smoothing: grayscale;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
code {
|
|
11
|
+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
|
|
12
|
+
monospace;
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import ReactDOM from 'react-dom/client';
|
|
3
|
+
import './index.css';
|
|
4
|
+
import App from './App';
|
|
5
|
+
|
|
6
|
+
const root = ReactDOM.createRoot(document.getElementById('root'));
|
|
7
|
+
root.render(
|
|
8
|
+
<React.StrictMode>
|
|
9
|
+
<App />
|
|
10
|
+
</React.StrictMode>
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import { useNavigate, useParams } from 'react-router-dom';
|
|
3
|
+
|
|
4
|
+
const API_URL = 'http://localhost:3001/api/books';
|
|
5
|
+
|
|
6
|
+
function BookForm() {
|
|
7
|
+
const [formData, setFormData] = useState({ title: '', author: '', year: '' });
|
|
8
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
9
|
+
|
|
10
|
+
const navigate = useNavigate();
|
|
11
|
+
const { id } = useParams();
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (id) {
|
|
15
|
+
setIsEditing(true);
|
|
16
|
+
fetchBookDetails();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}, [id]);
|
|
20
|
+
|
|
21
|
+
const fetchBookDetails = async () => {
|
|
22
|
+
try {
|
|
23
|
+
const response = await fetch(API_URL);
|
|
24
|
+
const data = await response.json();
|
|
25
|
+
const bookToEdit = data.find(b => b._id === id);
|
|
26
|
+
if (bookToEdit) {
|
|
27
|
+
setFormData({
|
|
28
|
+
title: bookToEdit.title,
|
|
29
|
+
author: bookToEdit.author,
|
|
30
|
+
year: bookToEdit.year || ''
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error('Error fetching book details:', error);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const handleChange = (e) => {
|
|
39
|
+
setFormData({ ...formData, [e.target.name]: e.target.value });
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const handleSubmit = async (e) => {
|
|
43
|
+
e.preventDefault();
|
|
44
|
+
if (!formData.title || !formData.author) return;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
let response;
|
|
48
|
+
if (isEditing) {
|
|
49
|
+
response = await fetch(`${API_URL}/${id}`, {
|
|
50
|
+
method: 'PUT',
|
|
51
|
+
headers: { 'Content-Type': 'application/json' },
|
|
52
|
+
body: JSON.stringify(formData)
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
response = await fetch(API_URL, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
headers: { 'Content-Type': 'application/json' },
|
|
58
|
+
body: JSON.stringify(formData)
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (response.ok) {
|
|
63
|
+
navigate('/');
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error('Error submitting form:', error);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div className="form-container">
|
|
72
|
+
<h2 style={{ borderBottom: 'none', padding: 0 }}>{isEditing ? 'Edit Book' : 'Add New Book'}</h2>
|
|
73
|
+
<form onSubmit={handleSubmit} className="book-form">
|
|
74
|
+
<input
|
|
75
|
+
type="text"
|
|
76
|
+
name="title"
|
|
77
|
+
placeholder="Book Title"
|
|
78
|
+
value={formData.title}
|
|
79
|
+
onChange={handleChange}
|
|
80
|
+
required
|
|
81
|
+
/>
|
|
82
|
+
<input
|
|
83
|
+
type="text"
|
|
84
|
+
name="author"
|
|
85
|
+
placeholder="Author"
|
|
86
|
+
value={formData.author}
|
|
87
|
+
onChange={handleChange}
|
|
88
|
+
required
|
|
89
|
+
/>
|
|
90
|
+
<input
|
|
91
|
+
type="number"
|
|
92
|
+
name="year"
|
|
93
|
+
placeholder="Publication Year"
|
|
94
|
+
value={formData.year}
|
|
95
|
+
onChange={handleChange}
|
|
96
|
+
/>
|
|
97
|
+
<div className="form-actions">
|
|
98
|
+
<button type="submit" className="submit-btn" style={{ flex: 1 }}>
|
|
99
|
+
{isEditing ? 'Update Book' : 'Submit'}
|
|
100
|
+
</button>
|
|
101
|
+
<button
|
|
102
|
+
type="button"
|
|
103
|
+
className="cancel-btn"
|
|
104
|
+
style={{ flex: 1 }}
|
|
105
|
+
onClick={() => navigate('/')}
|
|
106
|
+
>
|
|
107
|
+
Cancel
|
|
108
|
+
</button>
|
|
109
|
+
</div>
|
|
110
|
+
</form>
|
|
111
|
+
</div>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default BookForm;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import { useNavigate } from 'react-router-dom';
|
|
3
|
+
|
|
4
|
+
const API_URL = 'http://localhost:3001/api/books';
|
|
5
|
+
|
|
6
|
+
function BookList() {
|
|
7
|
+
const [books, setBooks] = useState([]);
|
|
8
|
+
const navigate = useNavigate();
|
|
9
|
+
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
fetchBooks();
|
|
12
|
+
}, []);
|
|
13
|
+
|
|
14
|
+
const fetchBooks = async () => {
|
|
15
|
+
try {
|
|
16
|
+
const response = await fetch(API_URL);
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
setBooks(data);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
console.error('Error fetching books:', error);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const handleDelete = async (id) => {
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(`${API_URL}/${id}`, { method: 'DELETE' });
|
|
27
|
+
if (response.ok) {
|
|
28
|
+
fetchBooks();
|
|
29
|
+
}
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('Error deleting book:', error);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div className="list-container">
|
|
37
|
+
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '15px' }}>
|
|
38
|
+
<h2 style={{ borderBottom: 'none', padding: 0, margin: 0 }}>Collection</h2>
|
|
39
|
+
<button onClick={() => navigate('/add')} className="submit-btn" style={{ margin: 0 }}>
|
|
40
|
+
+ Add Book
|
|
41
|
+
</button>
|
|
42
|
+
</div>
|
|
43
|
+
|
|
44
|
+
{books.length === 0 ? (
|
|
45
|
+
<p>No books available yet.</p>
|
|
46
|
+
) : (
|
|
47
|
+
<div className="table-responsive">
|
|
48
|
+
<table className="books-table">
|
|
49
|
+
<thead>
|
|
50
|
+
<tr>
|
|
51
|
+
<th>Title</th>
|
|
52
|
+
<th>Author</th>
|
|
53
|
+
<th>Year</th>
|
|
54
|
+
<th>Actions</th>
|
|
55
|
+
</tr>
|
|
56
|
+
</thead>
|
|
57
|
+
<tbody>
|
|
58
|
+
{books.map(book => (
|
|
59
|
+
<tr key={book._id}>
|
|
60
|
+
<td data-label="Title">{book.title}</td>
|
|
61
|
+
<td data-label="Author">{book.author}</td>
|
|
62
|
+
<td data-label="Year">{book.year || '-'}</td>
|
|
63
|
+
<td data-label="Actions" className="action-cell">
|
|
64
|
+
<button onClick={() => navigate(`/edit/${book._id}`)} className="edit-btn">Edit</button>
|
|
65
|
+
<button onClick={() => handleDelete(book._id)} className="delete-btn">Delete</button>
|
|
66
|
+
</td>
|
|
67
|
+
</tr>
|
|
68
|
+
))}
|
|
69
|
+
</tbody>
|
|
70
|
+
</table>
|
|
71
|
+
</div>
|
|
72
|
+
)}
|
|
73
|
+
</div>
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default BookList;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const mongoose = require('mongoose');
|
|
3
|
+
const cors = require('cors');
|
|
4
|
+
|
|
5
|
+
const app = express();
|
|
6
|
+
app.use(cors());
|
|
7
|
+
app.use(express.json());
|
|
8
|
+
|
|
9
|
+
mongoose.connect('mongodb://127.0.0.1:27017/crud_app')
|
|
10
|
+
.then(() => console.log('MongoDB Connected'))
|
|
11
|
+
.catch(err => console.log('Connection Error:', err));
|
|
12
|
+
|
|
13
|
+
const Book = mongoose.model('Book', new mongoose.Schema({
|
|
14
|
+
title: { type: String, required: true },
|
|
15
|
+
author: { type: String, required: true },
|
|
16
|
+
year: { type: Number }
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
app.get('/api/books', async (req, res) => {
|
|
20
|
+
try { res.json(await Book.find()); }
|
|
21
|
+
catch (e) { res.status(500).json({ error: e.message }); }
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
app.post('/api/books', async (req, res) => {
|
|
25
|
+
try { res.status(201).json(await new Book(req.body).save()); }
|
|
26
|
+
catch (e) { res.status(400).json({ error: e.message }); }
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
app.put('/api/books/:id', async (req, res) => {
|
|
30
|
+
try {
|
|
31
|
+
const book = await Book.findByIdAndUpdate(req.params.id, req.body, { new: true });
|
|
32
|
+
book ? res.json(book) : res.status(404).json({ error: 'Not found' });
|
|
33
|
+
} catch (e) { res.status(400).json({ error: e.message }); }
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
app.delete('/api/books/:id', async (req, res) => {
|
|
37
|
+
try {
|
|
38
|
+
const book = await Book.findByIdAndDelete(req.params.id);
|
|
39
|
+
book ? res.json({ message: 'Deleted' }) : res.status(404).json({ error: 'Not found' });
|
|
40
|
+
} catch (e) { res.status(500).json({ error: e.message }); }
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
app.listen(3001, () => console.log('Running on port 3001'));
|