main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from enum import Enum
  2. import time
  3. from datetime import datetime
  4. from typing import Optional
  5. from pydantic import BaseModel
  6. from fastapi import FastAPI, Query, UploadFile, File
  7. import dataset,json
  8. from fastapi import FastAPI
  9. from fastapi.middleware.cors import CORSMiddleware
  10. app = FastAPI()
  11. origins = [
  12. "https://hhh.com.tw"
  13. "http://172.105.205.52",
  14. "http://172.105.205.52:8001",
  15. "http://172.104.93.163",
  16. ]
  17. app.add_middleware(
  18. CORSMiddleware,
  19. # allow_origins=origins,
  20. allow_origins=["*"],
  21. allow_credentials=True,
  22. allow_methods=["*"],
  23. allow_headers=["*"],
  24. )
  25. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  26. class deco_request(BaseModel):
  27. id: int
  28. name: str
  29. email: str
  30. phone: str
  31. class deco_request_detail(BaseModel):
  32. id: int
  33. name: str
  34. email: str
  35. phone: str
  36. #detail
  37. loc: str
  38. h_class: str
  39. type: str
  40. budget: str
  41. size: float
  42. bed_num: int
  43. liv_num: int
  44. bath_num: int
  45. style: str
  46. prefer_date: str
  47. time_stamp: str
  48. gender: str
  49. @app.get("/deco_request")
  50. async def get_req():
  51. request_table = db['deco_request']
  52. statement = 'SELECT * FROM deco_request'
  53. reqs = []
  54. for req in db.query(statement):
  55. reqs.append({'name':req['name'],'email':req['email'],'phone':req['phone'],'loc':req['loc']
  56. ,'h_class':req['h_class'],'type':req['type'],'budget':req['budget'],'size':req['size'],'bed_num':req['bed_num']
  57. ,'liv_num':req['liv_num'],'bath_num':req['bath_num'],'style':req['style'],'prefer_date':req['prefer_date'],'gender':req.gender})
  58. return reqs
  59. @app.post("/deco_request")
  60. async def create_req(req:deco_request):
  61. request_table = db['deco_request']
  62. time_stamp = datetime.fromtimestamp(time.time())
  63. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  64. pk = request_table.insert({'name':req.name,'email':req.email,'phone':req.phone,'time_stamp':time_stamp})
  65. req.id = pk
  66. return req
  67. @app.post("/deco_request_detail")
  68. async def create_req_detail(req:deco_request_detail):
  69. statement = 'SELECT * FROM deco_request WHERE email="'+req.email+'"'
  70. email_cnt = 0
  71. for row in db.query(statement):
  72. email_cnt = email_cnt + 1
  73. statement = 'SELECT * FROM deco_request WHERE phone="'+req.phone+'"'
  74. phone_cnt = 0
  75. for row in db.query(statement):
  76. phone_cnt = phone_cnt + 1
  77. return_code = 0
  78. #return code 0=good, 1=email duplication, 2=phone duplication, 3= both duplicate
  79. if email_cnt>0 and phone_cnt==0:
  80. return_code = 1
  81. if email_cnt==0 and phone_cnt>0:
  82. return_code = 2
  83. if email_cnt>0 and phone_cnt>0:
  84. return_code = 3
  85. if return_code ==0 :
  86. request_table = db['deco_request']
  87. time_stamp = datetime.fromtimestamp(time.time())
  88. time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
  89. req.time_stamp = time_stamp
  90. pk = request_table.insert({'name':req.name,'email':req.email,'phone':req.phone,'loc':req.loc
  91. ,'h_class':req.h_class,'type':req.type,'budget':req.budget,'size':req.size,'bed_num':req.bed_num
  92. ,'liv_num':req.liv_num,'bath_num':req.bath_num,'style':req.style,'prefer_date':req.prefer_date,'time_stamp':req.time_stamp,'gender':req.gender})
  93. req.id = pk
  94. return return_code
  95. else :
  96. return return_code