|
@@ -1,160 +0,0 @@
|
|
|
-
|
|
|
-from enum import Enum
|
|
|
-from typing import Optional
|
|
|
-from pydantic import BaseModel
|
|
|
-from fastapi import FastAPI, Query, UploadFile, File
|
|
|
-import dataset,json
|
|
|
-from fastapi import FastAPI
|
|
|
-from fastapi.middleware.cors import CORSMiddleware
|
|
|
-from fastapi.staticfiles import StaticFiles
|
|
|
-import time
|
|
|
-from datetime import datetime
|
|
|
-from PIL import Image
|
|
|
-import io
|
|
|
-
|
|
|
-app = FastAPI()
|
|
|
-origins = [
|
|
|
- "http://172.105.205.52",
|
|
|
- "http://172.105.205.52:8000",
|
|
|
-]
|
|
|
-
|
|
|
-app.add_middleware(
|
|
|
- CORSMiddleware,
|
|
|
- allow_origins=origins,
|
|
|
- allow_credentials=True,
|
|
|
- allow_methods=["*"],
|
|
|
- allow_headers=["*"],
|
|
|
-)
|
|
|
-
|
|
|
-serverADDR = '127.0.0.1:8000/'
|
|
|
-cover_img_dir = 'static/cover_img/'
|
|
|
-app.mount("/static/cover_img", StaticFiles(directory="static/cover_img"), name="static/cover_img")
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-##########TAG CRUD################TAG CRUD##################TAG CRUD###########TAG CRUD##################
|
|
|
-class Tag(BaseModel):
|
|
|
- id: int
|
|
|
- name: str
|
|
|
-
|
|
|
-@app.get("/tags")
|
|
|
-async def get_tags():
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- statement = 'SELECT id,name FROM tag'
|
|
|
- tags = []
|
|
|
- for row in db.query(statement):
|
|
|
- tags.append({'id':row['id'],'name':row['name']})
|
|
|
- return tags
|
|
|
-
|
|
|
-@app.post("/tags")
|
|
|
-async def create_tags(tag:Tag):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- tag_table = db['tag_table']
|
|
|
- pk = tag_table.insert({'name':tag.name})
|
|
|
- tag.id = pk
|
|
|
- return tag
|
|
|
-
|
|
|
-@app.delete("/tags/{tag_id}")
|
|
|
-async def delete_tags(tag_id:int):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- sqls = 'DELETE FROM tag_table WHERE id = '+str(tag_id)
|
|
|
- db.query(sqls)
|
|
|
- sqls = 'DELETE FROM course_tag WHERE tag_id = '+str(tag_id)
|
|
|
- db.query(sqls)
|
|
|
- return 'success'
|
|
|
-
|
|
|
-@app.put("/tags/{tag_id}")
|
|
|
-async def update_tags(tag_id,tag:Tag):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- sqls = 'UPDATE tag_table\
|
|
|
- SET name="'+tag.name+'"\
|
|
|
- WHERE id='+tag_id+';'
|
|
|
- db.query(sqls)
|
|
|
- tag.id=tag_id
|
|
|
- return tag
|
|
|
-
|
|
|
-@app.get("/tags/{tag_id}")
|
|
|
-async def get_tags(tag_id):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- statement = 'SELECT id,name FROM tag_table where id = '+tag_id
|
|
|
- for row in db.query(statement):
|
|
|
- result = {'id':row['id'],'name':row['name']}
|
|
|
- return result
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-###########COURSE CRUD###########COURSE CRUD###########COURSE CRUD###########COURSE CRUD###########
|
|
|
-class Course(BaseModel):
|
|
|
- id: int
|
|
|
- title: str
|
|
|
- profile: str
|
|
|
- url: str #course website location url
|
|
|
- cover_img: str #img url form
|
|
|
- teacher_name: str
|
|
|
- teacher_img: str #teacher user image url form
|
|
|
- price: int
|
|
|
- price_discount: int
|
|
|
- hours: int
|
|
|
- units: int
|
|
|
-'''
|
|
|
-@app.get("/courses")
|
|
|
-async def get_courses(tags:List[int]):
|
|
|
-
|
|
|
- courses = []
|
|
|
-
|
|
|
-
|
|
|
- statement = 'SELECT * from course_table where id IN'+course_id_string
|
|
|
- for row in db.query(statement):
|
|
|
- courses.append({'id':row['id'],'title':row['title'],'url':row['url'],'img':row['img']
|
|
|
- ,'teacher_name':['teacher_name'],'teacher_img':row['teacher_img'],'price':row['price']
|
|
|
- ,'hours':row['hours'],'units':row['units']})
|
|
|
- return courses
|
|
|
-'''
|
|
|
-@app.post("/courses")
|
|
|
-async def create_courses(tags: list,course:Course):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- #add to course table
|
|
|
- course_table = db['course']
|
|
|
- pk = course_table.insert({'title':course.title,'profile':course.profile,'url':course.url,'cover_img':course.cover_img,'teacher_name':course.teacher_name,
|
|
|
- 'price':course.price,'price_discount':course.price_discount,'hours':course.hours,'units':course.units})
|
|
|
- course.id = pk
|
|
|
- course_tag_table = db['course_tag']
|
|
|
- for tag in tags:
|
|
|
- course_tag_table.insert({'course_id':course.id,'tag_id':tag})
|
|
|
- return {'msg':'新增成功'}
|
|
|
-
|
|
|
-
|
|
|
-@app.delete("/courses/{course_id}")
|
|
|
-async def delete_course(course_id:int):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- sqls = 'DELETE FROM course_table WHERE id = '+str(course_id)
|
|
|
- db.query(sqls)
|
|
|
- sqls = 'DELETE FROM course_tag WHERE course_id = '+str(course_id)
|
|
|
- db.query(sqls)
|
|
|
- return "success"
|
|
|
-
|
|
|
-@app.put("/courses/{course_id}")
|
|
|
-async def update_course(course_id,course:Course):
|
|
|
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/openTalk?charset=utf8mb4')
|
|
|
- sqls = 'UPDATE course_table\
|
|
|
- SET title="'+course.title+'", url="'+course.url+'", hours='+str(course.hours)+',\
|
|
|
- teacher_name="'+course.teacher_name+'", price='+str(course.price)+', units='+str(course.units)+'\
|
|
|
- WHERE id='+str(course_id)+';'
|
|
|
- db.query(sqls)
|
|
|
- course.id=course_id
|
|
|
- return course
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-@app.post("/upload_cover_img/")
|
|
|
-async def create_upload_file(file: UploadFile = File(...)):
|
|
|
- img_name = str(time.time()).replace('.','')
|
|
|
-
|
|
|
- contents = await file.read()
|
|
|
- image = Image.open(io.BytesIO(contents))
|
|
|
- image= image.convert("RGB")
|
|
|
- image.save(cover_img_dir+img_name+'.jpg')
|
|
|
-
|
|
|
- return {"msg": serverADDR+cover_img_dir+img_name+'.jpg'}
|
|
|
-
|
|
|
-
|