Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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: 19 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,38 @@ 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

commands = Command(command_type = payload.command_type, params = payload.params)
db.add(commands)
db.commit()
db.refresh(commands)

return {"data": commands}


@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.exec(select(Command).where(Command.id == id)).one_or_none()
if command is None:
raise HTTPException(status_code=404, detail="Error")

Choose a reason for hiding this comment

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

A more detailed error message would be nice.


db.delete(command)
db.commit()

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


class LoggerMiddleware(BaseHTTPMiddleware):
Expand All @@ -18,5 +22,17 @@ async def dispatch(
:return: Response from endpoint
"""
# TODO:(Member) Finish implementing this method
response = await call_next(request)
return response
start = time.perf_counter()
request_time = to_unix_time(datetime.datetime.now())

response: Response = await call_next(request)
duration = time.perf_counter() - start

Choose a reason for hiding this comment

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

Minor thing, any way to convert this into ms?


log_data = {
"request params": dict(request.query_params),
"datetime of request": request_time,
"execution duration": duration,
}

logger.info("Log data {}", log_data)
return response
7 changes: 6 additions & 1 deletion backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ 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
return self
if self.params == None and self.format == None:
return self
if (self.params != None and self.format != None) and len(self.params.split(",")) == len(self.format.split(",")):
return self
else:
raise ValueError("Value error")

Choose a reason for hiding this comment

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

Could you add a more descriptive error message?



class Command(BaseSQLModel, table=True):
Expand Down