From 1c91c1a0db29038db70d7574a5d3e00f61ae282f Mon Sep 17 00:00:00 2001 From: Joao Date: Fri, 22 May 2026 22:00:16 -0300 Subject: [PATCH] =?UTF-8?q?Evolu=C3=A7=C3=A3o=20da=20apirest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apirest/.env.example | 7 +++++ apirest/__pycache__/db.cpython-311.pyc | Bin 904 -> 910 bytes apirest/app.py | 40 +++++++++++++++++++++++++ apirest/teste.py | 14 +++++++++ 4 files changed, 61 insertions(+) create mode 100644 apirest/.env.example create mode 100644 apirest/teste.py diff --git a/apirest/.env.example b/apirest/.env.example new file mode 100644 index 0000000..e68a7de --- /dev/null +++ b/apirest/.env.example @@ -0,0 +1,7 @@ +PORT=5000 + +DB_HOST= +DB_USER= +DB_PASSWORD= +DB_NAME= +DB_PORT= \ No newline at end of file diff --git a/apirest/__pycache__/db.cpython-311.pyc b/apirest/__pycache__/db.cpython-311.pyc index 257bc86e781048c1842db643972a270bf8b26cd7..c87b11f881f1bf8a7e668916c90be113ea6d765b 100644 GIT binary patch delta 63 zcmeBR?_=j)&dbZi00e(-32fvRXHvCxwu%WYPAw{qF*G+aF)%WVaY-%CF3B&5DM>9Z RNsTEe%1 L58CX`", methods=["GET"]) +def listar_produto_id(id): + conexao = get_connection() + cursor = conexao.cursor(dictionary=True) + + cursor.execute("Select * from produto where id = %s", (id,)) + produto = cursor.fetchone() + + cursor.close() + conexao.close() + + if produto is None: + return jsonify({"erro" : "Produto não encontrado"}), 404 + + return jsonify(produto) + +#============================================================= + +@app.route("/produtos",methods=["POST"]) +def criar_produto(): + dados = request.get_json() + 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 inválido"}), 400 + conexao = get_connection() + cursor = conexao.cursor() + sql = "insert into produto (nome, preco, estoque) values (%s, %s, %s)" + cursor.execute(sql,(nome, preco, estoque)) + novo_id = cursor.lastrowid + cursor.close() + conexao.close() + return jsonify({"id" : novo_id, "nome": nome, "preco" : preco, "estoque" : estoque}), 201 + + #Inicializa o servidor da APIRest if __name__ == "__main__": app.run(port=PORT, debug=True) \ No newline at end of file diff --git a/apirest/teste.py b/apirest/teste.py new file mode 100644 index 0000000..58ae9c3 --- /dev/null +++ b/apirest/teste.py @@ -0,0 +1,14 @@ +aluno = { + "nome" : "João", + "matricula" : "10011454", + "ira" : 8.5, + "idade" : 35, + "ativo" : True, + "dicionario" : { "teste" : "A" }, + "notas" : [] +} + +print(aluno["nome"]) +print(aluno.get("nome")) +print(aluno.get("dicionario").get("teste")) +print(aluno.keys()) \ No newline at end of file