create-autostack 1.0.0
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/README.md +237 -0
- package/bin/index.js +675 -0
- package/lib/PackageManagerClass.js +185 -0
- package/package.json +55 -0
- package/templates/assets/fastapi.svg +1 -0
- package/templates/assets/flask.svg +1 -0
- package/templates/assets/mongodb.svg +1 -0
- package/templates/assets/mysql.svg +1 -0
- package/templates/assets/nodejs.svg +1 -0
- package/templates/assets/postgresql.svg +1 -0
- package/templates/backend/FASTAPI/MONGODB/.env +1 -0
- package/templates/backend/FASTAPI/MONGODB/config.py +26 -0
- package/templates/backend/FASTAPI/MONGODB/main.py +32 -0
- package/templates/backend/FASTAPI/MONGODB/models.py +56 -0
- package/templates/backend/FASTAPI/MONGODB/requirements.txt +6 -0
- package/templates/backend/FASTAPI/MONGODB/routes.py +189 -0
- package/templates/backend/FASTAPI/MONGODB/schemas.py +31 -0
- package/templates/backend/FASTAPI/MYSQL/.env +5 -0
- package/templates/backend/FASTAPI/MYSQL/config.py +29 -0
- package/templates/backend/FASTAPI/MYSQL/main.py +32 -0
- package/templates/backend/FASTAPI/MYSQL/models.py +31 -0
- package/templates/backend/FASTAPI/MYSQL/requirements.txt +6 -0
- package/templates/backend/FASTAPI/MYSQL/routes.py +180 -0
- package/templates/backend/FASTAPI/MYSQL/schemas.py +36 -0
- package/templates/backend/FASTAPI/POSTGRESQL/.env +5 -0
- package/templates/backend/FASTAPI/POSTGRESQL/config.py +29 -0
- package/templates/backend/FASTAPI/POSTGRESQL/main.py +32 -0
- package/templates/backend/FASTAPI/POSTGRESQL/models.py +31 -0
- package/templates/backend/FASTAPI/POSTGRESQL/requirements.txt +6 -0
- package/templates/backend/FASTAPI/POSTGRESQL/routes.py +180 -0
- package/templates/backend/FASTAPI/POSTGRESQL/schemas.py +36 -0
- package/templates/backend/FLASK/MONGODB/.env +6 -0
- package/templates/backend/FLASK/MONGODB/config.py +27 -0
- package/templates/backend/FLASK/MONGODB/main.py +5 -0
- package/templates/backend/FLASK/MONGODB/models.py +16 -0
- package/templates/backend/FLASK/MONGODB/requirements.txt +6 -0
- package/templates/backend/FLASK/MONGODB/routes.py +137 -0
- package/templates/backend/FLASK/MYSQL/.env +7 -0
- package/templates/backend/FLASK/MYSQL/config.py +24 -0
- package/templates/backend/FLASK/MYSQL/main.py +8 -0
- package/templates/backend/FLASK/MYSQL/models.py +26 -0
- package/templates/backend/FLASK/MYSQL/requirements.txt +6 -0
- package/templates/backend/FLASK/MYSQL/routes.py +135 -0
- package/templates/backend/FLASK/POSTGRESQL/.env +5 -0
- package/templates/backend/FLASK/POSTGRESQL/config.py +24 -0
- package/templates/backend/FLASK/POSTGRESQL/main.py +7 -0
- package/templates/backend/FLASK/POSTGRESQL/models.py +26 -0
- package/templates/backend/FLASK/POSTGRESQL/requirements.txt +6 -0
- package/templates/backend/FLASK/POSTGRESQL/routes.py +134 -0
- package/templates/backend/NODEJS/MONGODB/config/dbConn.js +11 -0
- package/templates/backend/NODEJS/MONGODB/controllers/noteController.js +79 -0
- package/templates/backend/NODEJS/MONGODB/controllers/userController.js +79 -0
- package/templates/backend/NODEJS/MONGODB/index.js +39 -0
- package/templates/backend/NODEJS/MONGODB/models/noteModel.js +17 -0
- package/templates/backend/NODEJS/MONGODB/models/userModel.js +17 -0
- package/templates/backend/NODEJS/MYSQL/config/dbConn.js +20 -0
- package/templates/backend/NODEJS/MYSQL/controllers/noteController.js +97 -0
- package/templates/backend/NODEJS/MYSQL/controllers/userController.js +98 -0
- package/templates/backend/NODEJS/MYSQL/index.js +39 -0
- package/templates/backend/NODEJS/MYSQL/models.txt +3 -0
- package/templates/backend/NODEJS/POSTGRESQL/config/dbConn.js +28 -0
- package/templates/backend/NODEJS/POSTGRESQL/controllers/noteController.js +91 -0
- package/templates/backend/NODEJS/POSTGRESQL/controllers/userController.js +92 -0
- package/templates/backend/NODEJS/POSTGRESQL/index.js +39 -0
- package/templates/backend/NODEJS/POSTGRESQL/models.txt +3 -0
- package/templates/backend/NODEJS/common/middleware/upstash.js +12 -0
- package/templates/backend/NODEJS/common/routes/noteRoutes.js +22 -0
- package/templates/backend/NODEJS/common/routes/userRoutes.js +22 -0
- package/templates/frontend/PREACT/src/app.jsx +103 -0
- package/templates/frontend/PREACT/vite.config.js +8 -0
- package/templates/frontend/PREACT-TS/src/app.tsx +112 -0
- package/templates/frontend/PREACT-TS/vite.config.ts +8 -0
- package/templates/frontend/REACT/src/App.css +27 -0
- package/templates/frontend/REACT/src/App.jsx +105 -0
- package/templates/frontend/REACT/vite.config.js +8 -0
- package/templates/frontend/REACT-SWC/src/App.css +27 -0
- package/templates/frontend/REACT-SWC/src/App.jsx +105 -0
- package/templates/frontend/REACT-SWC/vite.config.js +8 -0
- package/templates/frontend/REACT-SWC-TS/src/App.css +27 -0
- package/templates/frontend/REACT-SWC-TS/src/App.tsx +114 -0
- package/templates/frontend/REACT-SWC-TS/vite.config.ts +8 -0
- package/templates/frontend/REACT-TS/src/App.css +27 -0
- package/templates/frontend/REACT-TS/src/App.tsx +114 -0
- package/templates/frontend/REACT-TS/vite.config.ts +8 -0
- package/templates/frontend/SOLID/src/App.jsx +104 -0
- package/templates/frontend/SOLID/vite.config.js +7 -0
- package/templates/frontend/SOLID-TS/src/App.tsx +116 -0
- package/templates/frontend/SOLID-TS/vite.config.ts +7 -0
- package/templates/frontend/SVELTE/src/App.svelte +104 -0
- package/templates/frontend/SVELTE/vite.config.js +8 -0
- package/templates/frontend/SVELTE-TS/src/App.svelte +116 -0
- package/templates/frontend/SVELTE-TS/vite.config.ts +8 -0
- package/templates/frontend/VUE/src/App.vue +96 -0
- package/templates/frontend/VUE/vite.config.js +8 -0
- package/templates/frontend/VUE-TS/src/App.vue +112 -0
- package/templates/frontend/VUE-TS/vite.config.ts +8 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { User } from "../models/userModel.js"
|
|
2
|
+
import mongoose from "mongoose"
|
|
3
|
+
|
|
4
|
+
// Get All Users
|
|
5
|
+
export const getAllUsers = async (req, res) => {
|
|
6
|
+
try {
|
|
7
|
+
const users = await Note.find();
|
|
8
|
+
res.status(200).json(users);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
res.status(500).json({message: "Error getting users", error: error.message});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Get A Single User
|
|
15
|
+
export const getUserByID = async (req, res) => {
|
|
16
|
+
try {
|
|
17
|
+
const { id } = req.params;
|
|
18
|
+
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
19
|
+
return res.status(404).json({message: "User not found!"})
|
|
20
|
+
}
|
|
21
|
+
const user = await User.findById(id);
|
|
22
|
+
if (!user) {
|
|
23
|
+
return res.status(404).json({message: "User not found!"})
|
|
24
|
+
}
|
|
25
|
+
res.status(200).json(user);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
res.status(500).json({message: "Error finding user", error: error.message});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Create A User
|
|
32
|
+
export const createUser = async (req, res) => {
|
|
33
|
+
try {
|
|
34
|
+
const {username, email} = req.body;
|
|
35
|
+
if (!username || !email) {
|
|
36
|
+
return res.status(400).json({message: "Please fill in all the fields"})
|
|
37
|
+
}
|
|
38
|
+
const newUser = new User({title, content});
|
|
39
|
+
const savedUser = await newUser.save();
|
|
40
|
+
res.status(201).json({message: "User created successfully", createdNote: savedUser});
|
|
41
|
+
} catch (error) {
|
|
42
|
+
res.status(500).json({message: "Error creating user", error: error.message});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
// Update User
|
|
48
|
+
export const updateUser = async (req, res) => {
|
|
49
|
+
try {
|
|
50
|
+
const {username, email} = req.body;
|
|
51
|
+
const updatedUser = await User.findByIdAndUpdate(
|
|
52
|
+
req.params.id,
|
|
53
|
+
{
|
|
54
|
+
username,
|
|
55
|
+
email
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
new: true // Returns the updated user
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
if (!updatedUser) return res.status(404).json({message: "User not found!"});
|
|
62
|
+
res.status(200).json({message: "User updated successfully", updatedUser: updatedUser});
|
|
63
|
+
} catch (error) {
|
|
64
|
+
res.status(500).json({message: "Error updating user", error: error.message});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// Delete User
|
|
70
|
+
export const deleteUser = async (req, res) => {
|
|
71
|
+
try {
|
|
72
|
+
const deletedUser = await User.findByIdAndDelete(req.params.id);
|
|
73
|
+
if (!deletedUser) return res.status(404).json({message: "User not found"});
|
|
74
|
+
res.status(200).json({message: "User deleted successfully", deletedUser});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
res.status(500).json({message: "Error deleting user", error: error.message});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { connectDB } from './config/dbConn.js';
|
|
3
|
+
import dotenv from 'dotenv'
|
|
4
|
+
import noteRoutes from './routes/noteRoutes.js';
|
|
5
|
+
import userRoutes from './routes/userRoutes.js';
|
|
6
|
+
// import rateLimiter from './middleware/upstash.js'; // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
7
|
+
import cors from 'cors';
|
|
8
|
+
|
|
9
|
+
dotenv.config(); // This will help you read the env variable. Else, undefined.
|
|
10
|
+
|
|
11
|
+
const app = express();
|
|
12
|
+
const PORT = process.env.PORT || 3000;
|
|
13
|
+
|
|
14
|
+
// Middleware
|
|
15
|
+
app.use(express.json());
|
|
16
|
+
app.use(cors());
|
|
17
|
+
// app.use(rateLimiter); // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
18
|
+
|
|
19
|
+
// AutoStack API
|
|
20
|
+
app.use('/autostack', (req, res) => {
|
|
21
|
+
const message = `
|
|
22
|
+
Congrats! You have successfully set up your full-stack project!
|
|
23
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
24
|
+
You are ready to create your next big project!
|
|
25
|
+
`
|
|
26
|
+
return res.json({message: message, backend: 'NodeJS', database: "MongoDB", filepath: 'backend/src/server.js'})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// These are example APIs you can choose to keep them as it is, modify them according to your database or remove them entirely
|
|
30
|
+
app.use("/api/notes", noteRoutes);
|
|
31
|
+
app.use("/api/users", userRoutes);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// connectDB().then(() => {
|
|
35
|
+
// app.listen(PORT, () =>
|
|
36
|
+
// console.log("Server is running on port", PORT));
|
|
37
|
+
// })
|
|
38
|
+
|
|
39
|
+
app.listen(PORT, () => console.log("Server is running on port", PORT))
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import mongoose from "mongoose"
|
|
2
|
+
|
|
3
|
+
const noteSchema = new mongoose.Schema({
|
|
4
|
+
title: {
|
|
5
|
+
type: String,
|
|
6
|
+
required: true
|
|
7
|
+
}, content: {
|
|
8
|
+
type: String,
|
|
9
|
+
required: true
|
|
10
|
+
}
|
|
11
|
+
// ...
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
timestamps: true
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export const Note = mongoose.model("Note", noteSchema);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import mongoose from "mongoose"
|
|
2
|
+
|
|
3
|
+
const userSchema = new mongoose.Schema({
|
|
4
|
+
username: {
|
|
5
|
+
type: String,
|
|
6
|
+
required: true
|
|
7
|
+
}, email: {
|
|
8
|
+
type: String,
|
|
9
|
+
required: true
|
|
10
|
+
}
|
|
11
|
+
// ...
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
timestamps: true
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
export const User = mongoose.model("User", userSchema);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import mysql2 from 'mysql2';
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
export const connectDB = async () => {
|
|
7
|
+
try {
|
|
8
|
+
const connection = await mysql2.createConnection({
|
|
9
|
+
host: process.env.HOST,
|
|
10
|
+
user: process.env.USER,
|
|
11
|
+
password: process.env.PASSWORD,
|
|
12
|
+
database: process.env.DATABASE
|
|
13
|
+
});
|
|
14
|
+
console.log('MySQL connected successfully!');
|
|
15
|
+
return connection;
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error("Error connecting to database:", error.message);
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import pool from "../config/db.js";
|
|
2
|
+
|
|
3
|
+
// Get All Notes
|
|
4
|
+
export const getAllNotes = async (req, res) => {
|
|
5
|
+
try {
|
|
6
|
+
const [rows] = await pool.query('SELECT * FROM notes ORDER BY created_at DESC');
|
|
7
|
+
res.status(200).json(rows);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
res.status(500).json({error: error.message});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Get A Single Note
|
|
14
|
+
export const getNoteByID = async (req, res) => {
|
|
15
|
+
try {
|
|
16
|
+
const { id } = req.params;
|
|
17
|
+
const [rows] = await pool.query('SELECT * FROM notes WHERE id = ?', [id]);
|
|
18
|
+
|
|
19
|
+
if (rows.length === 0) {
|
|
20
|
+
return res.status(404).json({message: "Note not found!"});
|
|
21
|
+
}
|
|
22
|
+
res.status(200).json(rows[0]);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
res.status(500).json({error: error.message});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Create A Note
|
|
29
|
+
export const createNote = async (req, res) => {
|
|
30
|
+
try {
|
|
31
|
+
const {title, content} = req.body;
|
|
32
|
+
if (!title || !content) {
|
|
33
|
+
return res.status(400).json({message: "Please fill in all the fields"});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const [result] = await pool.query(
|
|
37
|
+
'INSERT INTO notes (title, content) VALUES (?, ?)',
|
|
38
|
+
[title, content]
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const [newNote] = await pool.query('SELECT * FROM notes WHERE id = ?', [result.insertId]);
|
|
42
|
+
|
|
43
|
+
res.status(201).json({
|
|
44
|
+
message: "Note created successfully",
|
|
45
|
+
createdNote: newNote[0]
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
res.status(500).json({message: "Error creating note", error: error.message});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Update Note
|
|
53
|
+
export const updateNote = async (req, res) => {
|
|
54
|
+
try {
|
|
55
|
+
const {title, content} = req.body;
|
|
56
|
+
const { id } = req.params;
|
|
57
|
+
|
|
58
|
+
const [result] = await pool.query(
|
|
59
|
+
'UPDATE notes SET title = ?, content = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
|
|
60
|
+
[title, content, id]
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
if (result.affectedRows === 0) {
|
|
64
|
+
return res.status(404).json({message: "Note not found!"});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const [updatedNote] = await pool.query('SELECT * FROM notes WHERE id = ?', [id]);
|
|
68
|
+
|
|
69
|
+
res.status(200).json({
|
|
70
|
+
message: "Note updated successfully",
|
|
71
|
+
updatedNote: updatedNote[0]
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
res.status(500).json({message: "Error updating note", error: error.message});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Delete Note
|
|
79
|
+
export const deleteNote = async (req, res) => {
|
|
80
|
+
try {
|
|
81
|
+
const { id } = req.params;
|
|
82
|
+
const [note] = await pool.query('SELECT * FROM notes WHERE id = ?', [id]);
|
|
83
|
+
|
|
84
|
+
if (note.length === 0) {
|
|
85
|
+
return res.status(404).json({message: "Note not found"});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await pool.query('DELETE FROM notes WHERE id = ?', [id]);
|
|
89
|
+
|
|
90
|
+
res.status(200).json({
|
|
91
|
+
message: "Note deleted successfully",
|
|
92
|
+
deletedNote: note[0]
|
|
93
|
+
});
|
|
94
|
+
} catch (error) {
|
|
95
|
+
res.status(500).json({message: "Error deleting note", error: error.message});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import pool from "../config/db.js";
|
|
2
|
+
|
|
3
|
+
// Get All Users
|
|
4
|
+
export const getAllUsers = async (req, res) => {
|
|
5
|
+
try {
|
|
6
|
+
const [rows] = await pool.query('SELECT * FROM users ORDER BY created_at DESC');
|
|
7
|
+
res.status(200).json(rows);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
res.status(500).json({message: "Error getting users", error: error.message});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Get A Single User
|
|
14
|
+
export const getUserByID = async (req, res) => {
|
|
15
|
+
try {
|
|
16
|
+
const { id } = req.params;
|
|
17
|
+
const [rows] = await pool.query('SELECT * FROM users WHERE id = ?', [id]);
|
|
18
|
+
|
|
19
|
+
if (rows.length === 0) {
|
|
20
|
+
return res.status(404).json({message: "User not found!"});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
res.status(200).json(rows[0]);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
res.status(500).json({message: "Error finding user", error: error.message});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Create A User
|
|
30
|
+
export const createUser = async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const {username, email} = req.body;
|
|
33
|
+
if (!username || !email) {
|
|
34
|
+
return res.status(400).json({message: "Please fill in all the fields"});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const [result] = await pool.query(
|
|
38
|
+
'INSERT INTO users (username, email) VALUES (?, ?)',
|
|
39
|
+
[username, email]
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const [newUser] = await pool.query('SELECT * FROM users WHERE id = ?', [result.insertId]);
|
|
43
|
+
|
|
44
|
+
res.status(201).json({
|
|
45
|
+
message: "User created successfully",
|
|
46
|
+
createdUser: newUser[0]
|
|
47
|
+
});
|
|
48
|
+
} catch (error) {
|
|
49
|
+
res.status(500).json({message: "Error creating user", error: error.message});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Update User
|
|
54
|
+
export const updateUser = async (req, res) => {
|
|
55
|
+
try {
|
|
56
|
+
const {username, email} = req.body;
|
|
57
|
+
const { id } = req.params;
|
|
58
|
+
|
|
59
|
+
const [result] = await pool.query(
|
|
60
|
+
'UPDATE users SET username = ?, email = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
|
|
61
|
+
[username, email, id]
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
if (result.affectedRows === 0) {
|
|
65
|
+
return res.status(404).json({message: "User not found!"});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const [updatedUser] = await pool.query('SELECT * FROM users WHERE id = ?', [id]);
|
|
69
|
+
|
|
70
|
+
res.status(200).json({
|
|
71
|
+
message: "User updated successfully",
|
|
72
|
+
updatedUser: updatedUser[0]
|
|
73
|
+
});
|
|
74
|
+
} catch (error) {
|
|
75
|
+
res.status(500).json({message: "Error updating user", error: error.message});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Delete User
|
|
80
|
+
export const deleteUser = async (req, res) => {
|
|
81
|
+
try {
|
|
82
|
+
const { id } = req.params;
|
|
83
|
+
const [user] = await pool.query('SELECT * FROM users WHERE id = ?', [id]);
|
|
84
|
+
|
|
85
|
+
if (user.length === 0) {
|
|
86
|
+
return res.status(404).json({message: "User not found"});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
await pool.query('DELETE FROM users WHERE id = ?', [id]);
|
|
90
|
+
|
|
91
|
+
res.status(200).json({
|
|
92
|
+
message: "User deleted successfully",
|
|
93
|
+
deletedUser: user[0]
|
|
94
|
+
});
|
|
95
|
+
} catch (error) {
|
|
96
|
+
res.status(500).json({message: "Error deleting user", error: error.message});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { connectDB } from './config/dbConn.js';
|
|
3
|
+
import dotenv from 'dotenv'
|
|
4
|
+
import noteRoutes from './routes/noteRoutes.js';
|
|
5
|
+
import userRoutes from './routes/userRoutes.js';
|
|
6
|
+
// import rateLimiter from './middleware/upstash.js'; // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
7
|
+
import cors from 'cors';
|
|
8
|
+
|
|
9
|
+
dotenv.config(); // This will help you read the env variable. Else, undefined.
|
|
10
|
+
|
|
11
|
+
const app = express();
|
|
12
|
+
const PORT = process.env.PORT || 3000;
|
|
13
|
+
|
|
14
|
+
// Middleware
|
|
15
|
+
app.use(express.json());
|
|
16
|
+
app.use(cors());
|
|
17
|
+
// app.use(rateLimiter); // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
18
|
+
|
|
19
|
+
// AutoStack API
|
|
20
|
+
app.use('/autostack', (req, res) => {
|
|
21
|
+
const message = `
|
|
22
|
+
Congrats! You have successfully set up your full-stack project!
|
|
23
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
24
|
+
You are ready to create your next big project!
|
|
25
|
+
`
|
|
26
|
+
return res.json({message: message, backend: 'NodeJS', database: "MySQL", filepath: 'backend/src/server.js'})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// These are example APIs you can choose to keep them as it is, modify them according to your database or remove them entirely
|
|
30
|
+
app.use("/api/notes", noteRoutes);
|
|
31
|
+
app.use("/api/users", userRoutes);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// connectDB().then(() => {
|
|
35
|
+
// app.listen(PORT, () =>
|
|
36
|
+
// console.log("Server is running on port", PORT));
|
|
37
|
+
// })
|
|
38
|
+
|
|
39
|
+
app.listen(PORT, () => console.log("Server is running on port", PORT))
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// db.js
|
|
2
|
+
import pkg from 'pg';
|
|
3
|
+
const { Pool } = pkg;
|
|
4
|
+
import dotenv from 'dotenv';
|
|
5
|
+
|
|
6
|
+
dotenv.config();
|
|
7
|
+
|
|
8
|
+
const pool = new Pool({
|
|
9
|
+
host: process.env.HOST,
|
|
10
|
+
user: process.env.USER,
|
|
11
|
+
password: process.env.PASSWORD,
|
|
12
|
+
database: process.env.DATABASE,
|
|
13
|
+
port: process.env.PORT || 5432
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const connectDB = async () => {
|
|
17
|
+
try {
|
|
18
|
+
const client = await pool.connect();
|
|
19
|
+
console.log('PostgreSQL connected successfully!');
|
|
20
|
+
client.release();
|
|
21
|
+
return pool;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error("Error connecting to database:", error.message);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default pool;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import pool from "../config/db.js";
|
|
2
|
+
|
|
3
|
+
// Get All Notes
|
|
4
|
+
export const getAllNotes = async (req, res) => {
|
|
5
|
+
try {
|
|
6
|
+
const result = await pool.query('SELECT * FROM notes ORDER BY created_at DESC');
|
|
7
|
+
res.status(200).json(result.rows);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
res.status(500).json({error: error.message});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Get A Single Note
|
|
14
|
+
export const getNoteByID = async (req, res) => {
|
|
15
|
+
try {
|
|
16
|
+
const { id } = req.params;
|
|
17
|
+
const result = await pool.query('SELECT * FROM notes WHERE id = $1', [id]);
|
|
18
|
+
|
|
19
|
+
if (result.rows.length === 0) {
|
|
20
|
+
return res.status(404).json({message: "Note not found!"});
|
|
21
|
+
}
|
|
22
|
+
res.status(200).json(result.rows[0]);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
res.status(500).json({error: error.message});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Create A Note
|
|
29
|
+
export const createNote = async (req, res) => {
|
|
30
|
+
try {
|
|
31
|
+
const {title, content} = req.body;
|
|
32
|
+
if (!title || !content) {
|
|
33
|
+
return res.status(400).json({message: "Please fill in all the fields"});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const result = await pool.query(
|
|
37
|
+
'INSERT INTO notes (title, content) VALUES ($1, $2) RETURNING *',
|
|
38
|
+
[title, content]
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
res.status(201).json({
|
|
42
|
+
message: "Note created successfully",
|
|
43
|
+
createdNote: result.rows[0]
|
|
44
|
+
});
|
|
45
|
+
} catch (error) {
|
|
46
|
+
res.status(500).json({message: "Error creating note", error: error.message});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Update Note
|
|
51
|
+
export const updateNote = async (req, res) => {
|
|
52
|
+
try {
|
|
53
|
+
const {title, content} = req.body;
|
|
54
|
+
const { id } = req.params;
|
|
55
|
+
|
|
56
|
+
const result = await pool.query(
|
|
57
|
+
'UPDATE notes SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3 RETURNING *',
|
|
58
|
+
[title, content, id]
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (result.rows.length === 0) {
|
|
62
|
+
return res.status(404).json({message: "Note not found!"});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
res.status(200).json({
|
|
66
|
+
message: "Note updated successfully",
|
|
67
|
+
updatedNote: result.rows[0]
|
|
68
|
+
});
|
|
69
|
+
} catch (error) {
|
|
70
|
+
res.status(500).json({message: "Error updating note", error: error.message});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Delete Note
|
|
75
|
+
export const deleteNote = async (req, res) => {
|
|
76
|
+
try {
|
|
77
|
+
const { id } = req.params;
|
|
78
|
+
const result = await pool.query('DELETE FROM notes WHERE id = $1 RETURNING *', [id]);
|
|
79
|
+
|
|
80
|
+
if (result.rows.length === 0) {
|
|
81
|
+
return res.status(404).json({message: "Note not found"});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
res.status(200).json({
|
|
85
|
+
message: "Note deleted successfully",
|
|
86
|
+
deletedNote: result.rows[0]
|
|
87
|
+
});
|
|
88
|
+
} catch (error) {
|
|
89
|
+
res.status(500).json({message: "Error deleting note", error: error.message});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import pool from "../config/db.js"
|
|
2
|
+
|
|
3
|
+
// Get All Users
|
|
4
|
+
export const getAllUsers = async (req, res) => {
|
|
5
|
+
try {
|
|
6
|
+
const result = await pool.query('SELECT * FROM users ORDER BY created_at DESC');
|
|
7
|
+
res.status(200).json(result.rows);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
res.status(500).json({message: "Error getting users", error: error.message});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Get A Single User
|
|
14
|
+
export const getUserByID = async (req, res) => {
|
|
15
|
+
try {
|
|
16
|
+
const { id } = req.params;
|
|
17
|
+
const result = await pool.query('SELECT * FROM users WHERE id = $1', [id]);
|
|
18
|
+
|
|
19
|
+
if (result.rows.length === 0) {
|
|
20
|
+
return res.status(404).json({message: "User not found!"});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
res.status(200).json(result.rows[0]);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
res.status(500).json({message: "Error finding user", error: error.message});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Create A User
|
|
30
|
+
export const createUser = async (req, res) => {
|
|
31
|
+
try {
|
|
32
|
+
const {username, email} = req.body;
|
|
33
|
+
if (!username || !email) {
|
|
34
|
+
return res.status(400).json({message: "Please fill in all the fields"});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const result = await pool.query(
|
|
38
|
+
'INSERT INTO users (username, email) VALUES ($1, $2) RETURNING *',
|
|
39
|
+
[username, email]
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
res.status(201).json({
|
|
43
|
+
message: "User created successfully",
|
|
44
|
+
createdUser: result.rows[0]
|
|
45
|
+
});
|
|
46
|
+
} catch (error) {
|
|
47
|
+
res.status(500).json({message: "Error creating user", error: error.message});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Update User
|
|
52
|
+
export const updateUser = async (req, res) => {
|
|
53
|
+
try {
|
|
54
|
+
const {username, email} = req.body;
|
|
55
|
+
const { id } = req.params;
|
|
56
|
+
|
|
57
|
+
const result = await pool.query(
|
|
58
|
+
'UPDATE users SET username = $1, email = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3 RETURNING *',
|
|
59
|
+
[username, email, id]
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
if (result.rows.length === 0) {
|
|
63
|
+
return res.status(404).json({message: "User not found!"});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
res.status(200).json({
|
|
67
|
+
message: "User updated successfully",
|
|
68
|
+
updatedUser: result.rows[0]
|
|
69
|
+
});
|
|
70
|
+
} catch (error) {
|
|
71
|
+
res.status(500).json({message: "Error updating user", error: error.message});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Delete User
|
|
76
|
+
export const deleteUser = async (req, res) => {
|
|
77
|
+
try {
|
|
78
|
+
const { id } = req.params;
|
|
79
|
+
const result = await pool.query('DELETE FROM users WHERE id = $1 RETURNING *', [id]);
|
|
80
|
+
|
|
81
|
+
if (result.rows.length === 0) {
|
|
82
|
+
return res.status(404).json({message: "User not found"});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
res.status(200).json({
|
|
86
|
+
message: "User deleted successfully",
|
|
87
|
+
deletedUser: result.rows[0]
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
res.status(500).json({message: "Error deleting user", error: error.message});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import express from 'express'
|
|
2
|
+
import { connectDB } from './config/dbConn.js';
|
|
3
|
+
import dotenv from 'dotenv'
|
|
4
|
+
import noteRoutes from './routes/noteRoutes.js';
|
|
5
|
+
import userRoutes from './routes/userRoutes.js';
|
|
6
|
+
// import rateLimiter from './middleware/upstash.js'; // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
7
|
+
import cors from 'cors';
|
|
8
|
+
|
|
9
|
+
dotenv.config(); // This will help you read the env variable. Else, undefined.
|
|
10
|
+
|
|
11
|
+
const app = express();
|
|
12
|
+
const PORT = process.env.PORT || 3000;
|
|
13
|
+
|
|
14
|
+
// Middleware
|
|
15
|
+
app.use(express.json());
|
|
16
|
+
app.use(cors());
|
|
17
|
+
// app.use(rateLimiter); // Uncomment this after setting rateLimiter credentials or remove entirely
|
|
18
|
+
|
|
19
|
+
// AutoStack API
|
|
20
|
+
app.use('/autostack', (req, res) => {
|
|
21
|
+
const message = `
|
|
22
|
+
Congrats! You have successfully set up your full-stack project!
|
|
23
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
24
|
+
You are ready to create your next big project!
|
|
25
|
+
`
|
|
26
|
+
return res.json({message: message, backend: 'NodeJS', database: "PostgreSQL", filepath: 'backend/src/server.js'})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// These are example APIs you can choose to keep them as it is, modify them according to your database or remove them entirely
|
|
30
|
+
app.use("/api/notes", noteRoutes);
|
|
31
|
+
app.use("/api/users", userRoutes);
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
// connectDB().then(() => {
|
|
35
|
+
// app.listen(PORT, () =>
|
|
36
|
+
// console.log("Server is running on port", PORT));
|
|
37
|
+
// })
|
|
38
|
+
|
|
39
|
+
app.listen(PORT, () => console.log("Server is running on port", PORT))
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// import { Ratelimit } from "@upstash/ratelimit"
|
|
2
|
+
// import { Redis } from "@upstash/redis"
|
|
3
|
+
// import dotenv from "dotenv"
|
|
4
|
+
|
|
5
|
+
// dotenv.config();
|
|
6
|
+
|
|
7
|
+
// const ratelimit = new Ratelimit({
|
|
8
|
+
// redis: Redis.fromEnv(),
|
|
9
|
+
// limiter: Ratelimit.slidingWindow(100, "60 s")
|
|
10
|
+
// })
|
|
11
|
+
|
|
12
|
+
// export default ratelimit;
|