Skip to content

Commit 482f750

Browse files
authored
Merge pull request #262 from chengkeshang/api-merchant-id-to-location
implement api to get merchant by id
2 parents bbf913b + 85df6e8 commit 482f750

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/MobileAppAPI/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
urlpatterns = [
1414
path('get_merchants/', Views.Merchants, name="get_merchants"),
1515
path('get_sponsors/', Views.Sponsors, name="get_sponsers"),
16+
path('get_merchant_location/', Views.MerchantLocation, name="get_merchant_location"),
1617
path('update_merchants/', Views.UpdateMerchants, name="update_merchants"),
1718
path('update_sponsors/', Views.UpdateMerchants, name="update_sponsers"),
1819
path('add_merchants/', Views.AddMerchants, name="add_merchants"),

src/MobileAppAPI/views.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22

3-
from django.http import HttpResponse, HttpResponseForbidden
3+
from django.http import HttpResponse, HttpResponseForbidden, JsonResponse
44
from django.shortcuts import get_object_or_404
55
from myCSSAhub.forms import MerchantsForm
66
from myCSSAhub.models import *
@@ -37,6 +37,28 @@ def Sponsors(request):
3737
jsonRes.append(jsonObj)
3838
return HttpResponse(json.dumps(jsonRes), content_type='application/json')
3939

40+
41+
# New function to get merchant location (longitude and latitude)
42+
def MerchantLocation(request):
43+
merchant_id = request.GET.get('id')
44+
if not merchant_id:
45+
return JsonResponse({'error': 'Merchant ID is required'}, status=400)
46+
47+
try:
48+
merchant = DiscountMerchant.objects.get(merchant_id=merchant_id, merchant_type='折扣商家')
49+
# Assuming merchant_longitude and merchant_latitude fields exist in your model
50+
location_data = {
51+
'id': merchant.merchant_id,
52+
'name': merchant.merchant_name,
53+
'longitude': merchant.longitude,
54+
'latitude': merchant.latitude,
55+
'address': merchant.merchant_address
56+
}
57+
return JsonResponse(location_data)
58+
except DiscountMerchant.DoesNotExist:
59+
return JsonResponse({'error': 'Merchant not found'}, status=404)
60+
61+
4062
# api_view documentation:
4163
# https://www.django-rest-framework.org/api-guide/views/
4264
# Specify the POST request, and authenticated user

0 commit comments

Comments
 (0)