Commit 4194e761 authored by shalithadeshan's avatar shalithadeshan

add database api file

parent 15ec85bc
import json
import os
from flask import Flask, request
from flask_cors import CORS, cross_origin
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="dog_research"
)
mycursor = mydb.cursor()
app = Flask(__name__)
cors = CORS(app)
uploadPath = "uploads"
@app.route('/predict', methods=['POST'])
@cross_origin()
def uploadImage():
print(os.getcwd())
f = request.files['file']
splits = f.filename.split("/")
print(splits[len(splits) - 1])
# os.remove(os.path.join(uploadPath, "upload.jpg"))
f.save(os.path.join(uploadPath, "upload.jpg"))
@app.route("/signup", methods=['POST'])
def signup():
data = request.get_json()
print(data)
sql = "INSERT INTO users (email, password) VALUES (%s, %s)"
val = (data['email'], data['password'])
mycursor.execute(sql, val)
mydb.commit()
mycursor.execute("SELECT id FROM users ORDER BY id DESC LIMIT 1")
# myresult = mycursor.fetchall()
id = 0
for (id) in mycursor:
# id = row['id']
id = id
print(id)
return {"id": id[0]}
@app.route("/login", methods=['POST'])
def login():
data = request.get_json()
print(data)
id = 0
try:
mycursor.execute(
"SELECT id FROM users WHERE email='" + data['email'] + "' AND password='" + data['password'] + "'")
# myresult = mycursor.fetchall()
for (id) in mycursor:
# id = row['id']
id = id
print(id)
return {"id": id[0]}
except:
return {"id": 0}
@app.route("/addPet", methods=['POST'])
def addPet():
data = request.get_json()
print(data)
sql = "INSERT INTO pets (name, age, weight, gender, breed) VALUES (%s, %s, %s, %s, %s)"
val = (data['name'], data['age'], data['weight'], data['gender'], data['breed'])
mycursor.execute(sql, val)
mydb.commit()
mycursor.execute("SELECT id FROM pets ORDER BY id DESC LIMIT 1")
# myresult = mycursor.fetchall()
id = 0
for (id) in mycursor:
# id = row['id']
id = id
print(id)
return {"id": id[0]}
@app.route("/addQuestion", methods=['POST'])
def addQuestion():
data = request.get_json()
print(data)
sql = "INSERT INTO questions (question, description, tag, votes) VALUES (%s, %s, %s, %s)"
val = (data['question'], data['description'], data['tag'], data['votes'])
mycursor.execute(sql, val)
mydb.commit()
mycursor.execute("SELECT id FROM questions ORDER BY id DESC LIMIT 1")
# myresult = mycursor.fetchall()
id = 0
for (id) in mycursor:
# id = row['id']
id = id
print(id)
return {"id": id[0]}
@app.route("/setQuestionVote", methods=['POST'])
def setQuestionVote():
data = request.get_json()
print(data)
sql = "UPDATE questions SET votes = " + str(data['votes']) + " WHERE id =" + str(data['id'])
mycursor.execute(sql)
mydb.commit()
return {"task": "Updated"}
@app.route("/addReview", methods=['POST'])
def addReview():
data = request.get_json()
print(data)
sql = "INSERT INTO reviews (review, post_id) VALUES (%s, %s)"
val = (data['review'], data['post_id'])
mycursor.execute(sql, val)
mydb.commit()
mycursor.execute("SELECT id FROM reviews ORDER BY id DESC LIMIT 1")
# myresult = mycursor.fetchall()
id = 0
for (id) in mycursor:
# id = row['id']
id = id
print(id)
return {"id": id[0]}
@app.route("/getPets", methods=['GET', 'POST'])
def getPets():
try:
mycursor.execute("SELECT * FROM pets")
myresult = mycursor.fetchall()
return json.dumps(myresult)
except:
return {"id": 0}
@app.route("/getQuestions", methods=['GET', 'POST'])
def getQuestions():
try:
mycursor.execute("SELECT * FROM questions")
myresult = mycursor.fetchall()
return json.dumps(myresult)
except:
return {"id": 0}
@app.route("/getReviews", methods=['GET', 'POST'])
def getReviews():
try:
mycursor.execute("SELECT * FROM reviews")
myresult = mycursor.fetchall()
return json.dumps(myresult)
except:
return {"id": 0}
if __name__ == '__main__':
app.run(debug=True, port=5005, host="0.0.0.0")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment