diff --git a/backend/api/endpoints/command.py b/backend/api/endpoints/command.py index 583ea5a..e744ee0 100644 --- a/backend/api/endpoints/command.py +++ b/backend/api/endpoints/command.py @@ -24,23 +24,36 @@ def get_commands(db: Session = Depends(get_db)): @command_router.post("/", response_model=CommandSingleResponse) -def create_command(payload: CommandRequest): +def create_command(payload: CommandRequest, db: Session = Depends(get_db)): """ Creates an item with the given payload in the database and returns this payload after pulling it from the database @param payload: The data used to create an item @return returns a json object with field of "data" under which there is the payload now pulled from the database """ - # TODO:(Member) Implement this endpoint - + new_command = Command(**payload.model_dump()) + db.add(new_command) + db.commit() + db.refresh(new_command) + return {"data": new_command} @command_router.delete("/{id}", response_model=CommandListResponse) -def delete_command(id: int): +def delete_command(id: int, db: Session = Depends(get_db)): """ Deletes the item with the given id if it exists. Otherwise raises a 404 error. @param id: The id of the item to delete @return returns the list of commands after deleting the item """ - # TODO:(Member) Implement this endpoint + command = db.get(Command, id) + + if command is None: + raise HTTPException(status_code=404, detail="Command not found") + + db.delete(command) + db.commit() + + query = select(Command) + items = db.exec(query).all() + return {"data": items} \ No newline at end of file diff --git a/backend/api/middlewares/logger_middleware.py b/backend/api/middlewares/logger_middleware.py index 44daa5d..6ebf1f2 100644 --- a/backend/api/middlewares/logger_middleware.py +++ b/backend/api/middlewares/logger_middleware.py @@ -1,7 +1,9 @@ +from time import perf_counter from collections.abc import Callable from typing import Any from fastapi import Request, Response from starlette.middleware.base import BaseHTTPMiddleware +from backend.utils.logging import logger class LoggerMiddleware(BaseHTTPMiddleware): @@ -17,6 +19,16 @@ async def dispatch( @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) @return Response from endpoint """ - # TODO:(Member) Finish implementing this method + logger.info(f"Request: {request.method} {request.url}") + + start_time = perf_counter() + logger.info(f"Start Time: {start_time}") + response = await call_next(request) + process_time = perf_counter() - start_time + + logger.info(f"Status: {response.status_code}") + logger.info(f"Headers: {response.headers}") + logger.info(f"Process Time: {process_time}") + return response diff --git a/backend/data/data_models.py b/backend/data/data_models.py index 68adddb..a8835a6 100644 --- a/backend/data/data_models.py +++ b/backend/data/data_models.py @@ -32,8 +32,12 @@ def validate_params_format(self): In either of these cases return self. Otherwise raise a ValueError. The format of the comma seperated values is "data1,data2" so no spaces between data and the commas. """ - # TODO: (Member) Implement this method - return self + if self.params is None and self.format is None: + return self + elif self.params is not None and self.format is not None and len(self.params.split(",")) == len(self.format.split(",")): + return self + else: + raise ValueError(f"Error: Params and format are not both None or have different numbers of comma seperated values.\n") class Command(BaseSQLModel, table=True):