Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
29 changes: 25 additions & 4 deletions backend/api/endpoints/command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends
from fastapi import APIRouter, Depends, HTTPException
from sqlmodel import Session, select

from backend.api.models.request_model import CommandRequest
Expand All @@ -23,23 +23,44 @@ 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

command1 = Command(command_type=payload.command_type, params=payload.params)

db.add(command1)
db.commit()
db.refresh(command1)

query = select(Command).where(Command.id == command1.id)
item = db.exec(query).one()
return {"data" : item}


@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
# with get_db() as db:
query = select(Command).where(Command.id == id)
item = db.exec(query).first()

if(item is None):
raise HTTPException(status_code=404)

db.delete(item)
db.commit()

items = db.exec(select(Command)).all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be repeated functionality! You can use the get_commands() function here instead!


return {"data": items}
9 changes: 8 additions & 1 deletion backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from typing import Any
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware

from backend.utils.logging import logger
from datetime import datetime

class LoggerMiddleware(BaseHTTPMiddleware):
async def dispatch(
Expand All @@ -18,5 +19,11 @@ async def dispatch(
:return: Response from endpoint
"""
# TODO:(Member) Finish implementing this method
req_dt = datetime.now()
params = request.query_params

response = await call_next(request)
resp_dt = datetime.now()

logger.info(f"Params: {params} requested at {req_dt}, lasting {(resp_dt - req_dt).total_seconds() * 1000}ms")
return response
6 changes: 5 additions & 1 deletion backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ 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
if(self.params is None and self.format is None):
return self
if(self.params is None or self.format is None
or self.params.count(",") != self.format.count(",")):
raise ValueError()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this value error have a description (i.e. ValueError("Description here"))

return self


Expand Down