-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
245 lines (183 loc) · 6.87 KB
/
Copy pathapplication.py
File metadata and controls
245 lines (183 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from flask_login import (UserMixin, login_user, LoginManager,
login_required, logout_user, current_user)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///ecommerce.db'
login_manager = LoginManager()
db = SQLAlchemy(app)
login_manager.init_app(app)
login_manager.login_view = 'login'
CORS(app)
class User(db.Model, UserMixin):
""" User model for authentication """
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(80), nullable=False)
cart = db.relationship('CartItem', backref='user', lazy=True)
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
price = db.Column(db.Double, nullable=False)
description = db.Column(db.Text, nullable=False)
class CartItem(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
product_id = db.Column(
db.Integer, db.ForeignKey('product.id'),
nullable=False
)
# Product
# -----------------------------------------------------------------------------
@app.route("/api/products/add", methods=["POST"])
@login_required
def add_product():
""" Add a new product to the database """
data = request.json
if 'name' in data and 'price' in data:
product = Product(
name=data["name"],
price=data["price"],
description=data.get("description", "")
)
db.session.add(product)
db.session.commit()
return jsonify({"message": "Product added successfully"})
return jsonify({"error": "Invalid product data"}), 400
@app.route("/api/products/delete/<int:product_id>", methods=["DELETE"])
@login_required
def delete_product(product_id):
""" Delete a product by its ID """
product = Product.query.get(product_id)
if product:
db.session.delete(product)
db.session.commit()
return jsonify({"message": "Product deleted successfully"})
return jsonify({"message": "Product not found"}), 404
@app.route("/api/products/<int:product_id>", methods=["GET"])
def get_product_details(product_id):
""" Get details of a product by its ID """
product = Product.query.get(product_id)
if product:
return jsonify({
"id": product.id,
"name": product.name,
"price": product.price,
"description": product.description
})
return jsonify({"message": "Product not found"}), 404
@app.route("/api/products/update/<int:product_id>", methods=["PUT"])
@login_required
def update_product(product_id):
""" Update a product by its ID """
data = request.json
product = Product.query.get(product_id)
if not product:
return jsonify({"message": "Product not found"}), 404
if 'name' in data:
product.name = data["name"]
if 'price' in data:
product.price = data["price"]
if 'description' in data:
product.description = data["description"]
db.session.commit()
return jsonify({"message": "Product updated successfully"})
@app.route("/api/products", methods=["GET"])
def get_products():
""" Get all products """
products = Product.query.all()
product_list = []
for product in products:
product_list.append({
"id": product.id,
"name": product.name,
"price": product.price
})
return jsonify(product_list)
# Autentication Endpoints
# -----------------------------------------------------------------------------
@login_manager.user_loader
def load_user(user_id):
""" Load user for Flask-Login """
return User.query.get(int(user_id))
@app.route("/api/login", methods=["POST"])
def login():
""" User login endpoint """
data = request.json
user = User.query.filter_by(username=data.get("username")).first()
if user and data.get("password") == user.password:
login_user(user)
return jsonify({"message": "Logged in successfully"})
return jsonify({"message": "Unauthorized. Invalid credentials"}), 401
@app.route("/api/logout", methods=["POST"])
@login_required
def logout():
""" User logout endpoint """
logout_user()
return jsonify({"message": "Logged out successfully"})
# Cart
# -----------------------------------------------------------------------------
@app.route("/api/cart/add/<int:product_id>", methods=["POST"])
@login_required
def add_to_cart(product_id):
""" Add a product to the user's cart """
user = User.query.get(int(current_user.id))
product = Product.query.get(product_id)
if user and product:
cart_item = CartItem(
user_id=user,
product_id=product
)
db.session.add(cart_item)
db.session.commit()
return jsonify({"message", "Item added to the cart successfully"})
return jsonify({"message", "Failed to add item to the cart"}), 400
@app.route("/api/cart/remove/<int:item_id>", methods=["DELETE"])
@login_required
def delete_from_cart(project_id):
""" Remove a product from the user's cart """
cart_item = CartItem.query.filter_by(
user_id=current_user.id,
product_id=project_id
).first()
if cart_item:
db.session.delete(cart_item)
db.session.commit()
return jsonify({"message": "Item removed from the cart successfully"})
return jsonify({"message": "Failed to remove item from the cart"}), 400
@app.route("/api/cart", methods=["GET"])
@login_required
def view_cart():
""" View the user's cart """
user = User.query.get(int(current_user.id))
cart_items = user.cart
cart_content = []
for cart_item in cart_items:
# fazer essa requisição fora do for, prezando a performance
product = Product.query.get(cart_item.product_id)
cart_content.append({
"id": cart_item.id,
"user_id": cart_item.user_id,
"product_id": cart_item.product_id,
"product_name": product.name,
"product_price": product.price
})
return jsonify(cart_content)
@app.route("api/cart/checkout", methods=["POST"])
@login_required
def checkout():
""" Checkout the user's cart and clear it """
user = User.query.get(int(current_user.id))
cart_items = user.cart
for cart_item in cart_items:
db.session.delete(cart_item)
db.session.commit()
return jsonify({"message": "Checkout successful. Cart has been cleared."})
# -----------------------------------------------------------------------------
@app.route("/")
def initial():
return "API up and running!"
if __name__ == "__main__":
app.run(debug=True)