Skip to content
Open
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: 20 additions & 3 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,40 @@ 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


command = Command(**payload.model_dump())
db.add(command)
db.commit()
db.refresh(command)
return {"data": 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 not command:
raise HTTPException(status_code=404)

db.delete(command)
db.commit()

sqlQuery = select(Command)
entries = db.exec(sqlQuery).all()
return {"data": entries}
22 changes: 22 additions & 0 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Any
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from time import time
from backend.utils.logging import logger


class LoggerMiddleware(BaseHTTPMiddleware):
Expand All @@ -18,5 +20,25 @@ async def dispatch(
@return Response from endpoint
"""
# TODO:(Member) Finish implementing this method

start_time = time()

logger.info(f"Incoming request: {request.method} {request.url.path}")
logger.info(f"Request time: {start_time}")


response = await call_next(request)

end_time = time()

logger.info(f"Outgoing request: {request.method} {request.url.path}")
logger.info(f"Response time: {end_time}")

logger.info(
f"Completed response: {request.method} {request.url.path} - Status: {response.status_code} - Duration: {end_time - start_time:.4f} seconds"
)

return response



13 changes: 13 additions & 0 deletions backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def validate_params_format(self):
The format of the comma seperated values is "data1,data2" so no spaces between data and the commas.
"""
# TODO: (Member) Implement this method

if self.params is None and self.format is None:
return self

if self.params is None or self.format is None:
raise ValueError()

params_list = self.params.split(",")
format_list = self.format.split(",")

if len(params_list) != len(format_list):
raise ValueError()

return self


Expand Down
Loading