mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-21 00:11:21 +09: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 @@
|
||||||
|
|
77
generate.py
77
generate.py
|
@ -1,42 +1,55 @@
|
||||||
import psycopg2
|
import psycopg2, os
|
||||||
|
from getpass import getpass
|
||||||
from util.config import conn_param
|
from util.config import conn_param
|
||||||
|
|
||||||
def __main__():
|
def __main__():
|
||||||
conn = psycopg2.connect(conn_param)
|
conn = psycopg2.connect(conn_param)
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
cur.execute(
|
try:
|
||||||
"""
|
open("./load.txt", "r")
|
||||||
create table account(
|
print("server already initialized")
|
||||||
name varchar(25),
|
except:
|
||||||
username varchar(25) primary key,
|
cur.execute(
|
||||||
password varchar(50) not null,
|
"""
|
||||||
salt varchar(50)
|
create table account(
|
||||||
unique(username)
|
name varchar(25),
|
||||||
);
|
username varchar(25),
|
||||||
"""
|
password varchar(50) not null,
|
||||||
)
|
salt varchar(50),
|
||||||
|
primary key(username)
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"""
|
"""
|
||||||
create table balset(
|
create table balset(
|
||||||
id serial primary key,
|
id serial primary key,
|
||||||
uid varchar(25) not null,
|
uid varchar(25) not null,
|
||||||
name varchar(50),
|
name varchar(50),
|
||||||
date bigint,
|
date bigint,
|
||||||
price bigint,
|
price bigint,
|
||||||
memo varchar(300),
|
buy boolean,
|
||||||
constraint FK_Account_ID
|
memo varchar(300),
|
||||||
foreign key (uid)
|
constraint FK_Account_ID
|
||||||
references account(username)
|
foreign key (uid)
|
||||||
on delete CASCADE
|
references account(username)
|
||||||
);
|
on delete CASCADE
|
||||||
"""
|
);
|
||||||
)
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
cur.close()
|
name = input("input your display name: ")
|
||||||
conn.close()
|
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__()
|
__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")
|
@router.post("/auth/login")
|
||||||
def login(auth: Credential):
|
def login(auth: Credential):
|
||||||
|
|
||||||
return {"ok": 1, "token": "Basic {}"}
|
return {"ok": 1, "token": "Basic {}"}
|
||||||
|
|
|
@ -19,6 +19,7 @@ def insert(balance: Balance, resp: Response):
|
||||||
return {
|
return {
|
||||||
"ok": 1,
|
"ok": 1,
|
||||||
"name": balance.name,
|
"name": balance.name,
|
||||||
|
"is_buy": balance.buy,
|
||||||
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
|
"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):
|
def query(id, resp: Response):
|
||||||
started = datetime.now().microsecond / 1000
|
started = datetime.now().microsecond / 1000
|
||||||
service = BalanceService()
|
service = BalanceService()
|
||||||
|
data = service.read(int(id))
|
||||||
try:
|
|
||||||
data = service.read(int(id))
|
if data == None:
|
||||||
except:
|
|
||||||
resp.status_code = 204
|
resp.status_code = 204
|
||||||
return {"ok": 0, "errno": "id '{}' result is not found".format(id)}
|
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}")
|
@router.patch("/balance/{action}/{id}")
|
||||||
def update(action, id, balance: UpdateForm, resp: Response):
|
def update(action, id, balance: UpdateForm, resp: Response):
|
||||||
service = BalanceService()
|
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(action)
|
||||||
print(id)
|
print(id)
|
||||||
resp.status_code = 400
|
resp.status_code = 400
|
||||||
|
@ -71,10 +71,12 @@ def update(action, id, balance: UpdateForm, resp: Response):
|
||||||
|
|
||||||
ok = service.update(
|
ok = service.update(
|
||||||
int(id),
|
int(id),
|
||||||
action, {
|
action,
|
||||||
|
{
|
||||||
"name": balance.name,
|
"name": balance.name,
|
||||||
"date": balance.date,
|
"date": balance.date,
|
||||||
"price": balance.price,
|
"price": balance.price,
|
||||||
|
"buy": balance.buy,
|
||||||
"memo": balance.memo
|
"memo": balance.memo
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,12 +6,14 @@ class Balance(BaseModel):
|
||||||
name: str
|
name: str
|
||||||
date: int
|
date: int
|
||||||
price: int
|
price: int
|
||||||
|
buy: bool = True
|
||||||
memo: str = ""
|
memo: str = ""
|
||||||
|
|
||||||
class UpdateForm(BaseModel):
|
class UpdateForm(BaseModel):
|
||||||
name: str = ""
|
name: str = ""
|
||||||
date: int = 0
|
date: int = 0
|
||||||
price: int = 0
|
price: int = 0
|
||||||
|
buy: bool = True
|
||||||
memo: str = ""
|
memo: str = ""
|
||||||
|
|
||||||
class BalanceService:
|
class BalanceService:
|
||||||
|
@ -23,8 +25,8 @@ class BalanceService:
|
||||||
cur = self._conn.cursor()
|
cur = self._conn.cursor()
|
||||||
try:
|
try:
|
||||||
cur.execute(
|
cur.execute(
|
||||||
"insert into balset(name, date, price, memo) values (%s, %s, %s, %s);",
|
"insert into balset(name, date, price, buy, memo) values (%s, %s, %s, %s, %s);",
|
||||||
(balance.name, balance.date, balance.price, balance.memo)
|
(balance.name, balance.date, balance.price, balance.buy, balance.memo)
|
||||||
)
|
)
|
||||||
|
|
||||||
self._conn.commit()
|
self._conn.commit()
|
||||||
|
@ -44,7 +46,7 @@ class BalanceService:
|
||||||
data = cur.fetchone()
|
data = cur.fetchone()
|
||||||
|
|
||||||
if data == None:
|
if data == None:
|
||||||
raise RuntimeError("data not found")
|
return None
|
||||||
|
|
||||||
cur.close()
|
cur.close()
|
||||||
self._conn.close()
|
self._conn.close()
|
||||||
|
@ -54,7 +56,8 @@ class BalanceService:
|
||||||
"name": data[1],
|
"name": data[1],
|
||||||
"date": data[2],
|
"date": data[2],
|
||||||
"price": data[3],
|
"price": data[3],
|
||||||
"memo": data[4]
|
"buy": data[4],
|
||||||
|
"memo": data[5]
|
||||||
}
|
}
|
||||||
|
|
||||||
def update(self, id: int, act: str, balance: UpdateForm):
|
def update(self, id: int, act: str, balance: UpdateForm):
|
||||||
|
|
Loading…
Reference in a new issue