diff --git a/Makefile b/Makefile deleted file mode 100644 index 8b13789..0000000 --- a/Makefile +++ /dev/null @@ -1 +0,0 @@ - diff --git a/generate.py b/generate.py index bf12fc2..051cb69 100644 --- a/generate.py +++ b/generate.py @@ -1,42 +1,55 @@ -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() - cur.execute( - """ - create table account( - name varchar(25), - username varchar(25) primary key, - password varchar(50) not null, - salt varchar(50) - unique(username) - ); - """ - ) + try: + open("./load.txt", "r") + print("server already initialized") + except: + cur.execute( + """ + create table account( + name varchar(25), + username varchar(25), + password varchar(50) not null, + salt varchar(50), + primary key(username) + ); + """ + ) - cur.execute( - """ - create table balset( - id serial primary key, - uid varchar(25) not null, - name varchar(50), - date bigint, - price bigint, - memo varchar(300), - constraint FK_Account_ID - foreign key (uid) - references account(username) - on delete CASCADE - ); - """ - ) + cur.execute( + """ + create table balset( + id serial primary key, + uid varchar(25) not null, + name varchar(50), + date bigint, + price bigint, + buy boolean, + memo varchar(300), + constraint FK_Account_ID + foreign key (uid) + references account(username) + on delete CASCADE + ); + """ + ) - conn.commit() - - cur.close() - conn.close() + 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__() diff --git a/load.txt b/load.txt new file mode 100644 index 0000000..c254184 --- /dev/null +++ b/load.txt @@ -0,0 +1 @@ +init=true \ No newline at end of file diff --git a/routes/auth.py b/routes/auth.py index dca351c..145871e 100644 --- a/routes/auth.py +++ b/routes/auth.py @@ -5,4 +5,5 @@ router = APIRouter() @router.post("/auth/login") def login(auth: Credential): + return {"ok": 1, "token": "Basic {}"} diff --git a/routes/balance.py b/routes/balance.py index 1a9bfce..cd16b49 100644 --- a/routes/balance.py +++ b/routes/balance.py @@ -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: + data = service.read(int(id)) + + 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 } ) diff --git a/service/balance_service.py b/service/balance_service.py index d6d25e1..c67b82f 100644 --- a/service/balance_service.py +++ b/service/balance_service.py @@ -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):