mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-20 15:11:21 +00:00
feat: middle save
This commit is contained in:
parent
8d628e4826
commit
38c22e4512
6 changed files with 62 additions and 43 deletions
1
Makefile
1
Makefile
|
@ -1 +0,0 @@
|
|||
|
21
generate.py
21
generate.py
|
@ -1,18 +1,23 @@
|
|||
import psycopg2
|
||||
import psycopg2, os
|
||||
from getpass import getpass
|
||||
from util.config import conn_param
|
||||
|
||||
def __main__():
|
||||
conn = psycopg2.connect(conn_param)
|
||||
cur = conn.cursor()
|
||||
|
||||
try:
|
||||
open("./load.txt", "r")
|
||||
print("server already initialized")
|
||||
except:
|
||||
cur.execute(
|
||||
"""
|
||||
create table account(
|
||||
name varchar(25),
|
||||
username varchar(25) primary key,
|
||||
username varchar(25),
|
||||
password varchar(50) not null,
|
||||
salt varchar(50)
|
||||
unique(username)
|
||||
salt varchar(50),
|
||||
primary key(username)
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
@ -25,6 +30,7 @@ def __main__():
|
|||
name varchar(50),
|
||||
date bigint,
|
||||
price bigint,
|
||||
buy boolean,
|
||||
memo varchar(300),
|
||||
constraint FK_Account_ID
|
||||
foreign key (uid)
|
||||
|
@ -36,7 +42,14 @@ def __main__():
|
|||
|
||||
conn.commit()
|
||||
|
||||
name = input("input your display name: ")
|
||||
username = input("input your username: ")
|
||||
password = getpass("input your password: ")
|
||||
passchk = getpass("type password one more time: ")
|
||||
|
||||
cur.close()
|
||||
conn.close()
|
||||
f = open("load.txt", "w")
|
||||
f.write("init=true")
|
||||
|
||||
__main__()
|
||||
|
|
1
load.txt
Normal file
1
load.txt
Normal file
|
@ -0,0 +1 @@
|
|||
init=true
|
|
@ -5,4 +5,5 @@ router = APIRouter()
|
|||
|
||||
@router.post("/auth/login")
|
||||
def login(auth: Credential):
|
||||
|
||||
return {"ok": 1, "token": "Basic {}"}
|
||||
|
|
|
@ -19,6 +19,7 @@ def insert(balance: Balance, resp: Response):
|
|||
return {
|
||||
"ok": 1,
|
||||
"name": balance.name,
|
||||
"is_buy": balance.buy,
|
||||
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
|
||||
}
|
||||
|
||||
|
@ -26,10 +27,9 @@ def insert(balance: Balance, resp: Response):
|
|||
def query(id, resp: Response):
|
||||
started = datetime.now().microsecond / 1000
|
||||
service = BalanceService()
|
||||
|
||||
try:
|
||||
data = service.read(int(id))
|
||||
except:
|
||||
|
||||
if data == None:
|
||||
resp.status_code = 204
|
||||
return {"ok": 0, "errno": "id '{}' result is not found".format(id)}
|
||||
|
||||
|
@ -43,7 +43,7 @@ def query(id, resp: Response):
|
|||
@router.patch("/balance/{action}/{id}")
|
||||
def update(action, id, balance: UpdateForm, resp: Response):
|
||||
service = BalanceService()
|
||||
if action != "name" and action != "date" and action != "price" and action != "memo":
|
||||
if action != "name" and action != "date" and action != "price" and action != "buy" and action != "memo":
|
||||
print(action)
|
||||
print(id)
|
||||
resp.status_code = 400
|
||||
|
@ -71,10 +71,12 @@ def update(action, id, balance: UpdateForm, resp: Response):
|
|||
|
||||
ok = service.update(
|
||||
int(id),
|
||||
action, {
|
||||
action,
|
||||
{
|
||||
"name": balance.name,
|
||||
"date": balance.date,
|
||||
"price": balance.price,
|
||||
"buy": balance.buy,
|
||||
"memo": balance.memo
|
||||
}
|
||||
)
|
||||
|
|
|
@ -6,12 +6,14 @@ class Balance(BaseModel):
|
|||
name: str
|
||||
date: int
|
||||
price: int
|
||||
buy: bool = True
|
||||
memo: str = ""
|
||||
|
||||
class UpdateForm(BaseModel):
|
||||
name: str = ""
|
||||
date: int = 0
|
||||
price: int = 0
|
||||
buy: bool = True
|
||||
memo: str = ""
|
||||
|
||||
class BalanceService:
|
||||
|
@ -23,8 +25,8 @@ class BalanceService:
|
|||
cur = self._conn.cursor()
|
||||
try:
|
||||
cur.execute(
|
||||
"insert into balset(name, date, price, memo) values (%s, %s, %s, %s);",
|
||||
(balance.name, balance.date, balance.price, balance.memo)
|
||||
"insert into balset(name, date, price, buy, memo) values (%s, %s, %s, %s, %s);",
|
||||
(balance.name, balance.date, balance.price, balance.buy, balance.memo)
|
||||
)
|
||||
|
||||
self._conn.commit()
|
||||
|
@ -44,7 +46,7 @@ class BalanceService:
|
|||
data = cur.fetchone()
|
||||
|
||||
if data == None:
|
||||
raise RuntimeError("data not found")
|
||||
return None
|
||||
|
||||
cur.close()
|
||||
self._conn.close()
|
||||
|
@ -54,7 +56,8 @@ class BalanceService:
|
|||
"name": data[1],
|
||||
"date": data[2],
|
||||
"price": data[3],
|
||||
"memo": data[4]
|
||||
"buy": data[4],
|
||||
"memo": data[5]
|
||||
}
|
||||
|
||||
def update(self, id: int, act: str, balance: UpdateForm):
|
||||
|
|
Loading…
Reference in a new issue