- Basically, an API (Application Programming Interface) is a set of defined rules that enable different applications to communicate with each other, like a bridge.
- So, if we have a database and we don't want other devices or subprograms accessing the database directly is implemented an API.
- Using an API we can guarantee security to our database, restrincting all subprograms and requests to a limited quantity of operations based on available endpoints.
- So, with an API we avoid to receive a "DROP DATABASE" comming from any application.
- Database: MySQL 8.0.33 (localhost)
- The API communicates with a MySQL database, which is running on a Docker container. This setup ensures portability and ease of deployment. The following tables are available in the database:
- Pedidos
- Produtos
- Categorias
- Clientes
- Lojas
- Locais
The API exposes the following endpoints for accessing and manipulating data:
/pedidos
- Retrieves all orders/pedidos/{id_pedido}
- Retrieves a specific order by its ID/produtos
- Retrieves all products/produtos/{id}
- Retrieves a specific product by its ID/lojas
- Retrieves all stores/lojas/{id}
- Retrieves a specific store by its ID/clientes
- Retrieves all customers/clientes/{id}
- Retrieves a specific customer by their ID/locais
- Retrieves all locations/locais/{UF}
- Retrieves locations by the specified state (e.g.,/locais/SP
for São Paulo)/categorias
- Retrieves all categories/categorias/{id}
- Retrieves a specific category by its ID/relatorio_vendas/produtos
- Retrieves a sales report for products/relatorio_vendas/lojas
- Retrieves a sales report for stores/relatorio_vendas/clientes
- Retrieves a sales report for customers
To run the API project with the MySQL database in a Docker container, you can use the following docker-compose.yml
configuration:
version: '3'
services:
mysql-development:
image: "mysql:${MYSQL_VERSION}"
environment:
MYSQL_HOST: "${MYSQL_HOST}"
MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD}"
MYSQL_DATABASE: "${MYSQL_DATABASE}"
ports:
- "3306:3306"
@api.get("/relatorio_vendas/produtos")
def get_df_relatorio_vendas_produto():
query = '''
SELECT
pedidos.ID_Produto,
pedidos.Qtd_Vendida,
pedidos.Preco_Unit,
pedidos.Custo_Unit,
produtos.Nome_Produto,
produtos.Marca_Produto
FROM pedidos
INNER JOIN produtos
ON pedidos.ID_Produto = produtos.ID_Produto;
'''
df = pd.DataFrame(engine.connect().execute(text(query)))
return df.to_json()
- Created by: Gabriel Zuany Duarte Vargas
- 2023, June
- Fell free to sugest new features!