Skip to content
Closed
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
32 changes: 28 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,47 @@ 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

# Create Cmd
new_cmd = Command(**payload.model_dump())

# Add into database
db.add(new_cmd)
db.commit()
db.refresh(new_cmd)

return {"data": new_cmd}


@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
query = select(Command).where(Command.id == id)
items = db.exec(query).all()
if not items:
raise HTTPException(status_code=404, detail="Item Not Found")
else:
for item in items:
db.delete(item)
db.commit()

remaining_query = select(Command)
remaining_items = db.exec(remaining_query).all()
return {"data":remaining_items}




12 changes: 12 additions & 0 deletions backend/api/middlewares/logger_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware

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

class LoggerMiddleware(BaseHTTPMiddleware):
async def dispatch(
Expand All @@ -18,5 +21,14 @@ async def dispatch(
:return: Response from endpoint
"""
# TODO:(Member) Finish implementing this method
start_time = time.time()
request_datetime = datetime.datetime.now().isoformat()
logger.info(f"\nIncoming Request Metadata | Method-{request.method} : URL-{request.url}")
response = await call_next(request)

duration = (time.time() - start_time)*1000
logger.info(
f"\nProcessed | Request Datetime {request_datetime} : Duration {duration} : Params {request.path_params}"
)

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,8 +33,25 @@ 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

# Copy params and format

Choose a reason for hiding this comment

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

this looks good, but make sure to throw and return a value error on failure cases.

Copy link
Author

Choose a reason for hiding this comment

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

I think I added it now :D

params = self.params
format = self.format
if (params == None and format == None):
return self
else:
lenParams = 0
lenFormat = 0
if (params != None):
lenParams = len(params.split(','))

if (format != None):
lenFormat = len(format.split(','))

if (lenParams == lenFormat):
return self

raise ValueError

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