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
23 changes: 18 additions & 5 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
14 changes: 13 additions & 1 deletion backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
8 changes: 6 additions & 2 deletions backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading