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..803a113a 100644 --- a/src/MobileAppAPI/views.py +++ b/src/MobileAppAPI/views.py @@ -108,3 +108,33 @@ def AddMerchants(request): return HttpResponse(json.dumps({'update': have_update}), content_type='application/json') else: return HttpResponseForbidden("No permission") + +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