File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ from fastapi import FastAPI
2+ from pydantic import BaseModel
3+ from typing import Optional
4+
5+ app = FastAPI ()
6+
7+ # temp database
8+ fakedb = []
9+
10+ # course model to store courses
11+ class Course (BaseModel ):
12+ id : int
13+ name : str
14+ price : float
15+ is_early_bird : Optional [bool ] = None
16+
17+ # Home/welcome route
18+ @app .get ("/" )
19+ def read_root ():
20+ return {"greetings" : "Welcome to LearnCodeOnline.in" }
21+
22+ # Get all courses
23+ @app .get ("/courses" )
24+ def get_courses ():
25+ return fakedb
26+
27+ # get single course
28+ @app .get ("/courses/{course_id}" )
29+ def get_a_course (course_id : int ):
30+ course = course_id - 1
31+ return fakedb [course ]
32+
33+ # add a new course
34+ @app .post ("/courses" )
35+ def add_course (course : Course ):
36+ fakedb .append (course .dict ())
37+ return fakedb [- 1 ]
38+
39+ # delete a course
40+ @app .delete ("/courses/{course_id}" )
41+ def delete_course (course_id : int ):
42+ fakedb .pop (course_id - 1 )
43+ return {"task" : "deletion successful" }
You can’t perform that action at this time.
0 commit comments