generated from COSC381-2024Winter/myMiniCanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
40 lines (32 loc) · 1.28 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from fastapi import FastAPI
from typing import List
from course import CourseManager, Course
from user import UserManager
from fastapi.security import APIKeyHeader
from pydantic import BaseModel, Field
coursemanager = CourseManager()
usermanager = UserManager()
usermanager.create_a_user("John", "pwd", "studnet")
usermanager.create_a_user("Alice", "pwd", "teacher")
usermanager.create_a_user("Jimmy", "pwd", "admin")
app = FastAPI()
@app.get("/")
def welcome():
return "Welcome to our miniCanvas!"
class CourseCreate(BaseModel):
semester: str
teacher_id_list: List[int] = Field(..., json_schema_extra={'example': [1, 2, 3]})
@app.post("/courses/{coursecode}")
def create_a_course(coursecode: str, course_data: CourseCreate):
teacher_list = usermanager.find_users(course_data.teacher_id_list)
course_id = coursemanager.create_a_course(coursecode, course_data.semester, teacher_list)
return {"course_id": course_id}
@app.put("/courses/{courseid}/students")
def import_students(courseid: int,
student_id_list: List[int]) -> None:
course = coursemanager.find_a_course(courseid)
student_list = usermanager.find_users(student_id_list)
course.import_students(student_list)
print(course.course_id)
print(course.student_list)
return None