feat: middle save

This commit is contained in:
Project_IO 2024-09-10 16:36:03 +09:00
parent 8d628e4826
commit 38c22e4512
6 changed files with 62 additions and 43 deletions

View file

@ -1 +0,0 @@

View file

@ -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()
conn.commit()
cur.close()
conn.close()
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
View file

@ -0,0 +1 @@
init=true

View file

@ -5,4 +5,5 @@ router = APIRouter()
@router.post("/auth/login")
def login(auth: Credential):
return {"ok": 1, "token": "Basic {}"}

View file

@ -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()
data = service.read(int(id))
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
}
)

View file

@ -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):