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
21 changes: 17 additions & 4 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, Path, HTTPException
from sqlmodel import Session, select
from typing import Annotated

from backend.api.models.request_model import CommandRequest
from backend.api.models.response_model import CommandListResponse, CommandSingleResponse
Expand All @@ -23,23 +24,35 @@ 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

item = Command(command_type=payload.command_type, params=payload.params)
db.add(item)
db.commit()
db.refresh(item)
return {"data": item}


@command_router.delete("/{id}", response_model=CommandListResponse)
def delete_command(id: int):
def delete_command(id: Annotated[int, Path(title = "The ID of the item to delete")], 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
query = select(Command).where(Command.id == id)
try:
item = db.exec(query).one()
except:
raise HTTPException(status_code=404, detail="Item not found")
db.delete(item)
db.commit()
return get_commands(db)
20 changes: 20 additions & 0 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from typing import Any
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from datetime import datetime
from backend.utils.logging import logger
import time


class LoggerMiddleware(BaseHTTPMiddleware):
Expand All @@ -18,5 +21,22 @@ async def dispatch(
:return: Response from endpoint
"""
# TODO:(Member) Finish implementing this method
params = dict(request.query_params)
start_time = time.perf_counter()
start_date = datetime.now()
if params:
logger.info(f"params {params} requested at {start_date}")
else:
logger.info(f"no query params requested at {start_date}")

response = await call_next(request)

process_time = time.perf_counter() - start_time
end_date = datetime.now()
logger.info(f"response returned at {end_date}, taking {process_time} seconds")

return response




15 changes: 15 additions & 0 deletions backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ 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:
raise ValueError("params is None while format is not")

if self.format is None:
raise ValueError("format is None while params is not")

params_value = self.params.split(',')
format_value = self.format.split(',')

if len(params_value) != len(format_value):
raise ValueError("length of params does not equal length of format")

return self


Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading