def post(self): data = request.get_json(force=True) data['_id'] = 0 order = OrdersModel(data) try: order.insert_to_db() except: return {"message": "An error occurred inserting the order."}, 500 return order.json(), 201
def put(self, order_id): # Create or Update data = request.get_json(force=True) order = OrdersModel.find_by_order_id(order_id) if order is None: order = OrdersModel(data['action'], data['symbol'], data['price'], data['order_type'], data['order_size'], data['value'], data['account_id']) else: order.price = data['price'] order.order_size = data['order_size'] order.value = data['value'] order.order_type = data['order_type'] order.update_to_db() return order.json()
def post(self, username): data = self.parser() with lock.lock: if username == g.user.username: a = AccountsModel.find_by_username(username) e = EventModel.find_by_id(data['event_id']) if e.total_available_tickets >= data["tickets_bought"]: if a.available_money >= e.price * data["tickets_bought"]: e.total_available_tickets = EventModel.total_available_tickets - data[ "tickets_bought"] a.available_money -= data["tickets_bought"] * e.price o = OrdersModel(e.id, data["tickets_bought"]) a.orders.append(o) db.session.commit() return {"order": o.json()}, 201 else: return {'message': 'Not enough money'}, 501 else: return {'message': 'No enough tickets'}, 502 else: return { 'message': 'Endpoint username and g.user.username not equal' }, 400
def post(self, username): user = AccountsModel.find_by_username(username) if user: parser = reqparse.RequestParser() parser.add_argument(n.IDEVNT, type=int, required=True, help="Id of the event") parser.add_argument(n.TCK, type=int, required=True, help="Number of tickets the user wants to buy") data = parser.parse_args() with lock.lock: if username == g.user.username: event = EventModel.find_by_id(data[n.IDEVNT]) if event: price = event.price*data[n.TCK] if user.available_money >= price: if event.total_available_tickets >= data[n.TCK]: try: user.pay(price) event.sell(data[n.TCK]) new_order = OrdersModel(data[n.IDEVNT], data[n.TCK]) user.orders.append(new_order) new_order.save_to_db() user.save_to_db() event.save_to_db() return new_order.json(), 201 except: return {n.MSG: "Error Description"}, 500 else: return {n.MSG: "The event does not have enough available ticket"}, 400 else: return {n.MSG: "The user does not have enough money"}, 400 else: return {n.MSG: "Event not found"}, 404 else: return {n.MSG: "Username error"}, 400 else: return {n.MSG: "Username not found"}, 404