mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-20 15:11:21 +00:00
feat: first commit
This commit is contained in:
parent
0152fbd177
commit
c7195aaa68
4 changed files with 118 additions and 0 deletions
5
.env.example
Normal file
5
.env.example
Normal file
|
@ -0,0 +1,5 @@
|
|||
DB_URL=
|
||||
DB_PORT=
|
||||
DB_DATABASE=
|
||||
DB_USERNAME=
|
||||
DB_PASSWORD=
|
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
.vscode/
|
||||
__pycache__/
|
||||
|
||||
.venv/
|
||||
|
||||
.env
|
||||
!.env.example
|
||||
|
||||
.DS_Store
|
70
app.py
Normal file
70
app.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
import os
|
||||
import psycopg2
|
||||
import time as time_2
|
||||
from pydantic import BaseModel
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, APIRouter
|
||||
|
||||
load_dotenv()
|
||||
|
||||
conn_param = "host=%s port=%s dbname=%s user=%s password=%s" % (
|
||||
os.getenv("DB_URL"),
|
||||
os.getenv("DB_PORT"),
|
||||
os.getenv("DB_DATABASE"),
|
||||
os.getenv("DB_USERNAME"),
|
||||
os.getenv("DB_PASSWORD")
|
||||
)
|
||||
|
||||
app = FastAPI()
|
||||
router = APIRouter()
|
||||
|
||||
class Balance(BaseModel):
|
||||
name: str
|
||||
date: int
|
||||
price: int
|
||||
|
||||
@router.get("/")
|
||||
def index():
|
||||
return "Hello, World!"
|
||||
|
||||
@router.post("/balance")
|
||||
def insert(balance: Balance):
|
||||
started = time_2.time()
|
||||
conn = psycopg2.connect(conn_param)
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"insert into balset(name, date, price) values (%s, %s, %s);",
|
||||
(balance.name, balance.date, balance.price)
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
return {"ok": 1, "respond_time": f"{time_2.time() - started}ms", "name": balance.name}
|
||||
|
||||
@router.get("/balance/{id}")
|
||||
def query(id):
|
||||
conn = psycopg2.connect(conn_param)
|
||||
cur = conn.cursor()
|
||||
cur.execute(
|
||||
"select * from balset where id = %s",
|
||||
id
|
||||
)
|
||||
|
||||
data = cur.fetchone()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
return {"ok": 1, "data": {"id": data[0], "name": data[1], "date": data[2], "price": data[3]}}
|
||||
|
||||
@router.patch("/balance/{id}")
|
||||
def update():
|
||||
return {"ok": 1, "id": "test"}
|
||||
|
||||
@router.delete("/balance/{id}")
|
||||
def delete():
|
||||
return {"ok": 1, "id": "test"}
|
||||
|
||||
app.include_router(router=router)
|
34
requirements.txt
Normal file
34
requirements.txt
Normal file
|
@ -0,0 +1,34 @@
|
|||
annotated-types==0.7.0
|
||||
anyio==4.4.0
|
||||
certifi==2024.8.30
|
||||
click==8.1.7
|
||||
dnspython==2.6.1
|
||||
email_validator==2.2.0
|
||||
fastapi==0.114.0
|
||||
fastapi-cli==0.0.5
|
||||
h11==0.14.0
|
||||
httpcore==1.0.5
|
||||
httptools==0.6.1
|
||||
httpx==0.27.2
|
||||
idna==3.8
|
||||
Jinja2==3.1.4
|
||||
markdown-it-py==3.0.0
|
||||
MarkupSafe==2.1.5
|
||||
mdurl==0.1.2
|
||||
psycopg2-binary==2.9.9
|
||||
pydantic==2.9.1
|
||||
pydantic_core==2.23.3
|
||||
Pygments==2.18.0
|
||||
python-dotenv==1.0.1
|
||||
python-multipart==0.0.9
|
||||
PyYAML==6.0.2
|
||||
rich==13.8.0
|
||||
shellingham==1.5.4
|
||||
sniffio==1.3.1
|
||||
starlette==0.38.5
|
||||
typer==0.12.5
|
||||
typing_extensions==4.12.2
|
||||
uvicorn==0.30.6
|
||||
uvloop==0.20.0
|
||||
watchfiles==0.24.0
|
||||
websockets==13.0.1
|
Loading…
Reference in a new issue