public SynchronizePortfolioResponse SynchronizePortfolio(SynchronizePortfolioRequest request)
        {
            SynchronizePortfolioResponse response = new SynchronizePortfolioResponse
            {
                Success       = true,
                Error         = "",
                PortfolioData = new List <PortfolioData>()
            };

            List <User_Keys> userKeys = db.User_Keys.Where(k => k.UserNr == request.UserNr).ToList();

            if (userKeys.Count == 0)
            {
                response.Success = false;
                response.Error   = "User does not have any authorization keys";
                return(response);
            }

            foreach (User_Keys key in userKeys)
            {
                if (key.ExchangeType == (int)ExchangeTypeEnum.Cex)
                {
                    CexBalanceRequest cexBalanceRequest = new CexBalanceRequest
                    {
                        Key    = key.KeyValue,
                        Nonce  = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString(),
                        Secret = key.SecretValue
                    };

                    CexBalanceResponse   cexBalanceResponse = _cexManager.GetBalance(cexBalanceRequest);
                    List <User_Balances> balances           = db.User_Balances.Where(b => b.UserNr == request.UserNr && b.ExchangeType == (int)ExchangeTypeEnum.Cex).ToList();

                    if (balances.Count > 0)
                    {
                        db.User_Balances.RemoveRange(balances);
                        db.SaveChanges();
                    }

                    if (cexBalanceResponse.BTC != null)
                    {
                        User_Balances btcBalances = new User_Balances
                        {
                            UserNr       = request.UserNr,
                            CurrencyType = (int)CurrencyType.BTC,
                            ExchangeType = (int)ExchangeTypeEnum.Cex,
                            Value        = cexBalanceResponse.BTC.Available
                        };

                        db.User_Balances.Add(btcBalances);
                    }

                    if (cexBalanceResponse.ETH != null)
                    {
                        User_Balances ethBalances = new User_Balances
                        {
                            UserNr       = request.UserNr,
                            CurrencyType = (int)CurrencyType.ETH,
                            ExchangeType = (int)ExchangeTypeEnum.Cex,
                            Value        = cexBalanceResponse.ETH.Available
                        };

                        db.User_Balances.Add(ethBalances);
                    }

                    if (cexBalanceResponse.EUR != null)
                    {
                        User_Balances eurBalances = new User_Balances
                        {
                            UserNr       = request.UserNr,
                            CurrencyType = (int)CurrencyType.EUR,
                            ExchangeType = (int)ExchangeTypeEnum.Cex,
                            Value        = cexBalanceResponse.EUR.Available
                        };

                        db.User_Balances.Add(eurBalances);
                    }

                    if (cexBalanceResponse.USD != null)
                    {
                        User_Balances usdBalances = new User_Balances
                        {
                            UserNr       = request.UserNr,
                            CurrencyType = (int)CurrencyType.USD,
                            ExchangeType = (int)ExchangeTypeEnum.Cex,
                            Value        = cexBalanceResponse.USD.Available
                        };

                        db.User_Balances.Add(usdBalances);
                    }

                    db.SaveChanges();
                }
                else
                {
                    PoloniexBalanceRequest poloniexBalanceRequest = new PoloniexBalanceRequest
                    {
                        Nonce = (154264078495300 + new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds()).ToString()
                    };

                    PoloniexBalanceResponse poloniexBalanceResponse = _poloniexManager.GetBalances(poloniexBalanceRequest, key.KeyValue, key.SecretValue);

                    List <User_Balances> balances = db.User_Balances.Where(b => b.UserNr == request.UserNr && b.ExchangeType == (int)ExchangeTypeEnum.Poloniex).ToList();

                    if (balances.Count > 0)
                    {
                        db.User_Balances.RemoveRange(balances);
                        db.SaveChanges();
                    }

                    foreach (var balance in poloniexBalanceResponse.Balances)
                    {
                        if (balance.Key == "BTC")
                        {
                            db.User_Balances.Add(new User_Balances
                            {
                                UserNr       = request.UserNr,
                                CurrencyType = (int)CurrencyType.BTC,
                                ExchangeType = (int)ExchangeTypeEnum.Poloniex,
                                Value        = balance.Value
                            });
                        }
                        if (balance.Key == "ETH")
                        {
                            db.User_Balances.Add(new User_Balances
                            {
                                UserNr       = request.UserNr,
                                CurrencyType = (int)CurrencyType.ETH,
                                ExchangeType = (int)ExchangeTypeEnum.Poloniex,
                                Value        = balance.Value
                            });
                        }
                        if (balance.Key == "USD")
                        {
                            db.User_Balances.Add(new User_Balances
                            {
                                UserNr       = request.UserNr,
                                CurrencyType = (int)CurrencyType.USD,
                                ExchangeType = (int)ExchangeTypeEnum.Poloniex,
                                Value        = balance.Value
                            });
                        }
                        if (balance.Key == "EUR")
                        {
                            db.User_Balances.Add(new User_Balances
                            {
                                UserNr       = request.UserNr,
                                CurrencyType = (int)CurrencyType.EUR,
                                ExchangeType = (int)ExchangeTypeEnum.Poloniex,
                                Value        = balance.Value
                            });
                        }
                    }

                    db.SaveChanges();
                }
            }

            #region CEX Balances
            List <User_Balances> allBalances    = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Cex && b.UserNr == request.UserNr).ToList();
            List <PortfolioData> portfolioDatas = new List <PortfolioData>
            {
                new PortfolioData
                {
                    ExchangeType  = (int)ExchangeTypeEnum.Cex,
                    CurrencyDatas = new List <CurrencyData>()
                }
            };

            foreach (var balance in allBalances)
            {
                PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Cex);
                portfolioData.CurrencyDatas.Add(new CurrencyData
                {
                    Symbol = CurrencyTypeEnumToString(balance.CurrencyType),
                    Value  = balance.Value
                });
            }
            #endregion

            #region Poloniex Balances

            allBalances = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Poloniex && b.UserNr == request.UserNr).ToList();

            portfolioDatas.Add(new PortfolioData
            {
                ExchangeType  = (int)ExchangeTypeEnum.Poloniex,
                CurrencyDatas = new List <CurrencyData>()
            });

            foreach (var balance in allBalances)
            {
                PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Poloniex);
                portfolioData.CurrencyDatas.Add(new CurrencyData
                {
                    Symbol = CurrencyTypeEnumToString(balance.CurrencyType),
                    Value  = balance.Value
                });
            }

            #endregion


            response.PortfolioData = portfolioDatas;

            return(response);
        }
        public LoginResponse Login(LoginRequest request)
        {
            LoginResponse response = new LoginResponse
            {
                Success = true,
                Error   = ""
            };

            User_Users user = db.User_Users.FirstOrDefault(u => u.UserName == request.UserName && u.Password == request.Password);

            if (user == null)
            {
                response.Success = false;
                response.Error   = "Username or password is incorrect";
            }
            else
            {
                response.User = Mapper.Map <User>(user);

                #region CEX Balances
                List <User_Balances> allBalances    = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Cex && b.UserNr == response.User.UserNr).ToList();
                List <PortfolioData> portfolioDatas = null;

                if (allBalances.Count > 0)
                {
                    portfolioDatas = new List <PortfolioData>
                    {
                        new PortfolioData
                        {
                            ExchangeType  = (int)ExchangeTypeEnum.Cex,
                            CurrencyDatas = new List <CurrencyData>()
                        }
                    };

                    foreach (var balance in allBalances)
                    {
                        PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Cex);
                        portfolioData.CurrencyDatas.Add(new CurrencyData
                        {
                            Symbol = CurrencyTypeEnumToString(balance.CurrencyType),
                            Value  = balance.Value
                        });
                    }
                }

                #endregion

                #region Poloniex Balances

                allBalances = db.User_Balances.Where(b => b.ExchangeType == (int)ExchangeTypeEnum.Poloniex && b.UserNr == response.User.UserNr).ToList();

                if (allBalances.Count > 0)
                {
                    portfolioDatas.Add(new PortfolioData
                    {
                        ExchangeType  = (int)ExchangeTypeEnum.Poloniex,
                        CurrencyDatas = new List <CurrencyData>()
                    });

                    foreach (var balance in allBalances)
                    {
                        PortfolioData portfolioData = portfolioDatas.FirstOrDefault(p => p.ExchangeType == (int)ExchangeTypeEnum.Poloniex);
                        portfolioData.CurrencyDatas.Add(new CurrencyData
                        {
                            Symbol = CurrencyTypeEnumToString(balance.CurrencyType),
                            Value  = balance.Value
                        });
                    }
                }

                #endregion

                response.PortfolioDatas = portfolioDatas;
            }

            return(response);
        }