Esempio n. 1
0
    def get_all_items():
        items_object = item_collection.find({})

        items = []
        for item in items_object:
            items.append(item)

        if len(items) == 0:
            return Tools.Result(False, Tools.errors('INF'))

        items = Item._get_gallery_image_urls_for_items(items)

        for item in items:
            menu_image_id = item['MenuImageUrl']['MenuImageId']
            item.pop('MenuImageUrl')
            item[
                'MenuImageUrl'] = 'https://cafe-art-backend.liara.run/item/menu/image/{}'.format(
                    menu_image_id)

            item_image_id = item['ItemImageUrl']['ItemImageId']
            item.pop('ItemImageUrl')
            item[
                'ItemImageUrl'] = 'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item_image_id)

        return Tools.Result(True, Tools.dumps(items))
Esempio n. 2
0
    def get_all_items_by_category():

        categories = Category._get_categories()
        print(categories)

        items_object = item_collection.find({}, {
            '_id': 0,
            'RowId': 1,
            'Title': 1
        })

        items = []
        for item in items_object:
            items.append(item)

        if len(items) == 0:
            return Tools.Result(False, Tools.errors('INF'))

        items_by_category = {}
        for category in categories:
            items_by_category[category['Title']] = []

        for item in items:
            for category in categories:
                if int(item['RowId']) == int(category['RowId']):
                    items_by_category[category['Title']].append(item['Title'])

        return Tools.Result(True, items_by_category)
Esempio n. 3
0
    def get_all_comments():
        item_object = item_collection.find({}, {'Comments': 1})

        comments = []
        for item in item_object:
            comments.append(item['Comments'])

        return Tools.Result(True, Tools.dumps(comments))
Esempio n. 4
0
    def get_favorite_items(user_id):

        items_object = item_collection.find({'Likes.UserId': user_id})

        favorite_items = []
        for item in items_object:
            favorite_items.append(item)

        return Tools.Result(True, Tools.dumps(favorite_items))
Esempio n. 5
0
    def get_items(row_id, user_id):

        if str(row_id) != "-1":
            items_object = item_collection.find({'RowId': row_id}, {
                'Comments': 0,
                'CategoryName': 0
            })
        else:
            items_object = item_collection.find({}, {
                'Comments': 0,
                'CategoryName': 0
            })

        items = []
        for item in items_object:
            items.append(item)

        if len(items) == 0:
            return Tools.Result(False, Tools.errors('INF'))

        items = Item._get_gallery_image_urls_for_items(items)

        for item in items:
            menu_image_id = item['MenuImageUrl']['MenuImageId']
            item.pop('MenuImageUrl')
            item[
                'MenuImageUrl'] = 'https://cafe-art-backend.liara.run/item/menu/image/{}'.format(
                    menu_image_id)

            item_image_id = item['ItemImageUrl']['ItemImageId']
            item.pop('ItemImageUrl')
            item[
                'ItemImageUrl'] = 'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item_image_id)

            if len(item['Likes']) == 0:
                item['IsLiked'] = False

            for user_id_ in item['Likes']:
                item['IsLiked'] = True if user_id_[
                    'UserId'] == user_id else False

        return Tools.Result(True, Tools.dumps(items))
Esempio n. 6
0
    def _get_favorite_items(user_id):

        items_object = item_collection.find({'Likes.UserId': user_id}, {
            'ItemImageUrl': 1,
            'RowId': 1,
            'Title': 1,
            'CategoryName': 1
        })

        favorite_items = []
        for item in items_object:
            item[
                "ItemImageUrl"] = "https://cafe-art-backend.liara.run/item/item/image/{0}".format(
                    item['ItemImageUrl']['ItemImageId'])
            favorite_items.append(item)

        return favorite_items
Esempio n. 7
0
    def get_top_items():
        items = item_collection.find({}, {
            'Comments': 1,
            'Title': 1,
            'ItemImageUrl': 1
        })

        if items is None:
            return Tools.Result(False, Tools.errors('INF'))

        items_arr = []
        for item in items:
            items_arr.append(item)

        items_rate_arr = []
        for item in items_arr:
            sum_ = 0
            total = len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                sum_ += int(comment['Rate'])

            items_rate_arr.append({
                'Title':
                item['Title'],
                'AverageRate':
                sum_ / total,
                'Image':
                'https://cafe-art-backend.liara.run/item/item/image/{}'.format(
                    item['ItemImageUrl']['ItemImageId']),
                'Total':
                total
            })

        # sort items based on average rate
        sorted_items = sorted(items_rate_arr,
                              key=itemgetter('AverageRate'),
                              reverse=True)

        upper_bound = min(5, len(items_rate_arr))

        return Tools.Result(True, Tools.dumps(sorted_items[:upper_bound]))
Esempio n. 8
0
    def get_all_unseen_comments():
        items = item_collection.find({}, {'Comments': 1, 'Title': 1})

        if items is None:
            return Tools.Result(False, 'INF')

        items_arr = []
        for item in items:
            items_arr.append(item)

        unseen_comments = []
        for item in items_arr:
            if len(item['Comments']) == 0:
                continue
            for comment in item['Comments']:
                if not comment['Seen']:
                    comment['Title'] = item['Title']
                    unseen_comments.append(comment)

        return Tools.Result(True, Tools.dumps(unseen_comments))
Esempio n. 9
0
    def comments_seen():
        items = item_collection.find({}, {'Comments': 1})

        if items is None:
            return Tools.Result(False, Tools.errors('INF'))

        items_arr = []
        for item in items:
            items_arr.append(item)

        total = 0
        seen = 0
        for item in items_arr:
            total += len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                seen += 1 if comment['Seen'] is True else 0

        response = {'Seen': seen, 'Total': total}

        return Tools.Result(True, Tools.dumps(response))
Esempio n. 10
0
    def get_average_rate(row_id):
        items = item_collection.find({'RowId': row_id}, {
            'Comments': 1,
            'Title': 1
        })

        if items is None:
            return Tools.Result(False, Tools.errors('INF'))

        items_arr = []
        for item in items:
            items_arr.append(item)

        items_rate_arr = {}
        for item in items_arr:
            sum_ = 0
            total = len(item['Comments'])
            if total == 0:
                continue
            for comment in item['Comments']:
                sum_ += int(comment['Rate'])
            items_rate_arr[item['Title']] = sum_ / total

        return Tools.Result(True, Tools.dumps(items_rate_arr))