def get(self): c.execute('SELECT * FROM orders o ' 'INNER JOIN customers c ON o.customer_id = c.id ' 'ORDER BY o.id') resp = [] for data in [x for x in c.fetchall()]: order_id = data[0] response = { "id": data[0], "total": data[2], "status": data[3], "created_at": data[4], "candelDate": data[5], "buyer": { "id": data[6], "name": data[7], "cpf": data[8], "email": data[9] }, "Items": [] } c.execute('SELECT p.id, p.sku, p.name, ot.amount, ot.price_unit, ot.total ' 'FROM orderItem ot ' 'INNER JOIN products p ON ot.product_id = p.id ' 'WHERE ot.order_id=(?)', (order_id,)) for item in c.fetchall(): response['Items'].append({ "product": { "id": item[0], "sku": item[1], "title": item[2] }, "amount": item[3], "price_unit": item[4], "total": item[5] }) resp.append(response) return make_response(jsonify(resp), 200)
def get(self): c.execute("SELECT * FROM products") data = [] for product in c.fetchall(): data.append({ "id": product[0], "sku": product[1], "name": product[2], "price": product[3], "created_at": product[4], "updated_at": product[5] }) return jsonify(data)
def checkApiKey(): if str(request.url_rule) == '/gettoken': return try: token = request.args['token'] except: return "API Key missing", 403 c.execute('SELECT * FROM tokens WHERE token=(?)', (token, )) for row in c.fetchall(): if token == row[1]: break else: return "Invalid key", 403
def put(self, order_id): data = request.get_json(force=True) c.execute('select exists(select 1 from orders where id=(?))', (order_id,)) if True not in c.fetchall()[0]: conn.close() return "Order ID not found", 404 try: c.execute("UPDATE orders SET status=(?), cancelDate=(?) WHERE id=(?)", (data['status'], str(datetime.now().isoformat()), order_id)) except IntegrityError as e: return str(e), 403 conn.commit() return data, 200
def get(self): c.execute("SELECT * FROM customers") data = [] for customer in c.fetchall(): data.append({ "id": customer[0], "name": customer[1], "cpf": customer[2], "email": customer[3], "created_at": customer[4], "updated_at": customer[5] }) return jsonify(data)