public DeleteAuthKeyResponse DeleteAuthKey(DeleteAuthKeyRequest request)
        {
            DeleteAuthKeyResponse response = new DeleteAuthKeyResponse
            {
                Success = true,
                Error   = ""
            };

            User_Keys DbKey = db.User_Keys.FirstOrDefault(k => k.UserNr == request.UserNr && k.KeyNr == request.KeyNr);

            if (DbKey == null)
            {
                response.Success = false;
                response.Error   = "No such key found";
                return(response);
            }

            try
            {
                db.User_Keys.Remove(DbKey);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.ErrorFormat($"error happend error: {e.Message} stacktrace: {e.StackTrace}");
                response.Success = false;
                response.Error   = "An error occured in user creation please check logs for more details";
            }

            return(response);
        }
        public CreateAuthKeyResponse CreateAuthKey(CreateAuthKeyRequest request)
        {
            CreateAuthKeyResponse response = new CreateAuthKeyResponse
            {
                Success = true,
                Error   = ""
            };

            User_Keys key = Mapper.Map <User_Keys>(request.AuthKey);

            User_Keys DbKey = db.User_Keys.FirstOrDefault(k => k.UserNr == key.UserNr && k.ExchangeType == key.ExchangeType);

            if (DbKey != null)
            {
                response.Success = false;
                response.Error   = "A key for that exchange already exists";
                return(response);
            }

            try
            {
                db.User_Keys.Add(key);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                _logger.ErrorFormat($"error happend error: {e.Message} stacktrace: {e.StackTrace}");
                response.Success = false;
                response.Error   = "An error occured in user creation please check logs for more details";
            }

            return(response);
        }
        public RecalculateActionsResponse RecalculateActions(RecalculateActionsRequest request)
        {
            List <Trade_Trades> allTrades = db.Trade_Trades.Where(t => t.TradeStatus != (int)TradeStatusTypeEnum.Failed && t.TradeStatus != (int)TradeStatusTypeEnum.Completed).ToList();
            Dictionary <int, List <EcoIndex> > allNewEcoIndexes = GetAllExchangeEcoIndexValues();

            foreach (Trade_Trades trade in allTrades)
            {
                List <Trade_Criteria> tradeCriterias = db.Trade_Criteria.Where(t => t.TradeNr == trade.TradeNr).ToList();
                Exchange_Pairs        exchangePair   = db.Exchange_Pairs.FirstOrDefault(t => t.PairNr == trade.ExchangePairNr);

                decimal successWeight = 0M;
                decimal totalWeight   = tradeCriterias.Sum(tc => tc.Weight);
                if (exchangePair != null)
                {
                    foreach (Trade_Criteria criteria in tradeCriterias)
                    {
                        EcoIndex criteriaIndex = allNewEcoIndexes.FirstOrDefault(i => i.Key == exchangePair.ExchangeType).Value.FirstOrDefault(e => (int)e.Id == criteria.EcoIndexType);

                        if (criteriaIndex != null)
                        {
                            if (criteria.CriteriaValueType == (int)CriteriaValueType.Above)
                            {
                                if (criteriaIndex.Value > criteria.Value)
                                {
                                    successWeight += criteria.Weight;
                                }
                            }
                            else if (criteria.CriteriaValueType == (int)CriteriaValueType.AboveOrEqual)
                            {
                                if (criteriaIndex.Value >= criteria.Value)
                                {
                                    successWeight += criteria.Weight;
                                }
                            }
                            else if (criteria.CriteriaValueType == (int)CriteriaValueType.Equal)
                            {
                                if (criteriaIndex.Value == criteria.Value)
                                {
                                    successWeight += criteria.Weight;
                                }
                            }
                            else if (criteria.CriteriaValueType == (int)CriteriaValueType.BelowOrEqual)
                            {
                                if (criteriaIndex.Value <= criteria.Value)
                                {
                                    successWeight += criteria.Weight;
                                }
                            }
                            else if (criteria.CriteriaValueType == (int)CriteriaValueType.Below)
                            {
                                if (criteriaIndex.Value < criteria.Value)
                                {
                                    successWeight += criteria.Weight;
                                }
                            }
                        }

                        if (successWeight >= totalWeight)
                        {
                            trade.TradeStatus = (int)TradeStatusTypeEnum.Valid;
                            db.SaveChanges();

                            if (exchangePair.ExchangeType == (int)ExchangeTypeEnum.Cex)
                            {
                                User_Keys key = db.User_Keys.FirstOrDefault(k => k.UserNr == trade.UserNr && k.ExchangeType == exchangePair.ExchangeType);

                                if (key != null)
                                {
                                    CexTradeRequest cexRequest = new CexTradeRequest
                                    {
                                        Amount  = trade.Value,
                                        Nonce   = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString(),
                                        Price   = 1M,
                                        Symbol1 = exchangePair.Symbol1.ToUpper(),
                                        Symbol2 = exchangePair.Symbol2.ToUpper(),
                                        Type    = trade.TradeAction == (int)TradeActionType.Buy ? "buy" : "sell",
                                        Key     = key.KeyValue,
                                        Secret  = key.SecretValue,
                                    };

                                    CexTradeResponse cexResponse = _cexManager.PerformTrade(cexRequest);

                                    if (cexResponse.Completed)
                                    {
                                        trade.TradeStatus = (int)TradeStatusTypeEnum.Completed;
                                        db.SaveChanges();
                                    }
                                    else
                                    {
                                        trade.TradeStatus = (int)TradeStatusTypeEnum.Failed;
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    trade.TradeStatus = (int)TradeStatusTypeEnum.Failed;
                                    db.SaveChanges();
                                }
                            }
                            else
                            {
                                User_Keys key = db.User_Keys.FirstOrDefault(k => k.UserNr == trade.UserNr && k.ExchangeType == exchangePair.ExchangeType);

                                if (key != null)
                                {
                                    PoloniexTradeRequest poloniexRequest = new PoloniexTradeRequest
                                    {
                                        Amount       = trade.Value,
                                        Command      = trade.TradeAction == (int)TradeActionType.Buy ? "buy" : "sell",
                                        CurrencyPair = exchangePair.Symbol1.ToUpper() + "_" + exchangePair.Symbol2.ToUpper(),
                                        Nonce        = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString(),
                                        Rate         = 1M
                                    };

                                    PoloniexTradeResponse poloniexTradeResponse = _poloniexManager.PerformTrade(poloniexRequest, key.KeyValue, key.SecretValue);

                                    if (poloniexTradeResponse.ResultingTrades?.Count > 0)
                                    {
                                        trade.TradeStatus = (int)TradeStatusTypeEnum.Completed;
                                        db.SaveChanges();
                                    }
                                    else
                                    {
                                        trade.TradeStatus = (int)TradeStatusTypeEnum.Failed;
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    trade.TradeStatus = (int)TradeStatusTypeEnum.Failed;
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }

            return(new RecalculateActionsResponse
            {
                Success = true,
                Error = ""
            });
        }