Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/MobileAppAPI/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
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"),
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')
]
54 changes: 53 additions & 1 deletion src/MobileAppAPI/views.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -108,3 +130,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')