From 561f8e60a570b7755580c93e7a2806d08b7f6990 Mon Sep 17 00:00:00 2001 From: evavaa Date: Tue, 29 Apr 2025 19:25:54 +1000 Subject: [PATCH 1/3] 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/3] 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 From 85df6e80bc6cb1651b61819bcbabd1f3b64591c8 Mon Sep 17 00:00:00 2001 From: Keshang Cheng Date: Tue, 6 May 2025 18:45:05 +1000 Subject: [PATCH 3/3] implement api to get merchant by id --- src/MobileAppAPI/urls.py | 1 + src/MobileAppAPI/views.py | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/MobileAppAPI/urls.py b/src/MobileAppAPI/urls.py index dd1dee16..898089c0 100644 --- a/src/MobileAppAPI/urls.py +++ b/src/MobileAppAPI/urls.py @@ -13,6 +13,7 @@ urlpatterns = [ path('get_merchants/', Views.Merchants, name="get_merchants"), path('get_sponsors/', Views.Sponsors, name="get_sponsers"), + path('get_merchant_location/', Views.MerchantLocation, name="get_merchant_location"), path('update_merchants/', Views.UpdateMerchants, name="update_merchants"), path('update_sponsors/', Views.UpdateMerchants, name="update_sponsers"), path('add_merchants/', Views.AddMerchants, name="add_merchants"), diff --git a/src/MobileAppAPI/views.py b/src/MobileAppAPI/views.py index 6146cf29..be679dea 100644 --- a/src/MobileAppAPI/views.py +++ b/src/MobileAppAPI/views.py @@ -1,6 +1,6 @@ import json -from django.http import HttpResponse, HttpResponseForbidden +from django.http import HttpResponse, HttpResponseForbidden, JsonResponse from django.shortcuts import get_object_or_404 from myCSSAhub.forms import MerchantsForm from myCSSAhub.models import * @@ -37,6 +37,28 @@ def Sponsors(request): jsonRes.append(jsonObj) return HttpResponse(json.dumps(jsonRes), content_type='application/json') + +# New function to get merchant location (longitude and latitude) +def MerchantLocation(request): + merchant_id = request.GET.get('id') + if not merchant_id: + return JsonResponse({'error': 'Merchant ID is required'}, status=400) + + try: + merchant = DiscountMerchant.objects.get(merchant_id=merchant_id, merchant_type='折扣商家') + # Assuming merchant_longitude and merchant_latitude fields exist in your model + location_data = { + 'id': merchant.merchant_id, + 'name': merchant.merchant_name, + 'longitude': merchant.longitude, + 'latitude': merchant.latitude, + 'address': merchant.merchant_address + } + return JsonResponse(location_data) + except DiscountMerchant.DoesNotExist: + return JsonResponse({'error': 'Merchant not found'}, status=404) + + # api_view documentation: # https://www.django-rest-framework.org/api-guide/views/ # Specify the POST request, and authenticated user