main.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from enum import Enum
  2. from typing import Optional
  3. from pydantic import BaseModel
  4. from fastapi import FastAPI, Query, UploadFile, File
  5. import dataset,json
  6. from fastapi import FastAPI
  7. from fastapi.middleware.cors import CORSMiddleware
  8. app = FastAPI()
  9. origins = [
  10. "https://hhh.com.tw"
  11. "http://172.105.205.52",
  12. "http://172.105.205.52:800020",
  13. ]
  14. app.add_middleware(
  15. CORSMiddleware,
  16. allow_origins=origins,
  17. allow_credentials=True,
  18. allow_methods=["*"],
  19. allow_headers=["*"],
  20. )
  21. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  22. class deco_request(BaseModel):
  23. id: int
  24. name: str
  25. email: str
  26. phone: str
  27. #detail
  28. loc: str
  29. h_class: str
  30. type: str
  31. budget: str
  32. size: float
  33. bed_num: int
  34. liv_num: int
  35. bath_num: int
  36. style: str
  37. prefer_date: str
  38. @app.get("/deco_request")
  39. async def get_tags():
  40. request_table = db['deco_request']
  41. statement = 'SELECT * FROM deco_request'
  42. reqs = []
  43. for req in db.query(statement):
  44. reqs.append({'name':req.name,'email':req.email,'phone':req.phone,'loc':req.loc
  45. ,'h_class':req.h_class,'type':req.type,'budget':req.budget,'size':req.size,'bed_num':req.bed_num
  46. ,'liv_num':req.liv_num,'bath_num':req.bath_num,'style':req.style,'prefer_date':req.prefer_date})
  47. return reqs
  48. @app.post("/deco_request")
  49. async def create_tags(req:deco_request):
  50. request_table = db['deco_request']
  51. pk = request_table.insert({'name':req.name,'email':req.email,'phone':req.phone,'loc':req.loc
  52. ,'h_class':req.h_class,'type':req.type,'budget':req.budget,'size':req.size,'bed_num':req.bed_num
  53. ,'liv_num':req.liv_num,'bath_num':req.bath_num,'style':req.style,'prefer_date':req.prefer_date})
  54. req.id = pk
  55. return req