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
18 changes: 14 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, status
from sqlmodel import Session, select

from backend.api.models.request_model import CommandRequest
Expand All @@ -23,23 +23,33 @@ 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(command_type = payload.command_type,params = payload.params)
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
item = db.get(Command, id)
if not item:
raise HTTPException(status_code = status.HTTP_404_NOT_FOUND, detail=f"command with id {id} was not found")
db.delete(item)
db.commit()
return get_commands(db)
18 changes: 18 additions & 0 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from typing import Any
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
import time
from datetime import datetime
from backend.utils import logging

logger = logging.logger

class LoggerMiddleware(BaseHTTPMiddleware):
async def dispatch(
Expand All @@ -18,5 +22,19 @@ async def dispatch(
:return: Response from endpoint
"""
# TODO:(Member) Finish implementing this method
start = time.time()
timsp = datetime.now().strftime("%d/%m/%Y, %H:%M:%S")

logger.info(f"""Incoming request:
Time: {timsp}
Raw Path: {request.url.path}
Path Params: {request.path_params}
Query Params: {dict(request.query_params)}
""")
response = await call_next(request)
time_dur = (time.time()-start)*1000

logger.info(f"""Outgoing Response:
Status: {response.status_code},
Duration: {time_dur}""")
return response
19 changes: 18 additions & 1 deletion backend/data/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,24 @@ 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
params = self.params
format = self.format

if not params and not format:
return self
if not params or not format:
raise ValueError(
"params and format must be both none"
)
params_list = params.split(",")
format_list = format.split(",")
if len(params_list) == len(format_list):
return self
else:
raise ValueError(
"params and format must be both none or have the same number of comma separated values"
)



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