added database lookup now need frontend
This commit is contained in:
parent
0647edf54a
commit
8f69bec6ca
33
main.py
33
main.py
@ -11,14 +11,6 @@ def get_db():
|
||||
db = sqlite3.connect("main.sqlite")
|
||||
return db
|
||||
|
||||
@app.route("/users", methods=["GET"])
|
||||
def get_users():
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT * FROM users")
|
||||
users = cursor.fetchall()
|
||||
return jsonify(users)
|
||||
|
||||
@app.route("/users", methods=["POST"])
|
||||
def create_user():
|
||||
db = get_db()
|
||||
@ -69,31 +61,14 @@ def login():
|
||||
|
||||
return response
|
||||
|
||||
@app.route("/users/<int:user_id>", methods=["GET"])
|
||||
def get_user(user_id):
|
||||
@app.route("/products.json")
|
||||
def products():
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
|
||||
user = cursor.fetchone()
|
||||
return jsonify(user)
|
||||
cursor.execute("SELECT * FROM products;")
|
||||
|
||||
@app.route("/users/<int:user_id>", methods=["PUT"])
|
||||
def update_user(user_id):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
name = request.form["name"]
|
||||
email = request.form["email"]
|
||||
cursor.execute("UPDATE users SET name = ?, email = ? WHERE id = ?", (name, email, user_id))
|
||||
db.commit()
|
||||
return jsonify({"message": "User updated successfully"})
|
||||
return jsonify(cursor.fetchall())
|
||||
|
||||
@app.route("/users/<int:user_id>", methods=["DELETE"])
|
||||
def delete_user(user_id):
|
||||
db = get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute("DELETE FROM users WHERE id = ?", (user_id,))
|
||||
db.commit()
|
||||
return jsonify({"message": "User deleted successfully"})
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
|
BIN
main.sqlite
BIN
main.sqlite
Binary file not shown.
11
setup.py
11
setup.py
@ -29,5 +29,16 @@ db.execute("CREATE TABLE IF NOT EXISTS token(" +
|
||||
"expiry INTEGER NOT NULL " +
|
||||
");")
|
||||
|
||||
db.execute("CREATE TABLE IF NOT EXISTS products(\
|
||||
name STRING NOT NULL,\
|
||||
category STRING,\
|
||||
price REAL NOT NULL,\
|
||||
specs STRING NOT NULL,\
|
||||
value REAL DEFAULT 0,\
|
||||
price REAL DEFAULT 0,\
|
||||
review REAL DEFAULT 0,\
|
||||
votes REAL DEFAULT 0\
|
||||
)")
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
21
test.py
Normal file → Executable file
21
test.py
Normal file → Executable file
@ -1,7 +1,18 @@
|
||||
from os import urandom
|
||||
from base64 import b64encode
|
||||
#!/usr/bin/env python3
|
||||
import sqlite3
|
||||
|
||||
rnd=urandom(16)
|
||||
rnd_bytes=b64encode(rnd).decode('utf-8')
|
||||
db = sqlite3.connect("main.sqlite")
|
||||
|
||||
print(rnd_bytes)
|
||||
db.execute('INSERT INTO products VALUES (\
|
||||
"Yuxin Little Magic", \
|
||||
"Rubiks Cube", \
|
||||
5.00, \
|
||||
"Really fast", \
|
||||
0.8, \
|
||||
0.8, \
|
||||
0, \
|
||||
0\
|
||||
)')
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
Loading…
Reference in New Issue
Block a user