Skip to content

Commit 69019d4

Browse files
committed
backend
1 parent bcd74d4 commit 69019d4

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

backend/api/endpoints/command.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,43 @@ def get_commands(db: Session = Depends(get_db)):
2424

2525

2626
@command_router.post("/", response_model=CommandSingleResponse)
27-
def create_command(payload: CommandRequest):
27+
def create_command(payload: CommandRequest, db: Session = Depends(get_db)):
2828
"""
2929
Creates an item with the given payload in the database and returns this payload after pulling it from the database
3030
3131
@param payload: The data used to create an item
3232
@return returns a json object with field of "data" under which there is the payload now pulled from the database
3333
"""
34-
# TODO:(Member) Implement this endpoint
35-
34+
try:
35+
posted_command = Command(**payload.dict())
36+
db.add(posted_command)
37+
db.commit()
38+
db.refresh(posted_command)
39+
return {"data": posted_command}
40+
except Exception as error:
41+
db.rollback()
42+
raise HTTPException(
43+
status_code = 500,
44+
detail = str(error),
45+
)
3646

3747

3848
@command_router.delete("/{id}", response_model=CommandListResponse)
39-
def delete_command(id: int):
49+
def delete_command(id: int, db: Session = Depends(get_db)):
4050
"""
4151
Deletes the item with the given id if it exists. Otherwise raises a 404 error.
4252
4353
@param id: The id of the item to delete
4454
@return returns the list of commands after deleting the item
4555
"""
56+
DeletedCommand = db.get(Command, id)
57+
if DeletedCommand is None:
58+
raise HTTPException(status_code=404, detail = "Command not found")
59+
db.delete(DeletedCommand)
60+
db.commit()
61+
return get_commands(db)
62+
63+
64+
65+
4666
# TODO:(Member) Implement this endpoint

backend/api/middlewares/logger_middleware.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections.abc import Callable
22
from typing import Any
33
from fastapi import Request, Response
4+
from ...utils.logging import logger
45
from starlette.middleware.base import BaseHTTPMiddleware
6+
from time import time
57

68

79
class LoggerMiddleware(BaseHTTPMiddleware):
@@ -18,5 +20,17 @@ async def dispatch(
1820
@return Response from endpoint
1921
"""
2022
# TODO:(Member) Finish implementing this method
23+
logger.info(f"Req headers:{dict(request.headers)}")
24+
logger.info(f"Req: {request.method} {request.url}")
25+
try:
26+
body = await response.json()
27+
logger.info("Req Body: {body}")
28+
except Exception as error:
29+
logger.warning(f"Unable to parse Request Body. Error: {str(error)}")
30+
startTime = time()
2131
response = await call_next(request)
32+
executionTime = time() - startTime
33+
logger.info(f"Response status: {response.status_code}")
34+
logger.info(f"Response Headers: {response.headers}")
35+
logger.info(f"Response Time:{executionTime:.2f} seconds")
2236
return response

backend/data/data_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ def validate_params_format(self):
3232
In either of these cases return self. Otherwise raise a ValueError.
3333
The format of the comma seperated values is "data1,data2" so no spaces between data and the commas.
3434
"""
35+
params = self.params
36+
format = self.format
37+
if (not params and not format):
38+
return self
39+
elif (type(params) ==str and type(format)==str):
40+
if(params.count(",")==format.count(",")):
41+
return self
42+
else:
43+
raise ValueError(f"""Error in Params and Format. Both Params and Format must have no spaces between data with commas separating each unique value or both must be None.
44+
The Current Params and Format are as follows:
45+
Params: {params}
46+
Format: {format}
47+
""")
3548
# TODO: (Member) Implement this method
3649
return self
3750

0 commit comments

Comments
 (0)