main.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "http://172.105.205.52",
  11. "http://172.105.205.52:8000",
  12. ]
  13. app.add_middleware(
  14. CORSMiddleware,
  15. allow_origins=origins,
  16. allow_credentials=True,
  17. allow_methods=["*"],
  18. allow_headers=["*"],
  19. )
  20. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
  21. ##########TAG CRUD################TAG CRUD##################TAG CRUD###########TAG CRUD##################
  22. class Tag(BaseModel):
  23. id: int
  24. name: str
  25. @app.get("/tags")
  26. async def get_tags():
  27. course_table = db['course_table']
  28. print(course_table.columns)
  29. statement = 'SELECT id,name FROM tag_table'
  30. tags = []
  31. for row in db.query(statement):
  32. tags.append({'id':row['id'],'name':row['name']})
  33. return tags
  34. @app.post("/tags")
  35. async def create_tags(tag:Tag):
  36. tag_table = db['tag_table']
  37. pk = tag_table.insert({'name':tag.name})
  38. tag.id = pk
  39. return tag
  40. @app.delete("/tags/{tag_id}")
  41. async def delete_tags(tag_id:int):
  42. sqls = 'DELETE FROM tag_table WHERE id = '+str(tag_id)
  43. db.query(sqls)
  44. sqls = 'DELETE FROM course_tag WHERE tag_id = '+str(tag_id)
  45. db.query(sqls)
  46. return 'success'
  47. @app.put("/tags/{tag_id}")
  48. async def update_tags(tag_id,tag:Tag):
  49. sqls = 'UPDATE tag_table\
  50. SET name="'+tag.name+'"\
  51. WHERE id='+tag_id+';'
  52. db.query(sqls)
  53. tag.id=tag_id
  54. return tag
  55. @app.get("/tags/{tag_id}")
  56. async def get_tags(tag_id):
  57. statement = 'SELECT id,name FROM tag_table where id = '+tag_id
  58. for row in db.query(statement):
  59. result = {'id':row['id'],'name':row['name']}
  60. return result
  61. ###########COURSE CRUD###########COURSE CRUD###########COURSE CRUD###########COURSE CRUD###########
  62. class Course(BaseModel):
  63. id: int
  64. title: str
  65. url: str #course website location url
  66. img: str #img url form
  67. teacher_name: str
  68. teacher_img: str #teacher user image url form
  69. price: int
  70. hours: int
  71. units: int
  72. @app.get("/courses")
  73. async def get_courses(tag_string):
  74. courses = []
  75. statement = 'SELECT course_id from course_tag where tag_id IN'+tag_string
  76. course_id_string = '('
  77. for row in db.query(statement):
  78. course_id_string = course_id_string + str(row['course_id'])+","
  79. course_id_string=course_id_string[:-1]
  80. course_id_string = course_id_string+')'
  81. statement = 'SELECT * from course_table where id IN'+course_id_string
  82. for row in db.query(statement):
  83. courses.append({'id':row['id'],'title':row['title'],'url':row['url'],'img':row['img']
  84. ,'teacher_name':['teacher_name'],'teacher_img':row['teacher_img'],'price':row['price']
  85. ,'hours':row['hours'],'units':row['units']})
  86. return courses
  87. @app.post("/courses")
  88. async def create_courses(Tag_IDs: list,course:Course):
  89. course_table = db['course_table']
  90. course_tag_table = db['course_tag']
  91. pkey = course_table.insert({'title':course.title,'url':course.url,'img':course.img
  92. ,'teacher_name':course.teacher_name, 'teacher_img': course.teacher_img,'units':course.units
  93. ,'price':course.price,'hours':course.hours})
  94. sqls = 'INSERT INTO course_tag (course_id, tag_id)VALUES'
  95. for tid in Tag_IDs:
  96. sqls = sqls + '('+str(pkey)+','+str(tid)+'),'
  97. sqls = sqls[:-1]
  98. db.query(sqls)
  99. return course
  100. @app.post("/course_cover_change/{course_id}")
  101. async def course_cover_change(course_id, uploaded_file: UploadFile = File(...)):
  102. file_location = f"course_cover_img/{course_id}"
  103. with open(file_location, "wb+") as file_object:
  104. file_object.write(uploaded_file.file.read())
  105. sqls = 'UPDATE course_table\
  106. SET img="'+file_location+'"\
  107. WHERE id='+course_id+';'
  108. db.query(sqls)
  109. return '{"file_loc":'+file_location+'}'
  110. @app.post("/teacher_img_change/{course_id}")
  111. async def teacher_img_change(course_id, uploaded_file: UploadFile = File(...)):
  112. file_location = f"teacher_img/{course_id}"
  113. with open(file_location, "wb+") as file_object:
  114. file_object.write(uploaded_file.file.read())
  115. sqls = 'UPDATE course_table\
  116. SET teacher_img="'+file_location+'"\
  117. WHERE id='+course_id+';'
  118. db.query(sqls)
  119. return '{"file_loc":'+file_location+'}'
  120. @app.delete("/courses/{course_id}")
  121. async def delete_course(course_id:int):
  122. sqls = 'DELETE FROM course_table WHERE id = '+str(course_id)
  123. db.query(sqls)
  124. sqls = 'DELETE FROM course_tag WHERE course_id = '+str(course_id)
  125. db.query(sqls)
  126. return "success"
  127. @app.put("/courses/{course_id}")
  128. async def update_course(course_id,course:Course):
  129. sqls = 'UPDATE course_table\
  130. SET title="'+course.title+'", url="'+course.url+'", hours='+str(course.hours)+',\
  131. teacher_name="'+course.teacher_name+'", price='+str(course.price)+', units='+str(course.units)+'\
  132. WHERE id='+str(course_id)+';'
  133. db.query(sqls)
  134. course.id=course_id
  135. return course