from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type
from mongoengine import Q

from cryptodataaccess import helpers
from cryptodataaccess.helpers import if_none_raise, if_none_raise_with_id


class TransactionRepository:

    def __init__(self, config, log_error):
        self.configuration = config
        self.log_error = log_error

    def fetch_transaction(self, id):
        return helpers.server_time_out_wrapper(self, self.do_fetch_transaction, id)

    def fetch_transactions(self, user_id):
        return helpers.server_time_out_wrapper(self, self.do_fetch_transactions, user_id)

    def fetch_transactions(self, user_id):
        return helpers.server_time_out_wrapper(self, self.do_fetch_transactions, user_id)

    def insert_transaction(self, user_id, volume, symbol, value, price, currency, date, source, source_id, operation):
        return helpers.server_time_out_wrapper(self, self.do_insert_transaction, user_id, volume, symbol,
                                               value, price, currency, date, source, source_id, operation)

    def update_transaction(self, id, user_id, volume, symbol, value, price, currency, date, source, source_id,
                           operation):
        return helpers.server_time_out_wrapper(self, self.do_update_transaction, id,
                                               user_id, volume, symbol, value, price, currency, date, source, source_id,
                                               operation)

    def delete_transaction(self, id, throw_if_does_not_exist=True):
        helpers.server_time_out_wrapper(self, self.do_delete_transaction, id, throw_if_does_not_exist)

    def do_delete_transaction(self, id, throw_if_does_not_exist=True):
        helpers.do_local_connect(self.configuration)
        trans = user_transaction.objects(id=id).first()
        if throw_if_does_not_exist:
            if_none_raise_with_id(id, trans)
        if trans is not None:
            trans.delete()

    def do_update_transaction(self, id, user_id, volume, symbol, value, price, currency, date, source, source_id,
                              operation):
        helpers.do_local_connect(self.configuration)
        trans = user_transaction.objects(id=id).first()
        if_none_raise_with_id(id, trans)
        trans.user_id = user_id
        trans.volume = volume
        trans.symbol = symbol
        trans.value = value
        trans.price = price
        trans.date = date
        trans.source = source
        trans.currency = currency
        trans.source_id = source_id
        trans.operation = operation
        trans.save()
        return user_transaction.objects(id=id).first()

    def do_insert_transaction(self, user_id, volume, symbol, value, price, currency, date, source, source_id,
                              operation):
        helpers.do_local_connect(self.configuration)
        trans = user_transaction()
        trans.user_id = user_id
        trans.volume = volume
        trans.symbol = symbol
        trans.value = value
        trans.price = price
        trans.date = date
        trans.currency = currency
        trans.source = source
        trans.source_id = source_id
        trans.operation = operation
        trans.save()
        return user_transaction.objects(id=trans.id).first()

    def do_fetch_transactions(self, user_id ):
        helpers.do_local_connect(self.configuration)
        return user_transaction.objects(Q(user_id=user_id))

    def do_fetch_transaction(self, id ):
        helpers.do_local_connect(self.configuration)
        return user_transaction.objects(Q(id=id))[0]

[((439, 507), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_fetch_transaction', 'id'], {}), '(self, self.do_fetch_transaction, id)\n', (470, 507), False, 'from cryptodataaccess import helpers\n'), ((567, 641), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_fetch_transactions', 'user_id'], {}), '(self, self.do_fetch_transactions, user_id)\n', (598, 641), False, 'from cryptodataaccess import helpers\n'), ((701, 775), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_fetch_transactions', 'user_id'], {}), '(self, self.do_fetch_transactions, user_id)\n', (732, 775), False, 'from cryptodataaccess import helpers\n'), ((911, 1065), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_insert_transaction', 'user_id', 'volume', 'symbol', 'value', 'price', 'currency', 'date', 'source', 'source_id', 'operation'], {}), '(self, self.do_insert_transaction, user_id,\n    volume, symbol, value, price, currency, date, source, source_id, operation)\n', (942, 1065), False, 'from cryptodataaccess import helpers\n'), ((1275, 1437), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_update_transaction', 'id', 'user_id', 'volume', 'symbol', 'value', 'price', 'currency', 'date', 'source', 'source_id', 'operation'], {}), '(self, self.do_update_transaction, id,\n    user_id, volume, symbol, value, price, currency, date, source,\n    source_id, operation)\n', (1306, 1437), False, 'from cryptodataaccess import helpers\n'), ((1601, 1699), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', (['self', 'self.do_delete_transaction', 'id', 'throw_if_does_not_exist'], {}), '(self, self.do_delete_transaction, id,\n    throw_if_does_not_exist)\n', (1632, 1699), False, 'from cryptodataaccess import helpers\n'), ((1776, 1820), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', (['self.configuration'], {}), '(self.configuration)\n', (1800, 1820), False, 'from cryptodataaccess import helpers\n'), ((2180, 2224), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', (['self.configuration'], {}), '(self.configuration)\n', (2204, 2224), False, 'from cryptodataaccess import helpers\n'), ((2289, 2321), 'cryptodataaccess.helpers.if_none_raise_with_id', 'if_none_raise_with_id', (['id', 'trans'], {}), '(id, trans)\n', (2310, 2321), False, 'from cryptodataaccess.helpers import if_none_raise, if_none_raise_with_id\n'), ((2869, 2913), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', (['self.configuration'], {}), '(self.configuration)\n', (2893, 2913), False, 'from cryptodataaccess import helpers\n'), ((2930, 2948), 'cryptomodel.cryptostore.user_transaction', 'user_transaction', ([], {}), '()\n', (2946, 2948), False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((3397, 3441), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', (['self.configuration'], {}), '(self.configuration)\n', (3421, 3441), False, 'from cryptodataaccess import helpers\n'), ((3552, 3596), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', (['self.configuration'], {}), '(self.configuration)\n', (3576, 3596), False, 'from cryptodataaccess import helpers\n'), ((1925, 1957), 'cryptodataaccess.helpers.if_none_raise_with_id', 'if_none_raise_with_id', (['id', 'trans'], {}), '(id, trans)\n', (1946, 1957), False, 'from cryptodataaccess.helpers import if_none_raise, if_none_raise_with_id\n'), ((3482, 3500), 'mongoengine.Q', 'Q', ([], {'user_id': 'user_id'}), '(user_id=user_id)\n', (3483, 3500), False, 'from mongoengine import Q\n'), ((1837, 1868), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', ([], {'id': 'id'}), '(id=id)\n', (1861, 1868), False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((2241, 2272), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', ([], {'id': 'id'}), '(id=id)\n', (2265, 2272), False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((2668, 2699), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', ([], {'id': 'id'}), '(id=id)\n', (2692, 2699), False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((3295, 3332), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', ([], {'id': 'trans.id'}), '(id=trans.id)\n', (3319, 3332), False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((3637, 3645), 'mongoengine.Q', 'Q', ([], {'id': 'id'}), '(id=id)\n', (3638, 3645), False, 'from mongoengine import Q\n')]