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
66 changes: 31 additions & 35 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import Flask, jsonify
from flask import request
from mysql.connector import connection

from db import DB
from itemservice import ItemService, ItemNotFoundException
Expand All @@ -27,62 +28,57 @@ def status():

@app.route("/items", methods=['POST'])
def create_item():
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
description = request.json.get('description')
if not description:
return {}, 400
try:
item = ItemService().create(description)
status_code = 201
except ItemNotFoundException:
return {}, 500
return jsonify(dataclasses.asdict(item)), status_code


@app.route("/items", methods=['GET'])
def list_items():
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
try:
items = ItemService().list(amount=request.args.get('amount'),
order_by=request.args.get('order_by'),
order=request.args.get('order'))
status_code = 200
except Exception as e:
print(e)
items = []
status_code = 404
return jsonify([dataclasses.asdict(item) for item in items]), status_code


@app.route("/items/<item_id>", methods=['GET'])
def get_item(item_id):
try:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
item_id = int(item_id)
item = ItemService().find_by_id(item_id)
status_code = 200
pass
except ItemNotFoundException:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
status_code = 404
return jsonify({'error_code': 'ITEM_NOT_FOUND'}), status_code

#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################

return jsonify(dataclasses.asdict(item)), status_code


@app.route("/items/<item_id>", methods=['PUT'])
def increment_item(item_id):
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
increment = True if request.json.get('increment') else False
if increment:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
try:
item = ItemService().increment(item_id)
status_code = 200
except ItemNotFoundException:
print(ItemNotFoundException)
status_code = 404
return jsonify({'error_code': 'ITEM_NOT_FOUND'}), status_code
return jsonify(dataclasses.asdict(item)), status_code
else:
return {}, 200
69 changes: 46 additions & 23 deletions item.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,62 @@ def __init__(self):
self.db = DB()

def create(self, description: str) -> Item:
#############################
#### WRITE YOUR CODE HERE ###
#############################
try:
cursor = self.db.get_connection()
sql = f'INSERT INTO {self.table_name} (description, available_amount) VALUES (%s, %s)'
cursor.execute(sql, (description, 0))
self.db.commit()
item_id = cursor.lastrowid
cursor.close()
except Exception as e:
print(e)
return None

#############################
return Item(item_id, description, 0)

def list(self, amount=None, order_by=None, order=None) -> List[Item]:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
items = jsons.load(records, List[Item])
try:
cursor = self.db.get_connection()
sql = f'SELECT * FROM {self.table_name}'
if order_by:
sql += f' ORDER BY {order_by}'
if order:
sql += f' {order}'
if amount:
sql += f' LIMIT {amount}'
sql += ';'
cursor.execute(sql)
items = jsons.load(cursor.fetchall(), List[Item])
cursor.close()
except Exception as e:
print(e)
return []
return items

def find_by_id(self, item_id: int, lock=False) -> Optional[Item]:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################

try:
sql = f'SELECT * FROM {self.table_name} WHERE id = %s'
cursor = self.db.get_connection()
cursor.execute(sql, (item_id,))
records = cursor.fetchall()
cursor.close()
except Exception as e:
print(e)
return None
if not records:
return None

item = jsons.load(records[0], Item)
return item

def increment(self, item: Item) -> Item:
#############################
#### WRITE YOUR CODE HERE ###
#############################

#############################
return item
try:
cursor = self.db.get_connection()
sql = f'UPDATE {self.table_name} SET available_amount = available_amount + 1 WHERE id = %s'
cursor.execute(sql, (item.id,))
self.db.commit()
item = self.find_by_id(item.id)
cursor.close()
except Exception as e:
print(e)
return None
return item