Skip to content

Commit 16b6a13

Browse files
data model, add delete commands
1 parent e2ae599 commit 16b6a13

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

backend/api/endpoints/command.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from fastapi import HTTPException
12
from fastapi import APIRouter, Depends
23
from sqlmodel import Session, select
34

@@ -15,31 +16,43 @@ def get_commands(db: Session = Depends(get_db)):
1516
"""
1617
Gets all the items
1718
18-
:return: Returns a list of commands
19+
:return: Returns a list of T
1920
"""
2021
query = select(Command)
2122
items = db.exec(query).all()
2223
return {"data": items}
2324

2425

2526
@command_router.post("/", response_model=CommandSingleResponse)
26-
def create_command(payload: CommandRequest):
27+
def create_command(payload: CommandRequest, db: Session = Depends(get_db)):
2728
"""
28-
Creates an item with the given payload in the database and returns this payload after pulling it from the database
29+
Creates an item with the given payload in the database and returns this
30+
payload after pulling it from the database
2931
3032
:param payload: The data used to create an item
3133
:return: returns a json object with field of "data" under which there is the payload now pulled from the database
3234
"""
33-
# TODO:(Member) Implement this endpoint
34-
35-
35+
# TODO:(Member) Implement this endpoint'
36+
payload = payload.model_dump()
37+
command = Command(**payload)
38+
db.add(command)
39+
db.commit()
40+
db.refresh(command)
41+
return {"data": command}
3642

3743
@command_router.delete("/{id}", response_model=CommandListResponse)
38-
def delete_command(id: int):
44+
def delete_command(id: int, db: Session = Depends(get_db)):
3945
"""
4046
Deletes the item with the given id if it exists. Otherwise raises a 404 error.
4147
4248
:param id: The id of the item to delete
4349
:return: returns the list of commands after deleting the item
4450
"""
4551
# TODO:(Member) Implement this endpoint
52+
command = db.get(entity=Command, ident=id)
53+
if command:
54+
db.delete(command)
55+
db.commit()
56+
return get_commands(db)
57+
else:
58+
raise HTTPException(status_code=404)

backend/api/middlewares/logger_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def dispatch(
1414
Logs should be printed so that they are easily readable and understandable.
1515
1616
:param request: Request received to this middleware from client (it is supplied by FastAPI)
17-
:param call_next: Endpoint or next middleware to be called (if any, this is the next middleware in the chain of middlewares, it is supplied by FastAPI)
17+
:param call_next: Endpoint or next middlew are to be called (if any, this is the next middleware in the chain of middlewares, it is supplied by FastAPI)
1818
:return: Response from endpoint
1919
"""
2020
# TODO:(Member) Finish implementing this method

backend/data/data_models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ def validate_params_format(self):
3333
The format of the comma seperated values is "data1,data2" so no spaces between data and the commas.
3434
"""
3535
# TODO: (Member) Implement this method
36+
# Done!
37+
if self.params is None and self.format is None:
38+
return self
39+
elif self.params and self.format and len(list(self.params.split(","))) == len(list(self.params.split(","))):
40+
return self
41+
else:
42+
raise ValueError()
3643
return self
3744

3845

0 commit comments

Comments
 (0)