Ejemplo n.º 1
0
        private void syncRates()
        {
            using (var context = new InvestingContext())
            {
                var balancesDates = context.Balances.Select(o => o.DateTimeStamp).ToArray();
                var fromDate      = context.Flows.Min(o => o.DateTimeStamp).AddMonths(-1);
                var toDate        = DateTime.Now;

                foreach (var type in Enum.GetValues(typeof(RateType)))
                {
                    var rateType = (RateType)type;
                    var data     = RatesDownloader.GetValues(rateType, fromDate, toDate);

                    var existingRateDates = context.Rates.Where(o => o.Type == rateType)
                                            .Select(o => o.DateTimeStamp);
                    var datesToAdd = balancesDates.Except(existingRateDates);

                    foreach (var d in datesToAdd.Where(o => data.Keys.Contains(o)))
                    {
                        context.Rates.Add(new Rate()
                        {
                            Type          = rateType,
                            DateTimeStamp = d,
                            Value         = data[d]
                        });
                    }
                    context.SaveChanges();
                }
            }
        }
Ejemplo n.º 2
0
 public InvestingIntegrationEventService(IEventBus eventBus, InvestingContext investingContext,
                                         Func <DbConnection, IIntegrationEventLogService> integrationEventLogServiceFactory)
 {
     _investingContext = investingContext ?? throw new ArgumentNullException(nameof(investingContext));
     _integrationEventLogServiceFactory = integrationEventLogServiceFactory ?? throw new ArgumentNullException(nameof(integrationEventLogServiceFactory));
     _eventBus        = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
     _eventLogService = _integrationEventLogServiceFactory(_investingContext.Database.GetDbConnection());
 }
Ejemplo n.º 3
0
 private GeneralInvestingInfo getGeneralInfo()
 {
     using (var context = new InvestingContext())
     {
         var data     = context.Balances.ToArray();
         var flows    = context.Flows.ToArray();
         var ratesUSD = context.Rates.Where(o => o.Type == RateType.USDollar).ToArray();
         return(new GeneralInvestingInfo(data, flows, ratesUSD));
     }
 }
Ejemplo n.º 4
0
        public void LoadAllInvestments()
        {
            List <Investment> investments = new List <Investment>();

            using (var db = new InvestingContext()) {
                investments = db.Investments.Include("Company").ToList();
            }

            foreach (Investment i in investments)
            {
                Company c = FetchCompanyInfo(i.Company.Symbol);
                i.CurrentPrice = c.CurrentPrice;
                Investments.Add(i);
            }
        }
        public ActionResult Login(LoginModel loginInfo)
        {
            ViewBag.Title = "Login";

            using (var context = new InvestingContext())
            {
                var user = context.Users.SingleOrDefault(o => o.Login == loginInfo.Login);
                if (user == null)
                {
                    ModelState.AddModelError("Status", string.Format("User with login {0} not found.", loginInfo.Login));
                }
                else
                {
                    if ((DateTime.Now - user.LastAttemptTime) > _attemptsInterval)
                    {
                        user.LoginAttempts = 0;
                    }
                    user.LastAttemptTime = DateTime.Now;
                    user.LoginAttempts++;
                    context.SaveChanges();
                    if (user.LoginAttempts >= MAX_ATTEMPTS)
                    {
                        ModelState.AddModelError("Status", string.Format("Too many attempts for user with login {0}. Please, wait.", loginInfo.Login));
                    }

                    if (ModelState.IsValid)
                    {
                        var hash = sha256(loginInfo.Password ?? string.Empty);
                        if (hash == user.Password)
                        {
                            FormsAuthentication.SignOut();
                            FormsAuthentication.SetAuthCookie(loginInfo.Login, loginInfo.Remember);
                            return(RedirectToAction("Index"));
                        }
                        else
                        {
                            ModelState.AddModelError("Status", "Invalid password.");
                        }
                    }
                }
            }

            return(View(loginInfo));
        }
        public ActionResult Index()
        {
            ViewBag.Title = "Account";

            AccountData accountInfo;

            using (var context = new InvestingContext())
            {
                var user = context.Users.FirstOrDefault(o => o.Login == HttpContext.User.Identity.Name);
                if (user == null)
                {
                    Logout();
                }

                accountInfo = new AccountData(user,
                                              context.Balances,
                                              context.Flows.Include(o => o.User));

                return(View(accountInfo));
            }
        }
Ejemplo n.º 7
0
        public ActionResult TakeBalance(string key, string date, string value)
        {
            if (!Request.IsLocal)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed));
            }
            try
            {
                if (key != System.Configuration.ConfigurationManager.AppSettings["api_key"])
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.MethodNotAllowed));
                }
                var d = DateTime.Parse(date);
                var v = double.Parse(value);

                using (var context = new InvestingContext())
                {
                    if (context.Balances.Any(o => o.DateTimeStamp == d))
                    {
                        new HttpStatusCodeResult(HttpStatusCode.NotAcceptable);
                    }
                    var row = new BalancesRow()
                    {
                        Balance       = v,
                        DateTimeStamp = d
                    };
                    context.Balances.Add(row);
                    context.SaveChanges();
                }

                syncRates();
            }
            catch
            {
                return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError));
            }

            return(new HttpStatusCodeResult(HttpStatusCode.OK));
        }
Ejemplo n.º 8
0
        public void CreateCrossInvestments()
        {
            Investments.Clear();
            foreach (Company c in Companies)
            {
                Investment i = new Investment()
                {
                    Company      = c,
                    BoughtPrice  = c.CurrentPrice,
                    CurrentPrice = c.CurrentPrice
                };
                Investments.Add(i);
            }

            using (var db = new InvestingContext()) {
                Investments.ToList().ForEach(i => {
                    if (!db.Investments.Include("Company").Any(x => x.Company.Symbol == i.Company.Symbol))
                    {
                        db.Investments.Add(i);
                    }
                });
                db.SaveChanges();
            }
        }
Ejemplo n.º 9
0
 public RoundtripsController(InvestingContext context, IRoundtripRepository roundtripRepository)
 {
     _context = context;
     this._roundtripRepository = roundtripRepository ?? throw new ArgumentNullException(nameof(roundtripRepository));
 }
Ejemplo n.º 10
0
 public InvestmentRepository(InvestingContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
Ejemplo n.º 11
0
 public RequestManager(InvestingContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
Ejemplo n.º 12
0
 public InvestmentsController(InvestingContext context, IInvestmentRepository investmentRepository, IMediator mediator)
 {
     _context = context;
     _investmentRepository = investmentRepository ?? throw new ArgumentNullException(nameof(investmentRepository));
     _mediator             = mediator ?? throw new ArgumentNullException(nameof(mediator));
 }
Ejemplo n.º 13
0
 public FundsController(InvestingContext context, IFundRepository fundRepository, IMediator mediator)
 {
     _context        = context ?? throw new ArgumentNullException(nameof(context));
     _fundRepository = fundRepository ?? throw new ArgumentNullException(nameof(fundRepository));
     _mediator       = mediator ?? throw new ArgumentNullException(nameof(mediator));
 }