123456789101112131415161718192021222324252627282930313233343536373839 |
- from enum import Enum
- from typing import Optional
- from fastapi import FastAPI, Query
- import dataset,json
- app = FastAPI()
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/cmm_test?charset=utf8mb4')
- @app.get("/tags")
- async def get_tags():
- tag_dict = {}
- table = db.load_table('tag_table')
- statement = 'SELECT id,name FROM tag_table'
- for row in db.query(statement):
- tag_dict[row['id']]=row['name']
- json_dump = json.dumps(tag_dict, ensure_ascii=False)
-
- return json_dump
- @app.get("/courses")
- async def get_courses(tag_string):
- course_dict = {}
- statement = 'SELECT course_id from course_tag where tag_id IN'+tag_string
- course_id_string = '('
- for row in db.query(statement):
- course_id_string = course_id_string + str(row['course_id'])+","
- course_id_string=course_id_string[:-1]
- course_id_string = course_id_string+')'
- statement = 'SELECT id,name from course_table where id IN'+course_id_string
- for row in db.query(statement):
- course_dict[row['id']]=row['name']
- json_dump = json.dumps(course_dict, ensure_ascii=False)
- return json_dump
|