-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Colleen Polka
authored and
Colleen Polka
committed
Oct 9, 2023
1 parent
dd5fb54
commit dd325f6
Showing
3 changed files
with
71 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from fastapi import APIRouter, HTTPException, Depends | ||
from fastapi.security import HTTPBasic, HTTPBasicCredentials | ||
from typing import Annotated | ||
from courses import COURSES | ||
from models import Course | ||
|
||
|
||
admin = APIRouter() | ||
|
||
security = HTTPBasic() | ||
|
||
|
||
def authenticate(credentials: Annotated[HTTPBasicCredentials, Depends(security)]): | ||
if not (credentials.username == "admin") or not (credentials.password == "password"): | ||
raise HTTPException( | ||
status_code=401, | ||
detail="Incorrect username or password", | ||
headers={"WWW-Authenticate": "Basic"}, | ||
) | ||
return credentials.username | ||
|
||
|
||
@admin.get("/users/me") | ||
def read_current_user(username: Annotated[HTTPBasicCredentials, Depends(authenticate)]): | ||
return {"username": username} | ||
|
||
|
||
@admin.post("/add-course/{course_id}") | ||
async def add_course(course_id: int, | ||
course: Course, | ||
username: Annotated[HTTPBasicCredentials, Depends(authenticate)] | ||
) -> Course: | ||
if course_id in COURSES: | ||
raise HTTPException(403, detail="Course already exists") | ||
COURSES[course_id] = course | ||
return COURSES[course_id] | ||
|
||
|
||
@admin.delete("/delete-course/{course_id}") | ||
async def delete_course(course_id: int, | ||
username: Annotated[HTTPBasicCredentials, Depends(authenticate)] | ||
) -> Course: | ||
if course_id in COURSES: | ||
deleted = COURSES.pop(course_id) | ||
return deleted | ||
raise HTTPException(404, detail="Course not found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from pydantic import BaseModel, Field | ||
from typing import Optional | ||
|
||
|
||
class Time(BaseModel): | ||
days: str = Field(description="Course meeting days", pattern="^[MTWRF]+$") | ||
start: int | ||
end: int | ||
|
||
|
||
class Course(BaseModel): | ||
name: str | ||
professor: str | ||
current_enr: Optional[int] = 0 | ||
max_enr: int | ||
time: Time | ||
|
||
|