Skip to content
Open
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
6 changes: 4 additions & 2 deletions app/api/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -12,4 +13,5 @@
api.add_resource(ParcelStatus, '/parcels/<int:id>/status')
api.add_resource(ParcelCurrentLocation, '/parcels/<int:id>/presentLocation')
api.add_resource(CancelParcel, '/parcels/<int:id>/cancel')
api.add_resource(UserOrders, '/users/<int:id>/parcels')
api.add_resource(UserOrders, '/users/<int:id>/parcels')
api.add_resource(IndividualParcel, '/parcels/<int:id>')
5 changes: 3 additions & 2 deletions app/api/v2/dbmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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):
Expand Down
11 changes: 10 additions & 1 deletion app/api/v2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ 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
"""
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:
Expand Down
22 changes: 22 additions & 0 deletions app/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion app/auth/v2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down