From 561f8e60a570b7755580c93e7a2806d08b7f6990 Mon Sep 17 00:00:00 2001 From: evavaa Date: Tue, 29 Apr 2025 19:25:54 +1000 Subject: [PATCH 1/2] add api that returns merchants filtered by category --- src/MobileAppAPI/urls.py | 3 ++- src/MobileAppAPI/views.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/MobileAppAPI/urls.py b/src/MobileAppAPI/urls.py index dd1dee16..762626cf 100644 --- a/src/MobileAppAPI/urls.py +++ b/src/MobileAppAPI/urls.py @@ -16,5 +16,6 @@ path('update_merchants/', Views.UpdateMerchants, name="update_merchants"), path('update_sponsors/', Views.UpdateMerchants, name="update_sponsers"), path('add_merchants/', Views.AddMerchants, name="add_merchants"), - path('add_sponsors/', Views.AddMerchants, name="add_sponsers") + path('add_sponsors/', Views.AddMerchants, name="add_sponsers"), + path('get_merchants_by_category/', Views.MerchantsByCategory, name='get_merchants_by_category') ] diff --git a/src/MobileAppAPI/views.py b/src/MobileAppAPI/views.py index 6146cf29..6daab23a 100644 --- a/src/MobileAppAPI/views.py +++ b/src/MobileAppAPI/views.py @@ -108,3 +108,37 @@ def AddMerchants(request): return HttpResponse(json.dumps({'update': have_update}), content_type='application/json') else: return HttpResponseForbidden("No permission") + + category = self.kwargs.get('category') + merchants = DiscountMerchant.objects.filter(merchant_Category=category).order_by("merchant_add_date").values() + return render(request, self.template_name, locals()) + +def MerchantsByCategory(request): + """ + Return merchants filtered by category + """ + + # Decode the request body from bytes to string using UTF-8 + body_unicode = request.body.decode('utf-8') + body_data = json.loads(body_unicode) + category = body_data.get('category') + # print(request.body) + # print(category) + + merchants = DiscountMerchant.objects.filter( + merchant_type='折扣商家', + merchant_Category=category + ).order_by("merchant_add_date") + + jsonRes = [] + for merchant in merchants: + jsonObj = dict( + id=merchant.merchant_id, + name=merchant.merchant_name, + sale=merchant.merchant_description, + location=merchant.merchant_address, + img=str(merchant.merchant_image.url) + ) + jsonRes.append(jsonObj) + + return HttpResponse(json.dumps(jsonRes), content_type='application/json') \ No newline at end of file From 5671c61969ba793e8a3e8aa12d03d3007b3ac833 Mon Sep 17 00:00:00 2001 From: evavaa Date: Tue, 29 Apr 2025 19:29:46 +1000 Subject: [PATCH 2/2] delete redundant code --- src/MobileAppAPI/views.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/MobileAppAPI/views.py b/src/MobileAppAPI/views.py index 6daab23a..803a113a 100644 --- a/src/MobileAppAPI/views.py +++ b/src/MobileAppAPI/views.py @@ -109,10 +109,6 @@ def AddMerchants(request): else: return HttpResponseForbidden("No permission") - category = self.kwargs.get('category') - merchants = DiscountMerchant.objects.filter(merchant_Category=category).order_by("merchant_add_date").values() - return render(request, self.template_name, locals()) - def MerchantsByCategory(request): """ Return merchants filtered by category