Skip to content

Commit 6b9d976

Browse files
committed
implemented backend tasks
1 parent bcd74d4 commit 6b9d976

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

backend/api/endpoints/command.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,40 @@ def get_commands(db: Session = Depends(get_db)):
2424

2525

2626
@command_router.post("/", response_model=CommandSingleResponse)
27-
def create_command(payload: CommandRequest):
27+
def create_command(payload: CommandRequest, db: Session = Depends(get_db)):
2828
"""
2929
Creates an item with the given payload in the database and returns this payload after pulling it from the database
3030
3131
@param payload: The data used to create an item
3232
@return returns a json object with field of "data" under which there is the payload now pulled from the database
3333
"""
34-
# TODO:(Member) Implement this endpoint
35-
34+
35+
new_command = Command(**payload.dict());
36+
37+
db.add(new_command)
38+
db.commit()
39+
db.refresh(new_command)
40+
41+
return {"data" : new_command}
3642

3743

3844
@command_router.delete("/{id}", response_model=CommandListResponse)
39-
def delete_command(id: int):
45+
def delete_command(id: int, db: Session = Depends(get_db)):
4046
"""
4147
Deletes the item with the given id if it exists. Otherwise raises a 404 error.
4248
4349
@param id: The id of the item to delete
4450
@return returns the list of commands after deleting the item
4551
"""
46-
# TODO:(Member) Implement this endpoint
52+
53+
command_to_delete = db.get(Command, id)
54+
if not command_to_delete:
55+
raise HTTPException(status_code=404)
56+
57+
db.delete(command_to_delete)
58+
db.commit()
59+
60+
remaining_commands = db.exec(select(Command)).all()
61+
62+
return {"data": remaining_commands}
63+

backend/api/middlewares/logger_middleware.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from collections.abc import Callable
2+
from time import time
23
from typing import Any
34
from fastapi import Request, Response
5+
from loguru import logger
46
from starlette.middleware.base import BaseHTTPMiddleware
57

8+
from backend.utils.logging import logger_setup, logger_close
9+
610

711
class LoggerMiddleware(BaseHTTPMiddleware):
812
async def dispatch(
@@ -17,6 +21,28 @@ async def dispatch(
1721
@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)
1822
@return Response from endpoint
1923
"""
20-
# TODO:(Member) Finish implementing this method
21-
response = await call_next(request)
22-
return response
24+
25+
#logger_setup() assuming this is run during startup
26+
27+
logger.info(f"Incoming request: {request.method} {request.url.path}");
28+
logger.info(f"Params: {request.query_params}");
29+
logger.info(f"Headers: {dict(request.headers)}");
30+
31+
try:
32+
start_time = time()
33+
response = await call_next(request)
34+
end_time = time() - start_time
35+
36+
logger.info(f"Outgoing response: {request.method} {request.url.path}")
37+
logger.info(f"Response status: {response.status_code}")
38+
logger.info(f"Response headers: {dict(response.headers)}")
39+
logger.info(f"Response time: {end_time}")
40+
41+
#await logger_close()
42+
return response
43+
44+
except Exception as e:
45+
#await logger_close()
46+
raise
47+
48+

backend/data/data_models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ 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-
return self
36+
if not self.params and not self.format:
37+
return self
38+
elif self.params and self.format and len(self.params.split(",")) == len(self.format.split(",")):
39+
return self
40+
else:
41+
raise ValueError
3742

3843

3944
class Command(BaseSQLModel, table=True):

0 commit comments

Comments
 (0)