def post(self): data = request.get_json(force=True) try: c.execute( "INSERT INTO products (id, sku, name, price, created_at, updated_at) " "VALUES (?, ?, ?, ?, ?, ?)", (data['id'], data['sku'], data['name'], data['price'], data['created_at'], data['updated_at'])) except IntegrityError as e: return str(e), 403 conn.commit() return data, 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)
def post(self): data = request.get_json(force=True) if not validar_cpf(data['cpf']): return "CPF INVALIDO", 403 try: c.execute( "INSERT INTO customers (id, name, cpf, email, created_at, updated_at) " "VALUES (?, ?, ?, ?, ?, ?)", (data['id'], data['name'], data['cpf'], data['email'], data['created_at'], data['updated_at'])) except IntegrityError as e: return str(e), 403 conn.commit() return data, 200
def generateToken(): token = ''.join( random.sample( string.ascii_letters + string.ascii_uppercase + string.digits, 50)) created_at = datetime.now().isoformat() expire_date = (datetime.now() + timedelta(minutes=15)).isoformat() c.execute( "INSERT INTO tokens (token, created_at, expire_date) VALUES (?, ?, ?)", (token, created_at, expire_date)) conn.commit() response = { "token": token, "created_at": created_at, "expire_date": expire_date } return jsonify(response), 200
def post(self): data = request.get_json(force=True) try: c.execute('INSERT INTO orders (id, created_at, status, total, customer_id) ' 'VALUES (?, ?, ?, ?, ?)', (data['id'], data['created_at'], data['status'], data['total'], data['buyer']['id'])) for item in data['items']: c.execute("INSERT INTO orderItem (amount, price_unit, total, order_id, product_id) " "VALUES (?, ?, ?, ?, ?)", (item['amount'], item['price_unit'], item['total'], data['id'], item['product']['id'])) except IntegrityError as e: return str(e), 403 conn.commit() return data, 200
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)