File size: 3,919 Bytes
ee37dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be46fd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee37dcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import time

from fastapi import  HTTPException,APIRouter
from .pydanticModel import *
from .dao import tableStore

router = APIRouter()

# 定义路由和函数
@router.post("/login")
@router.get("/login")
async def login(login: signIn):
    # ->"user info or throw error"
    # 检查email和password是否匹配
    table_store = tableStore(ots_client=router.ots_client,table_name=router.table_name)
    try:
        login_result = table_store.login(login.email, login.password)
        if login_result['ec']==200:
            return login_result
        else:
            raise HTTPException(status_code=400, detail="login failed in table store")
    except Exception as e:
        print(e)
        raise HTTPException(status_code=400, detail="sign in failed")

@router.get("/signUp")
@router.post("/signUp")
async def sign_up(signUp: signUp):
    # ->"user info or throw error"
    table_store = tableStore(ots_client=router.ots_client,table_name=router.table_name)
    try:
        signUp_result = table_store.userSignUp(signUp.email,signUp.password)
        if signUp_result['ec']==200:
            return signUp_result
        else:
            raise HTTPException(status_code=400, detail="sign up failed in table store")
    except Exception as e:
        raise HTTPException(status_code=400, detail="sign up failed")



@router.get("/accountDelete")
@router.post("/accountDelete")
async def account_delete(accountDelete: accountDelete):
    table_store = tableStore(ots_client=router.ots_client,table_name=router.table_name)
    try:
        delete_result = table_store.userDelete(accountDelete.email,accountDelete.password)
        if delete_result['ec']==200:
            return delete_result
        else:
            raise HTTPException(status_code=400, detail="account delete failed in table store")
    except Exception as e:
        raise HTTPException(status_code=400, detail="account delete failed")





@router.get("/purchase")
@router.post("/purchase")
async def purchase(plan: userPlan):
        #->bool or raise error
        # 提取plan价格和时间
        price = plan.price
        time_to_add = plan.duration_seconds
        email = plan.email
        password = plan.password
        table_store = tableStore(ots_client=router.ots_client, table_name=router.table_name)
        # 读取存储数据,如果存的expiredAt>now,那么从expiredAt加时间
        # 如果存的<now ,那么从now加时间 (总之是取两者较大+time_to_add)

        table_store.getUserInfo(email=email)
        cur_balance  = table_store.balance
        cur_expired_at = table_store.expired_at
        cur_time = int(time.time())
        user_password_stored = table_store.stored_password


        if cur_balance -price <0:
            raise HTTPException(status_code=400, detail="balance not enough")
        elif password!=user_password_stored:
            raise HTTPException(status_code=400, detail="auth not pass in purchase")
        else:
            expired_new = max(cur_expired_at, cur_time) + time_to_add
            balance_new = cur_balance -price
            # 更新余额和时间
            update_balance_result = table_store.updateColumnByPrimaryKey(
                key=router.key,
                key_value=email,
                update_column='balance',
                update_column_value=balance_new
            )
            if not update_balance_result:
                raise HTTPException(status_code=400, detail="updateBalance 结果失败")
            update_expired_result = table_store.updateColumnByPrimaryKey(
                key=router.key,
                key_value=email,
                update_column='expiredAt',
                update_column_value=expired_new
            )
            if not update_expired_result:
                raise HTTPException(status_code=400, detail="update_expired_result 结果失败")
            return True