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,16 @@
|
|
|
1
|
+
from bson import ObjectId
|
|
2
|
+
|
|
3
|
+
def serialize_user(doc):
|
|
4
|
+
return {
|
|
5
|
+
"id": str(doc.get("_id")),
|
|
6
|
+
"username": doc.get("username"),
|
|
7
|
+
"email": doc.get("email"),
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def serialize_note(doc):
|
|
12
|
+
return {
|
|
13
|
+
"id": str(doc.get("_id")),
|
|
14
|
+
"title": doc.get("title"),
|
|
15
|
+
"content": doc.get("content"),
|
|
16
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
from flask import request, jsonify
|
|
2
|
+
from MDB_config import app, db
|
|
3
|
+
from models import serialize_user, serialize_note
|
|
4
|
+
from bson import ObjectId
|
|
5
|
+
|
|
6
|
+
@app.route("/autostack", methods=["GET"])
|
|
7
|
+
def autostack():
|
|
8
|
+
message = """
|
|
9
|
+
Congrats! You have successfully set up your full-stack project!
|
|
10
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
11
|
+
You are ready to create your next big project!
|
|
12
|
+
"""
|
|
13
|
+
return jsonify({"message": message, "backend": "Flask", "database": "MongoDB", "filepath": 'backend/main.py'}), 200
|
|
14
|
+
|
|
15
|
+
# Example User REST APIs
|
|
16
|
+
@app.route("/get-users", methods=["GET"])
|
|
17
|
+
def get_users():
|
|
18
|
+
users = list(db.users.find())
|
|
19
|
+
json_users = [serialize_user(u) for u in users]
|
|
20
|
+
return jsonify({"users": json_users}), 200
|
|
21
|
+
|
|
22
|
+
@app.route("/create-user", methods=["POST"])
|
|
23
|
+
def create_user():
|
|
24
|
+
username = request.json.get("username")
|
|
25
|
+
email = request.json.get("email")
|
|
26
|
+
if not username or not email:
|
|
27
|
+
return jsonify({"message": "Missing fields"}), 400
|
|
28
|
+
|
|
29
|
+
# optional uniqueness check
|
|
30
|
+
if db.users.find_one({"$or": [{"username": username}, {"email": email}] } ):
|
|
31
|
+
return jsonify({"message": "User with same username or email already exists"}), 400
|
|
32
|
+
|
|
33
|
+
res = db.users.insert_one({"username": username, "email": email})
|
|
34
|
+
return jsonify({"message": "New User Created", "id": str(res.inserted_id)}), 201
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@app.route("/update-users/<user_id>", methods=["PATCH"])
|
|
38
|
+
def update_user(user_id):
|
|
39
|
+
try:
|
|
40
|
+
_id = ObjectId(user_id)
|
|
41
|
+
except Exception:
|
|
42
|
+
return jsonify({"message": "Invalid user id"}), 400
|
|
43
|
+
|
|
44
|
+
data = request.json
|
|
45
|
+
if not data:
|
|
46
|
+
return jsonify({"message": "No data provided"}), 400
|
|
47
|
+
|
|
48
|
+
update = {}
|
|
49
|
+
if "username" in data:
|
|
50
|
+
update["username"] = data["username"]
|
|
51
|
+
if "email" in data:
|
|
52
|
+
update["email"] = data["email"]
|
|
53
|
+
|
|
54
|
+
if not update:
|
|
55
|
+
return jsonify({"message": "No valid fields to update"}), 400
|
|
56
|
+
|
|
57
|
+
result = db.users.update_one({"_id": _id}, {"$set": update})
|
|
58
|
+
if result.matched_count == 0:
|
|
59
|
+
return jsonify({"message": "User not found"}), 404
|
|
60
|
+
|
|
61
|
+
return jsonify({"message": "User Updated"}), 200
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@app.route("/delete-user/<user_id>", methods=["DELETE"])
|
|
65
|
+
def delete_user(user_id):
|
|
66
|
+
try:
|
|
67
|
+
_id = ObjectId(user_id)
|
|
68
|
+
except Exception:
|
|
69
|
+
return jsonify({"message": "Invalid user id"}), 400
|
|
70
|
+
|
|
71
|
+
result = db.users.delete_one({"_id": _id})
|
|
72
|
+
if result.deleted_count == 0:
|
|
73
|
+
return jsonify({"message": "User not found"}), 404
|
|
74
|
+
|
|
75
|
+
return jsonify({"message": "User Deleted"}), 200
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Example Notes REST APIs
|
|
79
|
+
@app.route("/get-notes", methods=["GET"])
|
|
80
|
+
def get_notes():
|
|
81
|
+
notes = list(db.notes.find())
|
|
82
|
+
json_notes = [serialize_note(n) for n in notes]
|
|
83
|
+
return jsonify({"notes": json_notes}), 200
|
|
84
|
+
|
|
85
|
+
@app.route("/create-note", methods=["POST"])
|
|
86
|
+
def create_note():
|
|
87
|
+
title = request.json.get("title")
|
|
88
|
+
content = request.json.get("content")
|
|
89
|
+
if not title or not content:
|
|
90
|
+
return jsonify({"message": "Missing fields"}), 400
|
|
91
|
+
|
|
92
|
+
if db.notes.find_one({"title": title}):
|
|
93
|
+
return jsonify({"message": "Note with same title already exists"}), 400
|
|
94
|
+
|
|
95
|
+
res = db.notes.insert_one({"title": title, "content": content})
|
|
96
|
+
return jsonify({"message": "New Note Created", "id": str(res.inserted_id)}), 201
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@app.route("/update-notes/<note_id>", methods=["PATCH"])
|
|
100
|
+
def update_note(note_id):
|
|
101
|
+
try:
|
|
102
|
+
_id = ObjectId(note_id)
|
|
103
|
+
except Exception:
|
|
104
|
+
return jsonify({"message": "Invalid note id"}), 400
|
|
105
|
+
|
|
106
|
+
data = request.json
|
|
107
|
+
if not data:
|
|
108
|
+
return jsonify({"message": "No data provided for updation"}), 400
|
|
109
|
+
|
|
110
|
+
update = {}
|
|
111
|
+
if "title" in data:
|
|
112
|
+
update["title"] = data["title"]
|
|
113
|
+
if "content" in data:
|
|
114
|
+
update["content"] = data["content"]
|
|
115
|
+
|
|
116
|
+
if not update:
|
|
117
|
+
return jsonify({"message": "No valid fields to update"}), 400
|
|
118
|
+
|
|
119
|
+
result = db.notes.update_one({"_id": _id}, {"$set": update})
|
|
120
|
+
if result.matched_count == 0:
|
|
121
|
+
return jsonify({"message": "Note not found"}), 404
|
|
122
|
+
|
|
123
|
+
return jsonify({"message": "Note Updated"}), 200
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
@app.route("/delete-note/<note_id>", methods=["DELETE"])
|
|
127
|
+
def delete_note(note_id):
|
|
128
|
+
try:
|
|
129
|
+
_id = ObjectId(note_id)
|
|
130
|
+
except Exception:
|
|
131
|
+
return jsonify({"message": "Invalid note id"}), 400
|
|
132
|
+
|
|
133
|
+
result = db.notes.delete_one({"_id": _id})
|
|
134
|
+
if result.deleted_count == 0:
|
|
135
|
+
return jsonify({"message": "Note not found"}), 404
|
|
136
|
+
|
|
137
|
+
return jsonify({"message": "Note Deleted"}), 200
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from flask import Flask
|
|
2
|
+
from flask_sqlalchemy import SQLAlchemy
|
|
3
|
+
from flask_cors import CORS
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
load_dotenv()
|
|
8
|
+
|
|
9
|
+
app = Flask(__name__)
|
|
10
|
+
CORS(app)
|
|
11
|
+
|
|
12
|
+
# MySQL connection via environment variables. Example URI:
|
|
13
|
+
# mysql+pymysql://<user>:<password>@<host>:<port>/<dbname>
|
|
14
|
+
DB_USER = os.getenv("DB_USER", "root") # default value
|
|
15
|
+
DB_PASSWORD = os.getenv("DB_PASSWORD", "")
|
|
16
|
+
DB_HOST = os.getenv("DB_HOST", "localhost")
|
|
17
|
+
DB_PORT = os.getenv("DB_PORT", "3306")
|
|
18
|
+
DB_NAME = os.getenv("DB_NAME", "example_db")
|
|
19
|
+
|
|
20
|
+
app.config['SQLALCHEMY_DATABASE_URI'] = (
|
|
21
|
+
f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
|
22
|
+
)
|
|
23
|
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
24
|
+
db = SQLAlchemy(app)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from config import db
|
|
2
|
+
|
|
3
|
+
class User(db.Model):
|
|
4
|
+
id = db.Column(db.Integer, primary_key = True)
|
|
5
|
+
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
6
|
+
email = db.Column(db.String(120), unique=True, nullable=False)
|
|
7
|
+
|
|
8
|
+
def to_json(self):
|
|
9
|
+
return {
|
|
10
|
+
"id": self.id,
|
|
11
|
+
"username": self.username,
|
|
12
|
+
"email": self.email
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Note(db.Model):
|
|
17
|
+
id = db.Column(db.Integer, primary_key = True)
|
|
18
|
+
title = db.Column(db.String(80), unique=True, nullable=False)
|
|
19
|
+
content = db.Column(db.String(120), unique=True, nullable=False)
|
|
20
|
+
|
|
21
|
+
def to_json(self):
|
|
22
|
+
return {
|
|
23
|
+
"id": self.id,
|
|
24
|
+
"title": self.title,
|
|
25
|
+
"content": self.content
|
|
26
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
|
|
2
|
+
from flask import request, jsonify
|
|
3
|
+
from config import app, db
|
|
4
|
+
from models import User, Note
|
|
5
|
+
|
|
6
|
+
@app.route("/autostack", methods=["GET"])
|
|
7
|
+
def autostack():
|
|
8
|
+
message = """
|
|
9
|
+
Congrats! You have successfully set up your full-stack project!
|
|
10
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
11
|
+
You are ready to create your next big project!
|
|
12
|
+
"""
|
|
13
|
+
return jsonify({"message": message, "backend": "Flask", "database": "MySQL", "filepath": 'backend/main.py'}), 200
|
|
14
|
+
|
|
15
|
+
# Example user REST APIs
|
|
16
|
+
@app.route("/get-users", methods=["GET"])
|
|
17
|
+
def get_users():
|
|
18
|
+
users = User.query.all()
|
|
19
|
+
json_users = list(map(lambda x: x.to_json(), users))
|
|
20
|
+
return jsonify({"users": json_users}), 200
|
|
21
|
+
|
|
22
|
+
@app.route("/create-user", methods=["POST"])
|
|
23
|
+
def create_user():
|
|
24
|
+
username = request.json.get("username")
|
|
25
|
+
email = request.json.get("email")
|
|
26
|
+
|
|
27
|
+
if not username or not email:
|
|
28
|
+
return(jsonify({"message": "Missing fields"})), 400
|
|
29
|
+
|
|
30
|
+
new_user = User(username=username, email=email)
|
|
31
|
+
try:
|
|
32
|
+
db.session.add(new_user)
|
|
33
|
+
db.session.commit()
|
|
34
|
+
except Exception as e:
|
|
35
|
+
return jsonify({"message": str(e)}), 400
|
|
36
|
+
|
|
37
|
+
return jsonify({"message": "New User Created"}), 201
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@app.route("/update-users/<int:user_id>", methods=["PATCH"])
|
|
41
|
+
def update_user(user_id):
|
|
42
|
+
user = User.query.get(user_id)
|
|
43
|
+
if not user:
|
|
44
|
+
return jsonify({"message": "User not found"}), 404
|
|
45
|
+
data = request.json
|
|
46
|
+
if not data:
|
|
47
|
+
return jsonify({"message": "No data provided"}), 400
|
|
48
|
+
|
|
49
|
+
if "username" in data:
|
|
50
|
+
user.username = data["username"]
|
|
51
|
+
if "email" in data:
|
|
52
|
+
user.email = data["email"]
|
|
53
|
+
|
|
54
|
+
try:
|
|
55
|
+
db.session.commit()
|
|
56
|
+
except Exception as e:
|
|
57
|
+
return jsonify({"message": str(e)}), 400
|
|
58
|
+
|
|
59
|
+
return jsonify({"message": "User Updated"}), 200
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@app.route("/delete-user/<int:user_id>", methods=["DELETE"])
|
|
63
|
+
def delete_user(user_id):
|
|
64
|
+
user = User.query.get(user_id)
|
|
65
|
+
if not user:
|
|
66
|
+
return jsonify({"message": "User not found"}), 404
|
|
67
|
+
try:
|
|
68
|
+
db.session.delete(user)
|
|
69
|
+
db.session.commit()
|
|
70
|
+
except Exception as e:
|
|
71
|
+
return jsonify({"message": str(e)}), 400
|
|
72
|
+
|
|
73
|
+
return jsonify({"message": "User Deleted"}), 200
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# Example Notes REST APIs
|
|
77
|
+
@app.route("/get-notes", methods=["GET"])
|
|
78
|
+
def get_notes():
|
|
79
|
+
notes = Note.query.all()
|
|
80
|
+
json_notes = list(map(lambda x: x.to_json(), notes))
|
|
81
|
+
return jsonify({"notes": json_notes}), 200
|
|
82
|
+
|
|
83
|
+
@app.route("/create-note", methods=["POST"])
|
|
84
|
+
def create_note():
|
|
85
|
+
title = request.json.get("title")
|
|
86
|
+
content = request.json.get("content")
|
|
87
|
+
|
|
88
|
+
if not title or not content:
|
|
89
|
+
return(jsonify({"message": "Missing fields"})), 400
|
|
90
|
+
|
|
91
|
+
new_note = Note(title=title, content=content)
|
|
92
|
+
try:
|
|
93
|
+
db.session.add(new_note)
|
|
94
|
+
db.session.commit()
|
|
95
|
+
except Exception as e:
|
|
96
|
+
return jsonify({"message": str(e)}), 400
|
|
97
|
+
|
|
98
|
+
return jsonify({"message": "New Note Created"}), 201
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@app.route("/update-notes/<int:note_id>", methods=["PATCH"])
|
|
102
|
+
def update_note(note_id):
|
|
103
|
+
note = Note.query.get(note_id)
|
|
104
|
+
if not note:
|
|
105
|
+
return jsonify({"message": "Note not found"}), 404
|
|
106
|
+
data = request.json
|
|
107
|
+
|
|
108
|
+
if not data:
|
|
109
|
+
return jsonify({"message": "No data provided for updation"})
|
|
110
|
+
|
|
111
|
+
if "title" in data:
|
|
112
|
+
note.title = data["title"]
|
|
113
|
+
if "content" in data:
|
|
114
|
+
note.content = data["content"]
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
db.session.commit()
|
|
118
|
+
except Exception as e:
|
|
119
|
+
return jsonify({"message": str(e)}), 400
|
|
120
|
+
|
|
121
|
+
return jsonify({"message": "User Updated"}), 200
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@app.route("/delete-note/<int:note_id>", methods=["DELETE"])
|
|
125
|
+
def delete_note(note_id):
|
|
126
|
+
note = Note.query.get(note_id)
|
|
127
|
+
if not note:
|
|
128
|
+
return jsonify({"message": "Note not found"}), 404
|
|
129
|
+
try:
|
|
130
|
+
db.session.delete(note)
|
|
131
|
+
db.session.commit()
|
|
132
|
+
except Exception as e:
|
|
133
|
+
return jsonify({"message": str(e)}), 400
|
|
134
|
+
|
|
135
|
+
return jsonify({"message": "Note Deleted"}), 200
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from flask import Flask
|
|
2
|
+
from flask_sqlalchemy import SQLAlchemy
|
|
3
|
+
from flask_cors import CORS
|
|
4
|
+
from dotenv import load_dotenv
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
load_dotenv()
|
|
8
|
+
|
|
9
|
+
app = Flask(__name__)
|
|
10
|
+
CORS(app)
|
|
11
|
+
|
|
12
|
+
# PostgreSQL connection via environment variables. Example URI:
|
|
13
|
+
# postgresql+psycopg2://<user>:<password>@<host>:<port>/<dbname>
|
|
14
|
+
DB_USER = os.getenv("DB_USER", "postgres")
|
|
15
|
+
DB_PASSWORD = os.getenv("DB_PASSWORD", "")
|
|
16
|
+
DB_HOST = os.getenv("DB_HOST", "localhost")
|
|
17
|
+
DB_PORT = os.getenv("DB_PORT", "5432")
|
|
18
|
+
DB_NAME = os.getenv("DB_NAME", "example_db")
|
|
19
|
+
|
|
20
|
+
app.config['SQLALCHEMY_DATABASE_URI'] = (
|
|
21
|
+
f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
|
22
|
+
)
|
|
23
|
+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
24
|
+
db = SQLAlchemy(app)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from config import db
|
|
2
|
+
|
|
3
|
+
class User(db.Model):
|
|
4
|
+
id = db.Column(db.Integer, primary_key=True)
|
|
5
|
+
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
6
|
+
email = db.Column(db.String(120), unique=True, nullable=False)
|
|
7
|
+
|
|
8
|
+
def to_json(self):
|
|
9
|
+
return {
|
|
10
|
+
"id": self.id,
|
|
11
|
+
"username": self.username,
|
|
12
|
+
"email": self.email,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Note(db.Model):
|
|
17
|
+
id = db.Column(db.Integer, primary_key=True)
|
|
18
|
+
title = db.Column(db.String(80), unique=True, nullable=False)
|
|
19
|
+
content = db.Column(db.String(120), unique=True, nullable=False)
|
|
20
|
+
|
|
21
|
+
def to_json(self):
|
|
22
|
+
return {
|
|
23
|
+
"id": self.id,
|
|
24
|
+
"title": self.title,
|
|
25
|
+
"content": self.content,
|
|
26
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
from flask import request, jsonify
|
|
2
|
+
from config import app, db
|
|
3
|
+
from models import User, Note
|
|
4
|
+
|
|
5
|
+
@app.route("/autostack", methods=["GET"])
|
|
6
|
+
def autostack():
|
|
7
|
+
message = """
|
|
8
|
+
Congrats! You have successfully set up your full-stack project!
|
|
9
|
+
If you're reading this message, it means your frontend and backend are completely connected!
|
|
10
|
+
You are ready to create your next big project!
|
|
11
|
+
"""
|
|
12
|
+
return jsonify({"message": message, "backend": "Flask", "database": "PostgreSQL", "filepath": 'backend/main.py'}), 200
|
|
13
|
+
|
|
14
|
+
# These are example user REST APIs
|
|
15
|
+
@app.route("/get-users", methods=["GET"])
|
|
16
|
+
def get_users():
|
|
17
|
+
users = User.query.all()
|
|
18
|
+
json_users = list(map(lambda x: x.to_json(), users))
|
|
19
|
+
return jsonify({"users": json_users}), 200
|
|
20
|
+
|
|
21
|
+
@app.route("/create-user", methods=["POST"])
|
|
22
|
+
def create_user():
|
|
23
|
+
username = request.json.get("username")
|
|
24
|
+
email = request.json.get("email")
|
|
25
|
+
|
|
26
|
+
if not username or not email:
|
|
27
|
+
return(jsonify({"message": "Missing fields"})), 400
|
|
28
|
+
|
|
29
|
+
new_user = User(username=username, email=email)
|
|
30
|
+
try:
|
|
31
|
+
db.session.add(new_user)
|
|
32
|
+
db.session.commit()
|
|
33
|
+
except Exception as e:
|
|
34
|
+
return jsonify({"message": str(e)}), 400
|
|
35
|
+
|
|
36
|
+
return jsonify({"message": "New User Created"}), 201
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@app.route("/update-users/<int:user_id>", methods=["PATCH"])
|
|
40
|
+
def update_user(user_id):
|
|
41
|
+
user = User.query.get(user_id)
|
|
42
|
+
if not user:
|
|
43
|
+
return jsonify({"message": "User not found"}), 404
|
|
44
|
+
data = request.json
|
|
45
|
+
if not data:
|
|
46
|
+
return jsonify({"message": "No data provided"}), 400
|
|
47
|
+
|
|
48
|
+
if "username" in data:
|
|
49
|
+
user.username = data["username"]
|
|
50
|
+
if "email" in data:
|
|
51
|
+
user.email = data["email"]
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
db.session.commit()
|
|
55
|
+
except Exception as e:
|
|
56
|
+
return jsonify({"message": str(e)}), 400
|
|
57
|
+
|
|
58
|
+
return jsonify({"message": "User Updated"}), 200
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@app.route("/delete-user/<int:user_id>", methods=["DELETE"])
|
|
62
|
+
def delete_user(user_id):
|
|
63
|
+
user = User.query.get(user_id)
|
|
64
|
+
if not user:
|
|
65
|
+
return jsonify({"message": "User not found"}), 404
|
|
66
|
+
try:
|
|
67
|
+
db.session.delete(user)
|
|
68
|
+
db.session.commit()
|
|
69
|
+
except Exception as e:
|
|
70
|
+
return jsonify({"message": str(e)}), 400
|
|
71
|
+
|
|
72
|
+
return jsonify({"message": "User Deleted"}), 200
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
# These are examples notes REST APIs
|
|
76
|
+
@app.route("/get-notes", methods=["GET"])
|
|
77
|
+
def get_notes():
|
|
78
|
+
notes = Note.query.all()
|
|
79
|
+
json_notes = list(map(lambda x: x.to_json(), notes))
|
|
80
|
+
return jsonify({"notes": json_notes}), 200
|
|
81
|
+
|
|
82
|
+
@app.route("/create-note", methods=["POST"])
|
|
83
|
+
def create_note():
|
|
84
|
+
title = request.json.get("title")
|
|
85
|
+
content = request.json.get("content")
|
|
86
|
+
|
|
87
|
+
if not title or not content:
|
|
88
|
+
return(jsonify({"message": "Missing fields"})), 400
|
|
89
|
+
|
|
90
|
+
new_note = Note(title=title, content=content)
|
|
91
|
+
try:
|
|
92
|
+
db.session.add(new_note)
|
|
93
|
+
db.session.commit()
|
|
94
|
+
except Exception as e:
|
|
95
|
+
return jsonify({"message": str(e)}), 400
|
|
96
|
+
|
|
97
|
+
return jsonify({"message": "New Note Created"}), 201
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@app.route("/update-notes/<int:note_id>", methods=["PATCH"])
|
|
101
|
+
def update_note(note_id):
|
|
102
|
+
note = Note.query.get(note_id)
|
|
103
|
+
if not note:
|
|
104
|
+
return jsonify({"message": "Note not found"}), 404
|
|
105
|
+
data = request.json
|
|
106
|
+
|
|
107
|
+
if not data:
|
|
108
|
+
return jsonify({"message": "No data provided for updation"})
|
|
109
|
+
|
|
110
|
+
if "title" in data:
|
|
111
|
+
note.title = data["title"]
|
|
112
|
+
if "content" in data:
|
|
113
|
+
note.content = data["content"]
|
|
114
|
+
|
|
115
|
+
try:
|
|
116
|
+
db.session.commit()
|
|
117
|
+
except Exception as e:
|
|
118
|
+
return jsonify({"message": str(e)}), 400
|
|
119
|
+
|
|
120
|
+
return jsonify({"message": "User Updated"}), 200
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@app.route("/delete-note/<int:note_id>", methods=["DELETE"])
|
|
124
|
+
def delete_note(note_id):
|
|
125
|
+
note = Note.query.get(note_id)
|
|
126
|
+
if not note:
|
|
127
|
+
return jsonify({"message": "Note not found"}), 404
|
|
128
|
+
try:
|
|
129
|
+
db.session.delete(note)
|
|
130
|
+
db.session.commit()
|
|
131
|
+
except Exception as e:
|
|
132
|
+
return jsonify({"message": str(e)}), 400
|
|
133
|
+
|
|
134
|
+
return jsonify({"message": "Note Deleted"}), 200
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
export const connectDB = async () => {
|
|
4
|
+
try {
|
|
5
|
+
await mongoose.connect(process.env.MONGO_URI);
|
|
6
|
+
console.log('MONGODB connected successfully!');
|
|
7
|
+
} catch (error) {
|
|
8
|
+
console.error("Error connecting to MongoDB: ", error);
|
|
9
|
+
process.exit(1);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Note } from "../models/noteModel.js"
|
|
2
|
+
import mongoose from "mongoose"
|
|
3
|
+
|
|
4
|
+
// Get All Notes
|
|
5
|
+
export const getAllNotes = async (req, res) => {
|
|
6
|
+
try {
|
|
7
|
+
const notes = await Note.find();
|
|
8
|
+
res.status(200).json(notes);
|
|
9
|
+
} catch (error) {
|
|
10
|
+
res.status(500).json({error: error.message});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Get A Single Note
|
|
15
|
+
export const getNoteByID = async (req, res) => {
|
|
16
|
+
try {
|
|
17
|
+
const { id } = req.params;
|
|
18
|
+
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
19
|
+
return res.status(404).json({message: "Note not found!"})
|
|
20
|
+
}
|
|
21
|
+
const note = await Note.findById(id);
|
|
22
|
+
if (!note) {
|
|
23
|
+
return res.status(404).json({message: "Note not found!"})
|
|
24
|
+
}
|
|
25
|
+
res.status(200).json(note);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
res.status(500).json({error: error.message});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Create A Note
|
|
32
|
+
export const createNote = async (req, res) => {
|
|
33
|
+
try {
|
|
34
|
+
const {title, content} = req.body;
|
|
35
|
+
if (!title || !content) {
|
|
36
|
+
return res.status(400).json({message: "Please fill in all the fields"})
|
|
37
|
+
}
|
|
38
|
+
const newNote = new Note({title, content});
|
|
39
|
+
const savedNote = await newNote.save();
|
|
40
|
+
res.status(201).json({message: "Note created successfully", createdNote: savedNote});
|
|
41
|
+
} catch (error) {
|
|
42
|
+
res.status(500).json({message: "Error creating note", error: error.message});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
// Update Note
|
|
48
|
+
export const updateNote = async (req, res) => {
|
|
49
|
+
try {
|
|
50
|
+
const {title, content} = req.body;
|
|
51
|
+
const updatedNote = await Note.findByIdAndUpdate(
|
|
52
|
+
req.params.id,
|
|
53
|
+
{
|
|
54
|
+
title,
|
|
55
|
+
content
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
new: true // Returns the updated note
|
|
59
|
+
}
|
|
60
|
+
)
|
|
61
|
+
if (!updatedNote) return res.status(404).json({message: "Note not found!"});
|
|
62
|
+
res.status(200).json({message: "Note updated successfully", updatedNote: updatedNote});
|
|
63
|
+
} catch (error) {
|
|
64
|
+
res.status(500).json({message: "Error updating note", error: error.message});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
// Delete Note
|
|
70
|
+
export const deleteNote = async (req, res) => {
|
|
71
|
+
try {
|
|
72
|
+
const deletedNote = await Note.findByIdAndDelete(req.params.id);
|
|
73
|
+
if (!deletedNote) return res.status(404).json({message: "Note not found"});
|
|
74
|
+
res.status(200).json({message: "Note deleted successfully", deletedNote});
|
|
75
|
+
} catch (error) {
|
|
76
|
+
res.status(500).json({message: "Error deleting note", error: error.message});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|