From 8779474b8fb24b8e8b52eab1a56a42a076c42a86 Mon Sep 17 00:00:00 2001 From: ipaullly Date: Fri, 23 Nov 2018 09:13:49 +0300 Subject: [PATCH] [starts #162163623] Add GET order by id endpoint --- app/api/v2/__init__.py | 6 ++++-- app/api/v2/dbmodel.py | 5 +++-- app/api/v2/models.py | 11 ++++++++++- app/api/v2/views.py | 22 ++++++++++++++++++++++ app/auth/v2/models.py | 2 +- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/api/v2/__init__.py b/app/api/v2/__init__.py index 358a56f..819564d 100644 --- a/app/api/v2/__init__.py +++ b/app/api/v2/__init__.py @@ -1,7 +1,8 @@ from flask import Blueprint from flask_restful import Api #from ...api.v2.views import ParcelList, ParcelDestination, ParcelStatus, ParcelCurrentLocation, CancelParcel, UserOrders -from app.api.v2.views import ParcelList, ParcelDestination, ParcelStatus, ParcelCurrentLocation, CancelParcel, UserOrders +from app.api.v2.views import ParcelList, ParcelDestination, ParcelStatus, \ +ParcelCurrentLocation, CancelParcel, UserOrders, IndividualParcel version2 = Blueprint('v2', __name__, url_prefix="/api/v2") @@ -12,4 +13,5 @@ api.add_resource(ParcelStatus, '/parcels//status') api.add_resource(ParcelCurrentLocation, '/parcels//presentLocation') api.add_resource(CancelParcel, '/parcels//cancel') -api.add_resource(UserOrders, '/users//parcels') \ No newline at end of file +api.add_resource(UserOrders, '/users//parcels') +api.add_resource(IndividualParcel, '/parcels/') \ No newline at end of file diff --git a/app/api/v2/dbmodel.py b/app/api/v2/dbmodel.py index acda444..0db05aa 100644 --- a/app/api/v2/dbmodel.py +++ b/app/api/v2/dbmodel.py @@ -48,7 +48,7 @@ def persist_to_db(cls, query_string, tuple_data): cls.conn.commit() @classmethod - def add_to_db(cls, query_string, tuple_data): + def insert_fetch_from_db(cls, query_string, tuple_data): """ method that saves queries into the database """ @@ -70,7 +70,8 @@ def retrieve_one(cls, query_string): method returns data on a particular row from the database """ cls.cur.execute(query_string) - return cls.cur.fetchone() + result = cls.cur.fetchone() + return result @classmethod def retrieve_all(cls, query_string): diff --git a/app/api/v2/models.py b/app/api/v2/models.py index 3526fbc..6ca8325 100644 --- a/app/api/v2/models.py +++ b/app/api/v2/models.py @@ -33,7 +33,15 @@ def order_list(self): query = """SELECT item_name, destination, status, current_location, order_id FROM orders ORDER BY order_id ASC;""" resp = SenditDb.retrieve_all(query) return resp - + def retrieve_single_order(self, parcel_id): + """ + retrieves an order by id + """ + + query = """SELECT * FROM orders WHERE order_id={}""".format(parcel_id) + resp = SenditDb.retrieve_one(query) + return resp + def update_destination(self, new_dest, parcel_id): """ updates the destination of a user's parcels @@ -41,6 +49,7 @@ def update_destination(self, new_dest, parcel_id): payload = { "updated_destination" : new_dest } + input_query = """SELECT destination FROM orders WHERE order_id={}""".format(parcel_id) response = SenditDb.retrieve_one(input_query) if not response: diff --git a/app/api/v2/views.py b/app/api/v2/views.py index 65a4a91..c3f1b25 100644 --- a/app/api/v2/views.py +++ b/app/api/v2/views.py @@ -113,6 +113,28 @@ def get(self): "message" : "No orders in the database" }), 400) +class IndividualParcel(Resource): + """ + class for API endpoints for retrieving single order and cancelling particular order + """ + def get(self, id): + """ + get method to retrieve order by id + """ + + individ_order = order.order_list() + + for parcel in individ_order: + if parcel["order_id"] == id: + return make_response(jsonify({ + "message" : "Ok", + "order" : parcel + }), 200) + else: + response = { + "message" : "Invalid id" + } + return make_response(jsonify(response), 400) class ParcelDestination(Resource): """ diff --git a/app/auth/v2/models.py b/app/auth/v2/models.py index caaba9d..d059c36 100644 --- a/app/auth/v2/models.py +++ b/app/auth/v2/models.py @@ -20,7 +20,7 @@ def add_user(self, email, password): return False user_query = """INSERT INTO users (email, password) VALUES (%s, %s) RETURNING email, id""" tup = (email, hashed_password) - resp = SenditDb.add_to_db(user_query, tup) + resp = SenditDb.insert_fetch_from_db(user_query, tup) payload = resp return payload