diff --git a/apirest/__pycache__/db.cpython-311.pyc b/apirest/__pycache__/db.cpython-311.pyc new file mode 100644 index 0000000..257bc86 Binary files /dev/null and b/apirest/__pycache__/db.cpython-311.pyc differ diff --git a/apirest/app.py b/apirest/app.py index 2e4e94f..1625198 100644 --- a/apirest/app.py +++ b/apirest/app.py @@ -2,6 +2,7 @@ from flask import Flask, jsonify, request from flask_cors import CORS import os from dotenv import load_dotenv +from db import get_connection #Carrega arquivo de variáveis de ambiente (.env) load_dotenv() @@ -19,6 +20,19 @@ CORS(app) def health(): return jsonify({"message" : "API Flask version 1.0!!!"}) +@app.route("/produtos",methods=["GET"]) +def listar_produtos(): + conexao = get_connection() + cursor = conexao.cursor(dictionary=True) + + cursor.execute("select * from produto") + produtos = cursor.fetchall() + + cursor.close() + conexao.close() + + return jsonify(produtos) + #Inicializa o servidor da APIRest if __name__ == "__main__": app.run(port=PORT, debug=True) \ No newline at end of file diff --git a/apirest/db.py b/apirest/db.py new file mode 100644 index 0000000..2eb1008 --- /dev/null +++ b/apirest/db.py @@ -0,0 +1,17 @@ +import os +import mysql.connector +from dotenv import load_dotenv + +load_dotenv() + +def get_connection(): + return mysql.connector.connect( + host=os.getenv("DB_HOST"), + user=os.getenv("DB_USER"), + password=os.getenv("DB_PASSWORD"), + database=os.getenv("DB_NAME"), + port=os.getenv("DB_PORT") + ) + + + diff --git a/apirest/scripts.sql b/apirest/scripts.sql new file mode 100644 index 0000000..19253e6 --- /dev/null +++ b/apirest/scripts.sql @@ -0,0 +1,16 @@ +#Criar tabela produto +create table produto ( + id int auto_increment primary key, + nome varchar(100) not null, + preco decimal(10,2) not null, + estoque int not null +); + +#Carga incial do banco +insert into produto (nome,preco,estoque) +values ('Bola',12.50,10); +insert into produto (nome,preco,estoque) +values ('PS5',3950.50,5); + +#Exemplo de consulta +#select * from produto;