Example #1
0
    def unlike_item(item_id, user_id):
        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        # make sure user did not liked the item before
        liked_before = item_collection.find_one({
            '_id': ObjectId(item_id),
            'Likes.UserId': user_id
        }) is not None

        if not liked_before:
            return Tools.Result(False, Tools.errors('INF'))

        # update the likes
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$pull': {
                'Likes': {
                    'UserId': user_id
                }
            },
            '$inc': {
                'LikesCount': -1
            }
        })

        return Tools.Result(True, 'd')
Example #2
0
    def get_item(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)}, {
            'RowId': 1,
            'CategoryName': 1,
            'Title': 1,
            'Description': 1,
            'MenuImageUrl': 1,
            'ItemImageUrl': 1,
            'Likes': 1,
            'Price': 1
        })

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

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

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

        gallery_images_urls = Item._get_gallery_image_urls(item_id)

        item_object['GalleryUrls'] = gallery_images_urls

        return Tools.Result(True, Tools.dumps(item_object))
Example #3
0
    def comment_on_item(item_id, user_id, comment, rate):

        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        # # make sure user did not comment on the item before
        # commented_before = item_collection.find_one({'_id': ObjectId(item_id), 'Comments.UserId': user_id}) is not None

        # if commented_before:
        #     return Tools.Result(False, Tools.errors('IAE'))

        # update the comments
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$push': {
                'Comments': {
                    'CommentId': ObjectId(),
                    'UserId': user_id,
                    'Comment': comment,
                    'Rate': rate,
                    'Seen': False,
                    'Created_at': datetime.now()
                }
            }
        })

        return Tools.Result(True, 'd')
Example #4
0
    def unlike_image_gallery(item_id, user_id, gallery_image_id):
        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        gallery = item_collection.find_one(
            {
                '_id': ObjectId(item_id),
                'Gallery': {
                    '$elemMatch': {
                        'Id': ObjectId(gallery_image_id)
                    }
                }
            }, {
                '_id': 0,
                'Gallery': 1
            })

        found = False
        for images in gallery['Gallery']:
            if str(images['Id']) == gallery_image_id:
                for like in images['Likes']:
                    if like['UserId'] == user_id:
                        found = True

        if not found:
            return Tools.Result(False, Tools.errors('NA'))

        # update the likes
        item_collection.update_one({'_id': ObjectId(item_id)}, {
            '$dec': {
                'Gallery.$[elem].LikesCount': -1
            },
            '$pull': {
                'Gallery.$[elem].Likes': {
                    'UserId': user_id
                }
            }
        },
                                   array_filters=[{
                                       'elem.Id':
                                       ObjectId(gallery_image_id)
                                   }])

        return Tools.Result(True, 'd')
Example #5
0
    def get_item_menu_image(image_id):
        item_object = item_collection.find_one(
            {'MenuImageUrl.MenuImageId': image_id}, {'MenuImageUrl': 1})

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

        return item_object['MenuImageUrl']['MenuImage']
Example #6
0
    def get_gallery_images(item_id):
        item = item_collection.find_one({'_id': ObjectId(item_id)},
                                        {'Gallery': 1})

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

        return Tools.Result(True, Tools.dumps(item['Gallery']))
Example #7
0
    def get_comments_on_item(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)},
                                               {'Comments': 1})

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

        comments = item_object['Comments']

        return Tools.Result(True, Tools.dumps(comments))
Example #8
0
    def delete_item(item_id):
        valid = item_collection.find_one({'_id':
                                          ObjectId(item_id)}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        item_collection.delete_one({'_id': ObjectId(item_id)})

        return Tools.Result(True, 'd')
Example #9
0
    def get_gallery_image(gallery_image_id):
        item = item_collection.find_one({'Gallery.Id': gallery_image_id},
                                        {'Gallery': 1})

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

        response_gallery_image = ""
        for gallery_image in item['Gallery']:
            if gallery_image['Id'] == gallery_image_id:
                response_gallery_image = gallery_image['ImageUrl']

        return response_gallery_image
Example #10
0
    def _get_gallery_image_urls(item_id):
        item = item_collection.find_one({'_id': ObjectId(item_id)},
                                        {'Gallery': 1})

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

        gallery_ids = []
        for gallery in item['Gallery']:
            gallery_ids.append(
                "https://cafe-art-backend.liara.run/item/gallery/{}".format(
                    gallery['Id']))

        return gallery_ids
Example #11
0
    def delete_image_from_gallery(item_id, gallery_image_id):
        item = item_collection.find_one({'_id': ObjectId(item_id)}, {'_id': 1})

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

        item_collection.update_one(
            {'_id': ObjectId(item_id)},
            {'$pull': {
                'Gallery': {
                    'Id': gallery_image_id
                }
            }})

        return Tools.Result(True, 'd')
Example #12
0
    def modify_item(item_id,
                    row_id=None,
                    category_name=None,
                    title=None,
                    description=None,
                    price=None,
                    menu_image_url=None,
                    item_image_url=None):

        # make sure at least on attribute is not null
        if row_id is None and category_name is None and title is None and price is None and menu_image_url is None and item_image_url is None and description is None:
            return Tools.Result(False, Tools.errors('NA'))

        if (row_id is None
                and category_name is not None) or (row_id is not None
                                                   and category_name is None):
            return Tools.Result(False, Tools.errors('NA'))

        valid = item_collection.find_one({'_id': ObjectId(item_id)},
                                         {'_id': 1}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        updating_values = {}
        if title is not None:
            updating_values['Title'] = title
        if row_id is not None:
            updating_values['RowId'] = str(row_id)
            updating_values['CategoryName'] = category_name
        if price is not None:
            updating_values['Price'] = price
        if menu_image_url is not None:
            updating_values['MenuImageUrl.MenuImage'] = menu_image_url
        if item_image_url is not None:
            updating_values['ItemImageUrl.ItemImage'] = item_image_url
        if description is not None:
            updating_values['Description'] = description

        item_collection.update_one({'_id': ObjectId(item_id)},
                                   {'$set': {
                                       **updating_values
                                   }})

        return Tools.Result(True, 'd')
Example #13
0
    def get_rate_distribution(item_id):
        item_object = item_collection.find_one({'_id': ObjectId(item_id)},
                                               {'Comments': 1})

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

        comments = item_object['Comments']

        total = len(comments)
        rates = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0}

        for comment in comments:
            rates[str(comment['Rate'])] += 1

        return Tools.Result(True, Tools.dumps({
            'Total': total,
            'Rates': rates
        }))
Example #14
0
    def admin_saw_comment(comment_id):

        valid = item_collection.find_one(
            {'Comments.CommentId': ObjectId(comment_id)},
            {'_id': 1}) is not None

        if not valid:
            return Tools.Result(False, Tools.errors('INF'))

        item_collection.update_one(
            {'Comments.CommentId': ObjectId(comment_id)},
            {'$set': {
                'Comments.$[elem].Seen': True
            }},
            array_filters=[{
                'elem.CommentId': ObjectId(comment_id)
            }])

        return Tools.Result(True, 'd')