diff --git a/backend/api/endpoints/command.py b/backend/api/endpoints/command.py index 583ea5a..3ea70e5 100644 --- a/backend/api/endpoints/command.py +++ b/backend/api/endpoints/command.py @@ -24,23 +24,45 @@ 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 - + try: + posted_command = Command(**payload.dict()) + db.add(posted_command) + db.commit() + db.refresh(posted_command) + return {"data": posted_command} + except Exception as error: + db.rollback() + raise HTTPException( + status_code = 500, + detail = str(error), + ) @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 """ + deleted_command = db.get(Command, id) + if deleted_command is None: + raise HTTPException(status_code=404, detail = "Command not found") + db.delete(deleted_command) + db.commit() + query = select(Command) + items = db.exec(query).all() + return {"data": items} + + + + # TODO:(Member) Implement this endpoint diff --git a/backend/api/middlewares/logger_middleware.py b/backend/api/middlewares/logger_middleware.py index 44daa5d..4bd8738 100644 --- a/backend/api/middlewares/logger_middleware.py +++ b/backend/api/middlewares/logger_middleware.py @@ -1,7 +1,9 @@ from collections.abc import Callable from typing import Any from fastapi import Request, Response +from backend.utils.logging import logger from starlette.middleware.base import BaseHTTPMiddleware +from time import time class LoggerMiddleware(BaseHTTPMiddleware): @@ -18,5 +20,17 @@ async def dispatch( @return Response from endpoint """ # TODO:(Member) Finish implementing this method + logger.info(f"Req headers:{dict(request.headers)}") + logger.info(f"Req: {request.method} {request.url}") + try: + body = await response.json() + logger.info("Req Body: {body}") + except Exception as error: + logger.warning(f"Unable to parse Request Body. Error: {str(error)}") + startTime = time() response = await call_next(request) + executionTime = time() - startTime + logger.info(f"Response status: {response.status_code}") + logger.info(f"Response Headers: {response.headers}") + logger.info(f"Response Time:{executionTime:.2f} seconds") return response diff --git a/backend/data/data_models.py b/backend/data/data_models.py index 68adddb..b9a42f4 100644 --- a/backend/data/data_models.py +++ b/backend/data/data_models.py @@ -32,6 +32,18 @@ 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. """ + params = self.params + format = self.format + if self.format is None and self.params is None: + return self + elif isinstance(params, str) and isinstance(format, str) and params.count(",")==format.count(","): + return self + else: + raise ValueError(f"""Error in Params and Format. Both Params and Format must have no spaces between data with commas separating each unique value or both must be None. + The Current Params and Format are as follows: + Params: {params} + Format: {format} + """) # TODO: (Member) Implement this method return self