Last active
May 17, 2019 04:30
-
-
Save shalahuddinn/3ed032684d7fbaa253a6657bdbbd5aa1 to your computer and use it in GitHub Desktop.
Current Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This my current application code (Booking system have not been implemented) | |
# The Current Flow of this version: | |
# 1. The user selects the food/drinks. | |
# 2. The user enters his table number. | |
# 3. The user makes payment | |
# 4. The kiosk process the order. (POST to ORDER then POST to ORDERDETAIL). | |
# If Failed (The QTY is not sufficient), the KIOSK return the user's money. | |
# If success then continue to the next step. | |
# 5. Order is done. The kiosk print the receipt. | |
# You can see that there is an non-normal process, the KIOSK return the user's money. | |
# So I would like to change that, since it is not real and does not make sense. | |
# Imagine your card has been debt then the machine return back the debt when the qty of your order is not sufficient. -_- | |
#1 Make POST request to ORDER (CLIENT do this) | |
data = 'cardID=123456&amount=1200000' | |
#2 Response From Server | |
{'id': 40, 'cardID': 123456, 'amount': 500000, 'time': '2019-05-17T11:11:38.384667+07:00'} # I use the ID as the orderID | |
#3 Make POST request to ORDERDETAIL (CLIENT do this) | |
data = '[{"menuID": 3, "price": 100000, "qty": 4, "sellerID": 1, "tableNumber": 56, "orderID": "40"}, | |
{"menuID": 2, "price": 50000, "qty": 2, "sellerID": 2, "tableNumber": 56, "orderID": "40"}]' | |
#4 Checking the QTY in the SERVER (Kindly check the full code of the server at my github) | |
# Filename: api_views.py | |
# Enable Post of List | |
# https://stackoverflow.com/questions/37329771/django-rest-bulk-post-post-array-of-json-objects | |
# Accessed on March 9, 2019 | |
def create(self, request, pk=None, company_pk=None, project_pk=None): | |
is_many = True if isinstance(request.data, list) else False | |
serializer = self.get_serializer(data=request.data, many=is_many) | |
serializer.is_valid(raise_exception=True) | |
self.perform_create(serializer) | |
headers = self.get_success_headers(serializer.data) | |
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) | |
# Filename: models.py | |
def save(self, force_insert=False, force_update=False, using=None, update_fields=None): | |
if self.done: | |
self.finishTime = datetime.datetime.now() | |
else: | |
menuID = self.menuID.id | |
menuObject = Menu.objects.get(id=menuID) | |
tempQty = menuObject.qty - self.qty | |
if tempQty>=0: | |
menuObject.qty = tempQty | |
menuObject.save() | |
else: | |
# return serializers.ValidationError() | |
raise serializers.ValidationError(menuObject.name + ": STOCK IS NOT SUFFICIENT") | |
super().save(force_insert, force_update, using, update_fields) | |
#5 Response From Serverif success | |
[{'id': 41, 'orderID': 40, 'sellerID': 1, 'menuID': 3, 'menuName': 'Nasi Goreng', | |
'image': 'http://127.0.0.1:8000/media/media/Nasi_Goreng_ACL6BZG.jpg', 'price': 100000, 'qty': 4, | |
'tableNumber': 56, 'done': False, 'orderTime': '2019-05-17T11:11:38.384667+07:00', 'finishTime': None}, | |
{'id': 42, 'orderID': 40, 'sellerID': 2, 'menuID': 2, 'menuName': 'Soju', | |
'image': 'http://127.0.0.1:8000/media/media/Soju_gTvqW4D.jpg', 'price': 50000, 'qty': 2, | |
'tableNumber': 56, 'done': False, 'orderTime': '2019-05-17T11:11:38.384667+07:00', 'finishTime': None}] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment