From f7f72f00f43183525843e4b9a53d6d5b0bf35dc1 Mon Sep 17 00:00:00 2001 From: _Kevin_Zhang_ <49049813+Dragollax@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:36:56 -0700 Subject: [PATCH 1/4] initial --- backend/auction/views.py | 2 +- backend/bid/signals.py | 2 +- backend/bid/views.py | 11 +++++++++-- backend/core/settings.py | 2 ++ frontend/src/pages/ListingsPage.jsx | 25 ++++++++++++++++++++++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/backend/auction/views.py b/backend/auction/views.py index a5ab3f5..15b3155 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -317,7 +317,7 @@ def get(self, request, **kwargs): class AuctionItemsApiView(APIView): - permission_classes = [IsAuthenticated] + permission_classes = [AllowAny] def get_filtered_queryset(self, model, auction_day_id, filters): """ diff --git a/backend/bid/signals.py b/backend/bid/signals.py index d4b6382..1933356 100644 --- a/backend/bid/signals.py +++ b/backend/bid/signals.py @@ -43,7 +43,7 @@ def bid_updated(sender, instance, created, **kwargs): } async_to_sync(channel_layer.group_send)( - "auction_{}".format(instance.auction_id), + "bid_updates", {"type": "bid.update", "bid_data": bid_data}, ) diff --git a/backend/bid/views.py b/backend/bid/views.py index a673319..0aef4fb 100644 --- a/backend/bid/views.py +++ b/backend/bid/views.py @@ -15,6 +15,7 @@ class BidListApiView(APIView): permission_classes = [IsAuthenticated] + serializer_class = BidSerializer cognitoService = AWSCognitoService() @@ -52,8 +53,14 @@ def post(self, request, *args, **kwargs): item = get_object_or_404(model, id=data.get("object_id")) - highest_bid = Bid.objects.filter(item=item).order_by("-amount").first() - if highest_bid and int(data["amount"]) <= highest_bid.amount: + highest_bid = Bid.objects.filter(object_id=item.id).order_by("-amount").first() + if highest_bid is None: + if int(data["amount"]) < item.starting_price: + return Response( + {"error": "Your bid must be higher than the starting price."}, + status=status.HTTP_400_BAD_REQUEST, + ) + elif highest_bid and int(data["amount"]) <= highest_bid.amount: return Response( {"error": "Your bid must be higher than the current highest bid."}, status=status.HTTP_400_BAD_REQUEST, diff --git a/backend/core/settings.py b/backend/core/settings.py index a68d744..dd2d57e 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -15,7 +15,9 @@ from pathlib import Path from corsheaders.defaults import default_headers +from dotenv import load_dotenv +load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent diff --git a/frontend/src/pages/ListingsPage.jsx b/frontend/src/pages/ListingsPage.jsx index 2fb3506..aa8abb4 100644 --- a/frontend/src/pages/ListingsPage.jsx +++ b/frontend/src/pages/ListingsPage.jsx @@ -40,7 +40,9 @@ export default function ListingsPage() { const [selectedMinPrice, setSelectedMinPrice] = useState(0); const [selectedMaxPrice, setSelectedMaxPrice] = useState(0); const { fetchData } = useAxios(); - + const wsURL = process.env.REACT_APP_NODE_ENV === 'dev' + ? process.env.REACT_APP_DEV_BACKEND_WS_BASE_URL + : process.env.REACT_APP_PROD_BACKEND_WS_BASE_URL const sortByItems = ['All', 'Trucks', 'Equipment', 'Trailers']; const updateMinPrice = ({ target: { value } }) => { @@ -51,6 +53,27 @@ export default function ListingsPage() { } }; + useEffect(() => { + const chatSocket = new WebSocket(wsURL); + + chatSocket.onmessage = function(event) { + const message = JSON.parse(event.data); + + if (message.bid_data) { + const bidData = message.bid_data; + + setUnits(prevUnits => + prevUnits.map(unit => + unit.id === bidData.object_id + ? { ...unit, current_price: bidData.amount } + : unit + ) + ); + } + }; + + return () => chatSocket.close(); + }, []); const updateMaxPrice = ({ target: { value } }) => { if (value === '') { setSelectedMaxPrice(0); From 9e2a9e8b8297c2e77a7cc045eee408946b2ecdf2 Mon Sep 17 00:00:00 2001 From: _Kevin_Zhang_ <49049813+Dragollax@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:41:36 -0700 Subject: [PATCH 2/4] revert changes --- backend/auction/views.py | 2 +- backend/core/settings.py | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/auction/views.py b/backend/auction/views.py index 15b3155..a5ab3f5 100644 --- a/backend/auction/views.py +++ b/backend/auction/views.py @@ -317,7 +317,7 @@ def get(self, request, **kwargs): class AuctionItemsApiView(APIView): - permission_classes = [AllowAny] + permission_classes = [IsAuthenticated] def get_filtered_queryset(self, model, auction_day_id, filters): """ diff --git a/backend/core/settings.py b/backend/core/settings.py index dd2d57e..a68d744 100644 --- a/backend/core/settings.py +++ b/backend/core/settings.py @@ -15,9 +15,7 @@ from pathlib import Path from corsheaders.defaults import default_headers -from dotenv import load_dotenv -load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent From 33e6151b5c61899408492978d60a2c2012b55e76 Mon Sep 17 00:00:00 2001 From: _Kevin_Zhang_ <49049813+Dragollax@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:47:27 -0700 Subject: [PATCH 3/4] convention --- frontend/src/pages/ListingsPage.jsx | 45 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/frontend/src/pages/ListingsPage.jsx b/frontend/src/pages/ListingsPage.jsx index aa8abb4..4ce726c 100644 --- a/frontend/src/pages/ListingsPage.jsx +++ b/frontend/src/pages/ListingsPage.jsx @@ -53,27 +53,30 @@ export default function ListingsPage() { } }; - useEffect(() => { - const chatSocket = new WebSocket(wsURL); - - chatSocket.onmessage = function(event) { - const message = JSON.parse(event.data); - - if (message.bid_data) { - const bidData = message.bid_data; - - setUnits(prevUnits => - prevUnits.map(unit => - unit.id === bidData.object_id - ? { ...unit, current_price: bidData.amount } - : unit - ) - ); - } - }; - - return () => chatSocket.close(); - }, []); + function handleWebSocketMessage(event) { + const message = JSON.parse(event.data); + + if (message.bid_data) { + const bidData = message.bid_data; + + setUnits(prevUnits => + prevUnits.map(unit => + unit.id === bidData.object_id + ? { ...unit, current_price: bidData.amount } + : unit + ) + ); + } + } + + useEffect(() => { + const chatSocket = new WebSocket(wsURL); + + chatSocket.onmessage = handleWebSocketMessage; + + return () => chatSocket.close(); + }, []); + const updateMaxPrice = ({ target: { value } }) => { if (value === '') { setSelectedMaxPrice(0); From c7c151c4b573b911d0b5a4c4b3991ea783e3d42c Mon Sep 17 00:00:00 2001 From: _Kevin_Zhang_ <49049813+Dragollax@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:48:35 -0700 Subject: [PATCH 4/4] arrow function --- frontend/src/pages/ListingsPage.jsx | 44 ++++++++++++++--------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/frontend/src/pages/ListingsPage.jsx b/frontend/src/pages/ListingsPage.jsx index 4ce726c..c58fca1 100644 --- a/frontend/src/pages/ListingsPage.jsx +++ b/frontend/src/pages/ListingsPage.jsx @@ -53,30 +53,28 @@ export default function ListingsPage() { } }; - function handleWebSocketMessage(event) { - const message = JSON.parse(event.data); - - if (message.bid_data) { - const bidData = message.bid_data; + useEffect(() => { + const chatSocket = new WebSocket(wsURL); - setUnits(prevUnits => - prevUnits.map(unit => - unit.id === bidData.object_id - ? { ...unit, current_price: bidData.amount } - : unit - ) - ); - } - } - - useEffect(() => { - const chatSocket = new WebSocket(wsURL); - - chatSocket.onmessage = handleWebSocketMessage; - - return () => chatSocket.close(); - }, []); - + chatSocket.onmessage = (event) => { + const message = JSON.parse(event.data); + + if (message.bid_data) { + const bidData = message.bid_data; + + setUnits(prevUnits => + prevUnits.map(unit => + unit.id === bidData.object_id + ? { ...unit, current_price: bidData.amount } + : unit + ) + ); + } + }; + + return () => chatSocket.close(); + }, []); + const updateMaxPrice = ({ target: { value } }) => { if (value === '') { setSelectedMaxPrice(0);