def put(self, ean):
try:
args = parser.parse_args()
except Exception:
return abort(400, message="Invalid arguments")
with db:
with db.cursor() as cursor:
user_id = get_or_create_id(args['token'])
cursor.execute(
"SELECT products.name, products.type FROM fridge_items"
" JOIN products ON products.ean=fridge_items.ean"
" WHERE fridge_items.user_id=%s AND fridge_items.ean=%s",
[user_id[0], ean])
query = cursor.fetchone()
if query is None:
p = Product.get(ean) # Produces 404 if not exists
cursor.execute("INSERT INTO fridge_items (ean, user_id) VALUES (%s, %s)", [ean, user_id[0]])
return p, 201
else:
return {
"name": query[0],
"type": query[1]
}, 200
评论列表
文章目录