Skip to content

Commit

Permalink
Add fastapi route (#2)
Browse files Browse the repository at this point in the history
* Add fastapi routers

* Add payment route connected to Oracle DB

* Remove patch constraints from oracle package

* Romove stringified nulls from db

* fix categoria property insert

* Add python-dotenv support for configs
  • Loading branch information
andrelucassc authored Jul 21, 2023
1 parent 8afdbbd commit 52a56d3
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# IDE
.vscode
Empty file added app/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from functools import lru_cache
from typing import Union

from pydantic import BaseSettings


class Settings(BaseSettings):
db_client_lib: str
db_dsn: str
db_user: str = "dbuser"
db_password: str = "dbpassword"
db_wallet: str

default_payment_user: str

class Config:
env_file = "env"
env_file_encoding = "utf-8"


@lru_cache(maxsize=None)
def get_settings() -> Settings:
"""Get settigns for the DB"""
return Settings()
9 changes: 9 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi import FastAPI

from .routers.payments import router

app = FastAPI(
title="Personal Finance API - PFA",
version="0.0.1",
)
app.include_router(router)
Empty file added app/routers/__init__.py
Empty file.
87 changes: 87 additions & 0 deletions app/routers/payments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from datetime import datetime
from typing import Optional

import oracledb
from fastapi import APIRouter
from loguru import logger
from pydantic import BaseModel

from app.config import get_settings

router = APIRouter(prefix="/pay")
settings = get_settings()

oracledb.init_oracle_client(lib_dir=settings.db_client_lib)


class Payment(BaseModel):
"""Represents one payment in the database"""

ITEM: str
DATA_PAGAMENTO: str
CATEGORIA: str
PRECO_TOTAL: float
LOJA: str
METODO_PAGAMENTO: str
PRECO_DOLAR: Optional[float] = None
PRECO_MOEDA_ALTERNATIVA: Optional[float] = None
NOME_MOEDA_ALTERNATIVA: Optional[str] = None
NOME_PESSOA_PAGADOR: str = settings.default_payment_user
FLAG_PAGO: int = 0
URL: Optional[str] = None
TAGS: Optional[str] = None
PRESENTEADO: Optional[str] = None
FLAG_EM_ROTA: int = 0
SK_SUBSCRIPTION: Optional[int] = None
EMOJI: Optional[str] = None
ID: Optional[int] = None


@router.post("/")
async def register_payment(payment: Payment):
with oracledb.connect(
dsn=settings.db_dsn,
user=settings.db_user,
password=settings.db_password,
wallet_location=settings.db_wallet,
) as connection:
with connection.cursor() as cursor:
sql = "SELECT MAX(ID) FROM PAGAMENTOS"
max_id = [f for f in cursor.execute(sql)]
logger.debug("DB responded with {response}", response=str(max_id))
with connection.cursor() as cursor:
insert_statement = (
"INSERT INTO PAGAMENTOS (ID, ITEM, DATA_PAGAMENTO, CATEGORIA, PRECO_TOTAL"
+ ", LOJA, METODO_PAGAMENTO, PRECO_DOLAR, PRECO_MOEDA_ALTERNATIVA"
+ ", NOME_MOEDA_ALTERNATIVA, NOME_PESSOA_PAGADOR, FLAG_PAGO"
+ ", URL, TAGS, PRESENTEADO, FLAG_EM_ROTA, EMOJI)"
+ " VALUES (:newid, :newitem, :newdate, :newcategory, :newprice"
+ ", :newstore, :newmethod, :dolar, :altcoin, :altcoinname"
+ ", :person, :payed, :url, :tags, :present, :inroute, :emoji)"
)
payment.ID = max_id[0][0] + 1
cursor.execute(
insert_statement,
newid=payment.ID,
newitem=payment.ITEM,
newdate=datetime.strptime(payment.DATA_PAGAMENTO, "%Y-%m-%d"),
newcategory=payment.CATEGORIA,
newprice=payment.PRECO_TOTAL,
newstore=payment.LOJA,
newmethod=payment.METODO_PAGAMENTO,
dolar=payment.PRECO_DOLAR,
altcoin=payment.PRECO_MOEDA_ALTERNATIVA,
altcoinname=payment.NOME_MOEDA_ALTERNATIVA,
person=payment.NOME_PESSOA_PAGADOR,
payed=payment.FLAG_PAGO,
url=payment.URL,
tags=payment.TAGS,
present=payment.PRESENTEADO,
inroute=payment.FLAG_EM_ROTA,
emoji=payment.EMOJI,
)

connection.commit()
logger.info("Payment registered")
logger.debug("Payment: {payment}", payment=str(payment))
return {"message": "Payment registered"}
214 changes: 166 additions & 48 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ readme = "README.md"
python = "^3.8"
fastapi = "^0.95"
pydantic = "^1.10"
cx-Oracle-async = "^0.3"
loguru = "^0.6"
uvicorn = {extras = ["standard"], version = "^0.21.1"}
oracledb = "^1.3"
python-dotenv = "^1.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "*"
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[isort]
profile = black
skip = [".gitignore", ".dockerignore"]
sections = FUTURE,STDLIB,THIRDPARTY,LOCALFOLDER
sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER

[flake8]
ignore = E203, E266, E501, W503, F403, F401
Expand Down

0 comments on commit 52a56d3

Please sign in to comment.