main.py 3.3 KB

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