Ejemplo n.º 1
0
    def post(self):  # create order (at least one item needed)
        # get fields info
        json = flask.request.json
        fields = ['place_id', 'item_id', 'token']
        helper_functions.check_missing_fields(json, *fields)
        place_id, item_id, token = map(lambda x: json[x], fields)

        user_id, _ = helper_functions.decode_jwt(jwt_token=token)

        place_vertex: db.Place = helper_functions.get_vertex_or_404(
            place_id, db.Place)
        user_vertex: db.User = helper_functions.get_vertex_or_404(
            user_id, Label=db.User)
        item_vertex: db.MenuItem = helper_functions.get_vertex_or_404(
            item_id, Label=db.MenuItem)

        is_item_from_place = item_vertex.query().through_incoming_edge(
            db.HasItem).through_incoming_edge(db.HasMenu).filter_by_property(
                id=place_id).count().fetch_first() == 1

        if is_item_from_place:
            order_vertex = db.Order(False, place_vertex, user_vertex)
            order_vertex.add_item(item_vertex)
            return {
                'message': 'Order created and item added',
                'id': order_vertex.graph_value.id,
                'items': [{
                    item_vertex.graph_value.id: item_vertex.Properties
                }]
            }, HTTPStatus.OK

        else:
            return 'Item is not from the specified place', HTTPStatus.NOT_FOUND
Ejemplo n.º 2
0
    def post(self):  # create

        # get fields info
        json = flask.request.json
        fields = ['name', 'menu_id', 'value', 'description', 'token']
        helper_functions.check_missing_fields(json, *fields)
        name, menu_id, value, description, token = map(lambda x: json[x],
                                                       fields)

        administrator_id, _ = helper_functions.decode_jwt(jwt_token=token)

        menu_vertex: db.Menu = janusgraphy.Query.from_vertex_id(
            menu_id).filter_by_property(Label=db.Menu).fetch_first()
        admin_id: db.User = janusgraphy.Query.from_vertex_id(
            administrator_id).filter_by_property(Label=db.User).fetch_first()

        count = admin_id.query().through_outgoing_edge(
            db.Administers).through_outgoing_edge(
                db.HasMenu).filter_by_property(
                    id=menu_vertex.graph_value.id).count().fetch_first()

        if count == 1:
            menu_item_vertex = db.MenuItem(name=name,
                                           value=value,
                                           description=description,
                                           menu=menu_vertex)
            return {
                menu_item_vertex.graph_value.id: menu_item_vertex.Properties
            }, HTTPStatus.OK

        else:
            return 'No menu with id specified', HTTPStatus.NOT_FOUND
Ejemplo n.º 3
0
    def get(self):
        json = flask.request.args

        helper_functions.check_missing_fields(json, 'token')
        token = json['token']

        user_id, _ = helper_functions.decode_jwt(jwt_token=token)
        user_places = janusgraphy.Query.from_vertex_id(user_id).through_outgoing_edge(db.Administers).fetch_all()

        return {p.graph_value.id: p.Properties for p in user_places}, HTTPStatus.OK
Ejemplo n.º 4
0
    def put(self):
        json = flask.request.json
        fields = ['order_id', 'token']
        helper_functions.check_missing_fields(json, *fields)
        order_id, token = map(lambda x: json[x], fields)

        user_id, _ = helper_functions.decode_jwt(jwt_token=token)

        order_vertex = helper_functions.get_vertex_or_404(order_id, db.Order)
        if order_vertex.is_from_user(user_id):
            order_vertex.accept()
        else:
            return 'Invalid order id', HTTPStatus.NOT_FOUND
Ejemplo n.º 5
0
    def get(self, place_id):  # get
        json = flask.request.args

        helper_functions.check_missing_fields(json, 'token')
        token = json['token']

        user_id, _ = helper_functions.decode_jwt(jwt_token=token)
        user_places = janusgraphy.Query.from_vertex_id(user_id).through_outgoing_edge(db.Administers).fetch_all()
        places_info = {p.graph_value.id: p.Properties for p in user_places}

        if place_id in places_info:
            return places_info[place_id], HTTPStatus.OK
        else:
            return 'Place not found or not authorized', HTTPStatus.NOT_FOUND
Ejemplo n.º 6
0
    def put(self):  # add item to order
        # get fields info
        json = flask.request.json
        fields = ['order_id', 'item_id', 'token']
        helper_functions.check_missing_fields(json, *fields)
        order_id, item_id, token = map(lambda x: json[x], fields)

        user_id, _ = helper_functions.decode_jwt(jwt_token=token)

        order_vertex: db.Order = helper_functions.get_vertex_or_404(
            order_id, db.Order, 'order not found')
        item_vertex: db.MenuItem = helper_functions.get_vertex_or_404(
            item_id, db.MenuItem, 'item not found')

        if order_vertex.is_from_user(user_id):
            order_vertex.add_item(item_vertex)
            return 'Item added', HTTPStatus.OK

        else:
            return 'Invalid order id', HTTPStatus.NOT_FOUND
Ejemplo n.º 7
0
    def post(self):  # create
        json = flask.request.json
        fields = ['name', 'display_name', 'token']
        helper_functions.check_missing_fields(json, *fields)

        name, display_name, token = map(lambda x: json[x], fields)

        administrator_id, _ = helper_functions.decode_jwt(jwt_token=token)

        admin_user_vertex = janusgraphy.Query.from_vertex_id(administrator_id).fetch_first()

        owned_places_query = admin_user_vertex.query().through_outgoing_edge(db.Administers)
        owned_places_with_same_name = list(owned_places_query.filter_by_property(name=name).fetch_all())

        if len(owned_places_with_same_name) == 0:
            place_vertex = db.Place(name=name, display_name=display_name, adm=admin_user_vertex)
            place_vertex.add_to_graph()
            return {"message": "Place created", 'id': place_vertex.graph_value.id}, HTTPStatus.OK

        else:
            return "There is already a place with the same name", HTTPStatus.CONFLICT
Ejemplo n.º 8
0
    def post(self):  # create

        # get fields info
        json = flask.request.json
        fields = ['name', 'place_id', 'token']
        helper_functions.check_missing_fields(json, *fields)
        name, place_id, token = map(lambda x: json[x], fields)

        # get user info (no need to check if the vertex is a User vertex as the token integrity is granted)
        administrator_id, _ = helper_functions.decode_jwt(jwt_token=token)
        admin_user_vertex = helper_functions.get_vertex_or_404(
            administrator_id, db.User)
        place_vertex = helper_functions.get_vertex_or_404(place_id, db.Place)

        #checks whether the user owns the place given
        place_count = admin_user_vertex.query().through_outgoing_edge(
            db.Administers).filter_by_property(
                id=place_vertex.graph_value.id).count().fetch_first()

        if place_count == 1:
            menu_query = place_vertex.query().through_edge(db.HasMenu)
            owned_menus_with_same_name_count = menu_query.filter_by_property(
                name=name).count().fetch_first()

            if owned_menus_with_same_name_count == 0:
                menu_vertex = db.Menu(name=name, place=place_vertex)
                menu_vertex.add_to_graph()
                return {
                    "message": "Menu created",
                    'id': menu_vertex.graph_value.id
                }, HTTPStatus.OK

            else:
                return "There is already a manu with the same name", HTTPStatus.CONFLICT
        else:
            return "The user does not own the place", HTTPStatus.FORBIDDEN
Ejemplo n.º 9
0
    def get(self):
        json = flask.request.args

        if 'place_id' in json:

            fields = ['place_id', 'token']
            helper_functions.check_missing_fields(json, *fields)
            place_id, token = map(lambda x: json[x], fields)

            place_vertex: db.Place = helper_functions.get_vertex_or_404(
                place_id, db.Place)
            user_id, _ = helper_functions.decode_jwt(jwt_token=token)

            q_relation = janusgraphy.Query.relation()  # represents the order
            q_relation.through_incoming_edge(db.Orders).filter_by_property(
                id=user_id, Label=db.User)

            user_orders_from_place_q = place_vertex.query(verbose=True)
            user_orders_from_place_q.through_incoming_edge(db.From)
            user_orders_from_place_q.filter_by_property(done=False)
            user_orders_from_place_q.filter_by_relation(q_relation)
            user_orders_from_place: List[
                db.Order] = user_orders_from_place_q.fetch_all()
            orders = {}
            for order in user_orders_from_place:
                q = order.query()
                q.through_outgoing_edge(db.Has)
                q.filter_by_property(Label=db.MenuItem)
                items: List[db.MenuItem] = q.fetch_all()
                orders[order.graph_value.id] = {
                    "items": [{
                        **item.Properties, 'id': item.graph_value.id
                    } for item in items]
                }

            return orders, HTTPStatus.OK

        elif 'order_id' in json:
            fields = ['order_id', 'token']
            helper_functions.check_missing_fields(json, *fields)
            order_id, token = map(lambda x: json[x], fields)

            order_vertex: db.Order = helper_functions.get_vertex_or_404(
                order_id, db.Order)
            user_id, _ = helper_functions.decode_jwt(jwt_token=token)

            if order_vertex.is_from_user(user_id):
                q = order_vertex.query()
                q.through_outgoing_edge(db.Has)
                q.filter_by_property(Label=db.MenuItem)
                items: List[db.MenuItem] = q.fetch_all()
                return {
                    "items": [{
                        **item.Properties, 'id': item.graph_value.id
                    } for item in items]
                }, HTTPStatus.OK
            else:
                return 'order not found', HTTPStatus.NOT_FOUND

        else:
            return 'missing place_id or order_id', HTTPStatus.BAD_REQUEST