2026-05-22 22:44:04 +00:00
|
|
|
from flask import Flask, jsonify, request
|
|
|
|
|
from flask_cors import CORS
|
|
|
|
|
import os
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
from db import get_connection
|
|
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
|
PORT = os.getenv("PORT")
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
CORS(app)
|
|
|
|
|
|
|
|
|
|
@app.route("/health")
|
|
|
|
|
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)
|
|
|
|
|
|
2026-05-22 23:18:42 +00:00
|
|
|
cursor.execute("Select * from produto")
|
2026-05-22 22:44:04 +00:00
|
|
|
produtos = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
|
conexao.close()
|
|
|
|
|
|
|
|
|
|
return jsonify(produtos)
|
|
|
|
|
|
2026-05-22 23:18:42 +00:00
|
|
|
@app.route("/produtos/<int:id>", methods=["GET"])
|
|
|
|
|
def listar_produto_id(id):
|
|
|
|
|
conexao = get_connection()
|
|
|
|
|
cursor = conexao.cursor(dictionary=True)
|
|
|
|
|
|
2026-05-22 23:38:09 +00:00
|
|
|
cursor.execute("Select * from produto where id = %s", (id,))
|
2026-05-22 23:18:42 +00:00
|
|
|
produto = cursor.fetchone()
|
|
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
|
conexao.close()
|
|
|
|
|
|
|
|
|
|
if produto is None:
|
|
|
|
|
return jsonify({"erro" : "Produto nao encontrado"}), 404
|
|
|
|
|
|
|
|
|
|
return jsonify(produto)
|
2026-05-22 23:38:09 +00:00
|
|
|
@app.route("/proddutos",methods=["POST"])
|
|
|
|
|
def criar_produto():
|
|
|
|
|
dados = request.get_jason()
|
|
|
|
|
|
|
|
|
|
nome = dados.get("nome")
|
|
|
|
|
preco = dados.get("preco")
|
|
|
|
|
estoque = dados.get("estoque")
|
|
|
|
|
|
|
|
|
|
if nome is None or preco is None or estoque is None
|
|
|
|
|
return jsonify({"erro" : "Produto invalido"}), 400
|
|
|
|
|
|
|
|
|
|
conexao = get_connection()
|
|
|
|
|
cursor = conexao.cursor()
|
|
|
|
|
|
2026-05-22 23:18:42 +00:00
|
|
|
|
2026-05-22 22:44:04 +00:00
|
|
|
if __name__ == "__main__":
|
2026-05-22 23:18:42 +00:00
|
|
|
app.run(port=PORT, debug=True)
|