-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethods.py
More file actions
194 lines (155 loc) · 6.86 KB
/
Methods.py
File metadata and controls
194 lines (155 loc) · 6.86 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
from db import db
from User import User
from Product import Product
from CartIntem import CartIntem
from flask import request, jsonify
from flask_login import login_user, logout_user, current_user
class Methods:
def registration_admin(self):
data=request.json
if 'username' in data:
admin=User(username=data['username'], password=data['password'], is_admin=data['is_admin'])
db.session.add(admin)
db.session.commit()
return jsonify({'message':'Registered successfully.'})
return jsonify({'message':'User already exists.'}), 409
def registration_user(self):
data=request.json
# Verifica se os campos obrigatórios estão na requisição
if 'username' not in data or not data['username'].strip():
return jsonify({'message': 'Username cannot be empty.'}), 400
if 'password' not in data or not data['password'].strip():
return jsonify({'message': 'Password cannot be empty.'}), 400
# Verifica se o usuário já existe no banco
existing_user = User.query.filter_by(username=data['username']).first()
if existing_user:
return jsonify({'message': 'User already exists.'}), 409
admin=User(username=data['username'], password=data['password'])
db.session.add(admin)
db.session.commit()
return jsonify({'message':'Registered successfully.'})
return jsonify({'message':'User already exists.'}), 409
def registration_user(self):
data=request.json
# Verifica se os campos obrigatórios estão na requisição
if 'username' not in data or not data['username'].strip():
return jsonify({'message': 'Username cannot be empty.'}), 400
if 'password' not in data or not data['password'].strip():
return jsonify({'message': 'Password cannot be empty.'}), 400
# Verifica se o usuário já existe no banco
existing_user = User.query.filter_by(username=data['username']).first()
if existing_user:
return jsonify({'message': 'User already exists.'}), 409
# Cria um novo usuário
user = User(username=data['username'], password=data['password'])
db.session.add(user)
db.session.commit()
return jsonify({'message': 'Registered successfully.'}), 201
def login(self):
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
def logout(self):
logout_user()
return jsonify({"message":"Logout successfully"})
# Adicionando porduto
def add_products(self):
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({"message":"Invalid product data"}), 400
#Deletando produro
def delete_product(self,product_id):
#Recuperar o produto da base de dados
# Verificar se o porduto existe
#Se existe, apagar da base de dado
#Se não existe, retornar 404 not found
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
# Buscanndo o porduto
def get_product_details(self,product_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"}), 400
# Atualiza produto
def upade_product(self,product_id):
product= Product.query.get(product_id)
if not product:
return jsonify({"message":"Product not found"}), 404
data=request.json
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"})
# Apresentar todos os produtos
def get_products(self):
products=Product.query.all()
# if products:
products_list=[]
for product in products:
product_data={
"id" :product.id,
"name" :product.name,
"price" :product.price
}
products_list.append(product_data)
return jsonify(products_list)
def add_to_cart(self,product_id):
# User
user=User.query.get(int(current_user.id))
# Porduct
product=Product.query.get(product_id)
if user and product:
cart_item= CartIntem(user_id=user.id, product_id=product.id)
db.session.add(cart_item)
db.session.commit()
return jsonify({'message': 'Item added to the car sucessfully'})
return jsonify({'message': 'Failed to add item to the cart'}), 400
def remove_from_car(self,product_id):
cart_item=CartIntem.query.filter_by(user_id=current_user.id,product_id=product_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
def view_cat(self):
user= User.query.get(int(current_user.id))
cart_items=user.cart
cart_content=[]
for cart_item in cart_items:
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)
def checkout(self):
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.'})