Compare commits
No commits in common. "45f4b502330d20d15c98c9cdaf95243acf810ccc" and "f97bbf724414c44564599d5eb8787f0297721dcb" have entirely different histories.
45f4b50233
...
f97bbf7244
|
|
@ -1,2 +0,0 @@
|
||||||
.venv/
|
|
||||||
.env
|
|
||||||
Binary file not shown.
|
|
@ -1,79 +0,0 @@
|
||||||
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()
|
|
||||||
#Carrega variável de ambiente PORT. Essa variável contém a porta TCP utilizada na APIREST
|
|
||||||
PORT = os.getenv("PORT")
|
|
||||||
|
|
||||||
#Cria objeto Flask. Esse objeto é necessário para utilização do FLASK
|
|
||||||
#(módulo python para criação de APIs REST).
|
|
||||||
app = Flask(__name__)
|
|
||||||
#Aplica cors no objeto Flask
|
|
||||||
CORS(app)
|
|
||||||
|
|
||||||
#Criar rota de saúde da aplicação
|
|
||||||
@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)
|
|
||||||
|
|
||||||
cursor.execute("select * from produto")
|
|
||||||
produtos = cursor.fetchall()
|
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
conexao.close()
|
|
||||||
|
|
||||||
return jsonify(produtos)
|
|
||||||
|
|
||||||
#=============================================================
|
|
||||||
|
|
||||||
@app.route("/produtos/<int:id>", 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))
|
|
||||||
conexao.commit()
|
|
||||||
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)
|
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
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")
|
|
||||||
)
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
<!DOCTYPE html>
|
|
||||||
<html lang="pt-br">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<link rel="stylesheet" href="style.css">
|
|
||||||
<title>Produtos</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1> Lista de produtos</h1>
|
|
||||||
<main>
|
|
||||||
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Nome</th>
|
|
||||||
<th>Preço</th>
|
|
||||||
<th>Estoque</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody id="conteudo_tabela">
|
|
||||||
|
|
||||||
</tbody>
|
|
||||||
|
|
||||||
</table>
|
|
||||||
</main>
|
|
||||||
<script src=""script.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
async function carregarProdutos(){
|
|
||||||
const resposta = await fetch("http://localhost:5000/produtos");
|
|
||||||
if (!resposta.ok){
|
|
||||||
throw new Error("Não foi possível carregar a lista")
|
|
||||||
}
|
|
||||||
const produtos = await resposta.join();
|
|
||||||
|
|
||||||
let tbody = document.querySelector("tbody");
|
|
||||||
|
|
||||||
produtos.forEach(produto =>{
|
|
||||||
let linha = document.createElement("tr")
|
|
||||||
linha.innerHTML = `
|
|
||||||
<td>${produto.nome}</td>
|
|
||||||
<td>${produto.preco}</td>
|
|
||||||
<td>${produto.estoque}</td>
|
|
||||||
`
|
|
||||||
|
|
||||||
tbody.appendChild
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
* {
|
|
||||||
margin : o;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1{
|
|
||||||
width : 300px;
|
|
||||||
background-color: brown;
|
|
||||||
padding: 20px;
|
|
||||||
border: 10px solid black;
|
|
||||||
text-align: center;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
blinker==1.9.0
|
|
||||||
click==8.3.3
|
|
||||||
colorama==0.4.6
|
|
||||||
Flask==3.1.3
|
|
||||||
flask-cors==6.0.2
|
|
||||||
itsdangerous==2.2.0
|
|
||||||
Jinja2==3.1.6
|
|
||||||
MarkupSafe==3.0.3
|
|
||||||
mysql-connector-python==9.7.0
|
|
||||||
python-dotenv==1.2.2
|
|
||||||
Werkzeug==3.1.8
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
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 inicial do banco
|
|
||||||
insert into produto (nome,preco,estoque)
|
|
||||||
values ('Bola',12.50,10);
|
|
||||||
|
|
||||||
#Exemplo de consulta
|
|
||||||
insert into produto (nome,preco,estoque)
|
|
||||||
values ('PS5',3950.50,5);
|
|
||||||
|
|
||||||
#select * from produto;
|
|
||||||
|
|
||||||
#delete from produto where id
|
|
||||||
|
|
@ -1,20 +1,5 @@
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import scrolledtext
|
from tkinter import scrolledtext
|
||||||
import socket
|
|
||||||
import threading
|
|
||||||
|
|
||||||
def ouvir_servidor(socket_servidor):
|
|
||||||
while True:
|
|
||||||
mensagem = socket_servidor.recv(80000)
|
|
||||||
area_mensagens.config(state="normal")
|
|
||||||
area_mensagens.insert(tk.END, mensagem.decode() + "\n")
|
|
||||||
area_mensagens.config(state="disabled")
|
|
||||||
|
|
||||||
socket_servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
socket_servidor.connect(("10.209.1.45",5000))
|
|
||||||
|
|
||||||
thread = threading.Thread(target=ouvir_servidor, args=[socket_servidor,])
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
janela = tk.Tk()
|
janela = tk.Tk()
|
||||||
janela.title("Chat do IFRN")
|
janela.title("Chat do IFRN")
|
||||||
|
|
@ -26,13 +11,7 @@ entrada_mensagem = tk.Entry(janela, width=80)
|
||||||
entrada_mensagem.pack()
|
entrada_mensagem.pack()
|
||||||
|
|
||||||
def clicar():
|
def clicar():
|
||||||
mensagem = entrada_mensagem.get()
|
print("Fui clicado")
|
||||||
print(mensagem)
|
|
||||||
socket_servidor.send(mensagem.encode())
|
|
||||||
entrada_mensagem.delete(0, tk.END)
|
|
||||||
area_mensagens.config(state="normal")
|
|
||||||
area_mensagens.insert(tk.END, mensagem + "\n")
|
|
||||||
area_mensagens.config(state="disabled")
|
|
||||||
|
|
||||||
botao = tk.Button(janela, text="Enviar", command=clicar)
|
botao = tk.Button(janela, text="Enviar", command=clicar)
|
||||||
botao.pack()
|
botao.pack()
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ clientes = []
|
||||||
|
|
||||||
def ouvir_cliente(cliente):
|
def ouvir_cliente(cliente):
|
||||||
while True:
|
while True:
|
||||||
mensagem = cliente.recv(80000)
|
mensagem = cliente.recv(1024)
|
||||||
for socket_cliente in clientes:
|
for socket_cliente in clientes:
|
||||||
if socket_cliente != cliente:
|
if socket_cliente != cliente:
|
||||||
socket_cliente.send(mensagem)
|
socket_cliente.send(mensagem)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue