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(); } } }
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 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)); }
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(); } }