Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
12 changes: 12 additions & 0 deletions backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading