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]

[((15, 15, 15, 83), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(15, 47, 15, 51): 'self', (15, 53, 15, 78): 'self.do_fetch_transaction', (15, 80, 15, 82): 'id'}, {}), '(self, self.do_fetch_transaction, id)', False, 'from cryptodataaccess import helpers\n'), ((18, 15, 18, 89), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(18, 47, 18, 51): 'self', (18, 53, 18, 79): 'self.do_fetch_transactions', (18, 81, 18, 88): 'user_id'}, {}), '(self, self.do_fetch_transactions, user_id)', False, 'from cryptodataaccess import helpers\n'), ((21, 15, 21, 89), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(21, 47, 21, 51): 'self', (21, 53, 21, 79): 'self.do_fetch_transactions', (21, 81, 21, 88): 'user_id'}, {}), '(self, self.do_fetch_transactions, user_id)', False, 'from cryptodataaccess import helpers\n'), ((24, 15, 25, 106), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(24, 47, 24, 51): 'self', (24, 53, 24, 79): 'self.do_insert_transaction', (24, 81, 24, 88): 'user_id', (24, 90, 24, 96): 'volume', (24, 98, 24, 104): 'symbol', (25, 47, 25, 52): 'value', (25, 54, 25, 59): 'price', (25, 61, 25, 69): 'currency', (25, 71, 25, 75): 'date', (25, 77, 25, 83): 'source', (25, 85, 25, 94): 'source_id', (25, 96, 25, 105): 'operation'}, {}), '(self, self.do_insert_transaction, user_id,\n    volume, symbol, value, price, currency, date, source, source_id, operation)', False, 'from cryptodataaccess import helpers\n'), ((29, 15, 31, 57), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(29, 47, 29, 51): 'self', (29, 53, 29, 79): 'self.do_update_transaction', (29, 81, 29, 83): 'id', (30, 47, 30, 54): 'user_id', (30, 56, 30, 62): 'volume', (30, 64, 30, 70): 'symbol', (30, 72, 30, 77): 'value', (30, 79, 30, 84): 'price', (30, 86, 30, 94): 'currency', (30, 96, 30, 100): 'date', (30, 102, 30, 108): 'source', (30, 110, 30, 119): 'source_id', (31, 47, 31, 56): 'operation'}, {}), '(self, self.do_update_transaction, id,\n    user_id, volume, symbol, value, price, currency, date, source,\n    source_id, operation)', False, 'from cryptodataaccess import helpers\n'), ((34, 8, 34, 102), 'cryptodataaccess.helpers.server_time_out_wrapper', 'helpers.server_time_out_wrapper', ({(34, 40, 34, 44): 'self', (34, 46, 34, 72): 'self.do_delete_transaction', (34, 74, 34, 76): 'id', (34, 78, 34, 101): 'throw_if_does_not_exist'}, {}), '(self, self.do_delete_transaction, id,\n    throw_if_does_not_exist)', False, 'from cryptodataaccess import helpers\n'), ((37, 8, 37, 52), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', ({(37, 33, 37, 51): 'self.configuration'}, {}), '(self.configuration)', False, 'from cryptodataaccess import helpers\n'), ((46, 8, 46, 52), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', ({(46, 33, 46, 51): 'self.configuration'}, {}), '(self.configuration)', False, 'from cryptodataaccess import helpers\n'), ((48, 8, 48, 40), 'cryptodataaccess.helpers.if_none_raise_with_id', 'if_none_raise_with_id', ({(48, 30, 48, 32): 'id', (48, 34, 48, 39): 'trans'}, {}), '(id, trans)', False, 'from cryptodataaccess.helpers import if_none_raise, if_none_raise_with_id\n'), ((64, 8, 64, 52), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', ({(64, 33, 64, 51): 'self.configuration'}, {}), '(self.configuration)', False, 'from cryptodataaccess import helpers\n'), ((65, 16, 65, 34), 'cryptomodel.cryptostore.user_transaction', 'user_transaction', ({}, {}), '()', False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((80, 8, 80, 52), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', ({(80, 33, 80, 51): 'self.configuration'}, {}), '(self.configuration)', False, 'from cryptodataaccess import helpers\n'), ((84, 8, 84, 52), 'cryptodataaccess.helpers.do_local_connect', 'helpers.do_local_connect', ({(84, 33, 84, 51): 'self.configuration'}, {}), '(self.configuration)', False, 'from cryptodataaccess import helpers\n'), ((40, 12, 40, 44), 'cryptodataaccess.helpers.if_none_raise_with_id', 'if_none_raise_with_id', ({(40, 34, 40, 36): 'id', (40, 38, 40, 43): 'trans'}, {}), '(id, trans)', False, 'from cryptodataaccess.helpers import if_none_raise, if_none_raise_with_id\n'), ((81, 40, 81, 58), 'mongoengine.Q', 'Q', (), '', False, 'from mongoengine import Q\n'), ((38, 16, 38, 47), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', (), '', False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((47, 16, 47, 47), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', (), '', False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((60, 15, 60, 46), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', (), '', False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((77, 15, 77, 52), 'cryptomodel.cryptostore.user_transaction.objects', 'user_transaction.objects', (), '', False, 'from cryptomodel.cryptostore import user_notification, user_channel, user_transaction, operation_type\n'), ((85, 40, 85, 48), 'mongoengine.Q', 'Q', (), '', False, 'from mongoengine import Q\n')]