1- from fastapi import FastAPI # import FastApi class
2- from fastapi import HTTPException # import HHTPExeption class
3- from storage import TaskStorage # import TaskStorage class
4- app = FastAPI () # create copy of FastAPi class
5- storage = TaskStorage () # create copy of TaskStorage class
1+ from fastapi import FastAPI
2+ from fastapi import HTTPException
3+ from storage_gist import GistStorage
4+ app = FastAPI ()
5+ storage = GistStorage ()
66# we display a list of all received tasks.
7- @app .get ("/tasks" ) # use standart FastApi get decorator
8- def get_tasks (): # get task method
9- return storage .load () # return task lsit
7+ @app .get ("/tasks" )
8+ def get_tasks ():
9+ return storage .load ()
1010# We add a new task, add it to the task list, and increment the id counter.
11- @app .post ("/tasks" ) # use standart FastAPI post decorator
12- def create_task (name : str , condition : str = 'new' ): # create new task method
13- tasks = storage .load () # task list now in tasks
14- new_id = max ((task ['id' ] for task in tasks ), default = 0 ) + 1 # crate new id fo task
15- task = {'id' : new_id , 'name' : name , 'condition' : condition } # task dict
16- tasks .append (task ) # append task on list
17- storage .save (tasks ) # save task
18- return task # return new task
11+ @app .post ("/tasks" )
12+ def create_task (name : str , condition : str = 'new' ):
13+ tasks = storage .load ()
14+ new_id = max ((task ['id' ] for task in tasks ), default = 0 ) + 1
15+ task = {'id' : new_id , 'name' : name , 'condition' : condition }
16+ tasks .append (task )
17+ storage .save (tasks )
18+ return task
1919# We update the task; if the task is missing, we raise an error.
20- @app .put ("/tasks/{task_id}" ) # use standart FastAPI put decorator
21- def update_task (task_id : int , name : str , condition : str ): # update task meyhod
22- tasks = storage .load () # loads all tasks list
23- for task in tasks : # finding task cicle
24- if task ['id' ] == task_id : # check task id in list
25- task ['name' ] = name # update name if id in list
26- task ['condition' ] = condition # update status if id in list
27- storage .save (tasks ) # save task in list
28- return task # return updated task
29- raise HTTPException (status_code = 404 , detail = 'task not found' ) # raise if id not in list
20+ @app .put ("/tasks/{task_id}" )
21+ def update_task (task_id : int , name : str , condition : str ):
22+ tasks = storage .load ()
23+ for task in tasks :
24+ if task ['id' ] == task_id :
25+ task ['name' ] = name
26+ task ['condition' ] = condition
27+ storage .save (tasks )
28+ return task
29+ raise HTTPException (status_code = 404 , detail = 'task not found' )
3030# We search for a task by ID, if we find it, we delete it, if not, we raise an error
31- @app .delete ("/tasks/{task_id}" ) # use standart FastApi delete decorator
32- def delete_task (task_id : int ): # delete task method
33- tasks = storage .load () # loads all tasks list
34- for task in tasks : # finding task cicle
35- if task ['id' ] == task_id : # check task id in list
36- tasks .remove (task ) # remove task if id in list
37- storage .save (tasks ) # save tasks list
38- return 'task deleted' # return delete message
39- raise HTTPException (status_code = 404 , detail = 'task not found' ) # raise if id not in list
31+ @app .delete ("/tasks/{task_id}" )
32+ def delete_task (task_id : int ):
33+ tasks = storage .load ()
34+ for task in tasks :
35+ if task ['id' ] == task_id :
36+ tasks .remove (task )
37+ storage .save (tasks )
38+ return 'task deleted'
39+ raise HTTPException (status_code = 404 , detail = 'task not found' )
0 commit comments