public static string SetFavourite(string MoneyChangerName) { string changes = ""; if (HttpContext.Current.Session["Username"] != null) { using (cz2006anythingEntities model = new cz2006anythingEntities()) { string username = HttpContext.Current.Session["Username"].ToString(); var thisFavourite = model.Favourites.Where(z => z.Username == username && z.MoneyChanger.Name == MoneyChangerName).FirstOrDefault(); if (thisFavourite == null) { Favourite favourite = new Favourite(); favourite.MoneyChanger = model.MoneyChangers.Where(z => z.Name == MoneyChangerName).First(); favourite.Username = username; model.Favourites.Add(favourite); changes = "Added"; } else { model.Favourites.Remove(thisFavourite); changes = "Deleted"; } model.SaveChanges(); } } return(changes); }
public ActionResult Register(string Username, string Password, int MobileNumber) { using (cz2006anythingEntities myEntities = new cz2006anythingEntities()) { string message = "Success"; var username = myEntities.Users.Where(z => z.Username == Username).FirstOrDefault(); var mobilenumber = myEntities.Users.Where(z => z.MobileNumber == MobileNumber).FirstOrDefault(); if (username == null && mobilenumber == null) { User user = new User(); user.Username = Username; user.Password = Password; user.MobileNumber = MobileNumber; myEntities.Users.Add(user); myEntities.SaveChanges(); } else if (username != null) { message = "Username"; } else if (mobilenumber != null) { message = "Mobile"; } return(Json(message , JsonRequestBehavior.AllowGet)); } }
public static void UpdateExchangeRates(string moneychanger_name, string currency_code, float?exchange_rate_buy, float?exchange_rate_sell, string last_update_buy, string last_update_sell) { using (cz2006anythingEntities model = new cz2006anythingEntities()) { var selling = model.ExchangeRates.Where(z => z.MoneyChanger.Name == moneychanger_name && z.Currency.Name == currency_code && z.Currency1.Name == "SGD").FirstOrDefault(); if (exchange_rate_sell != null && exchange_rate_sell != 0) { if (selling == null) { selling = new ExchangeRate(); selling.MoneyChanger = model.MoneyChangers.Where(z => z.Name == moneychanger_name).FirstOrDefault(); selling.Currency = model.Currencies.Where(z => z.Name == currency_code).FirstOrDefault(); selling.Currency1 = model.Currencies.Where(z => z.Name == "SGD").FirstOrDefault(); selling.Rate = (float)exchange_rate_sell; selling.LastUpdated = CalculationController.SetDate(last_update_sell); model.ExchangeRates.Add(selling); } else { selling.Rate = (float)exchange_rate_sell; selling.LastUpdated = CalculationController.SetDate(last_update_sell); } } else { if (selling != null) { model.ExchangeRates.Remove(selling); } } var buying = model.ExchangeRates.Where(z => z.MoneyChanger.Name == moneychanger_name && z.Currency1.Name == currency_code && z.Currency.Name == "SGD").FirstOrDefault(); if (exchange_rate_buy != null && exchange_rate_buy != 0) { exchange_rate_buy = 1 / exchange_rate_buy; if (buying == null) { buying = new ExchangeRate(); buying.MoneyChanger = model.MoneyChangers.Where(z => z.Name == moneychanger_name).FirstOrDefault(); buying.Currency1 = model.Currencies.Where(z => z.Name == currency_code).FirstOrDefault(); buying.Currency = model.Currencies.Where(z => z.Name == "SGD").FirstOrDefault(); buying.Rate = (float)exchange_rate_buy; buying.LastUpdated = CalculationController.SetDate(last_update_buy); model.ExchangeRates.Add(buying); } else { buying.Rate = (float)exchange_rate_buy; buying.LastUpdated = CalculationController.SetDate(last_update_buy); } } else { if (buying != null) { model.ExchangeRates.Remove(buying); } } model.SaveChanges(); } }
public static void UpdateMoneyChanger(string Name, string Address, string Img, string OpeningHours, int?Tel_No) { using (cz2006anythingEntities model = new cz2006anythingEntities()) { var thisMC = model.MoneyChangers.Where(z => z.Name == Name).FirstOrDefault(); if (thisMC == null) { thisMC = new MoneyChanger(); } var arr = Address.Split(' '); thisMC.Name = Name; if (arr.Count() > 0) { string PostalCode = arr[arr.Count() - 1]; thisMC.Location = Address.Replace(", " + PostalCode, ""); thisMC.PostalCode = PostalCode; } thisMC.OpeningHours = OpeningHours; thisMC.Photo = Img; thisMC.ContactNumber = Tel_No; if (model.MoneyChangers.Where(z => z.Name == thisMC.Name).FirstOrDefault() == null) { model.MoneyChangers.Add(thisMC); } model.SaveChanges(); } }
public ActionResult Details(string MoneyChangerName) { using (cz2006anythingEntities model = new cz2006anythingEntities()) { var MoneyChanger = model.MoneyChangers.Where(z => z.Name.Replace(".", "").Contains(MoneyChangerName)).OrderBy(z => z.Name.Length).FirstOrDefault(); if (MoneyChanger != null) { var x = MoneyChanger.ExchangeRates; var y = MoneyChanger.Favourites; } return(View(MoneyChanger)); } }
public async System.Threading.Tasks.Task <ActionResult> Index() { if (Session["Locked"] == null) { Session["Locked"] = true; } else if ((bool)Session["Locked"] == false) { await ApiController.SetRates(); } using (cz2006anythingEntities model = new cz2006anythingEntities()) { return(View(model.Currencies.ToList())); } }
public ActionResult Login(string Username, string Password) { using (cz2006anythingEntities myEntities = new cz2006anythingEntities()) { string message = "Success"; var user = myEntities.Users.Where(z => z.Username == Username && z.Password == Password).FirstOrDefault(); if (user == null) { message = "Invalid Login Credentials"; } return(Json(message , JsonRequestBehavior.AllowGet)); } }
public static object Filter(string Search, string ExchangeFrom, string ExchangeTo, string SortBy, bool IsFavourite) { string username = HttpContext.Current.Session["Username"] == null? "" : HttpContext.Current.Session["Username"].ToString(); using (cz2006anythingEntities model = new cz2006anythingEntities()) { var moneyChangers = model.ExchangeRates.Where(z => z.ExchangeFromId == model.Currencies.Where(y => y.Name == ExchangeFrom).FirstOrDefault().Id && z.ExchangeToId == model.Currencies.Where(y => y.Name == ExchangeTo).FirstOrDefault().Id); var exchangeRates = moneyChangers.Where(z => z.MoneyChanger.Name.Contains(Search) || z.MoneyChanger.Location.Contains(Search)); if (IsFavourite) { exchangeRates = exchangeRates.Where(z => z.MoneyChanger.Favourites.Where(a => a.Username == username).FirstOrDefault() != null); } if (SortBy == "Best") { exchangeRates = exchangeRates.OrderByDescending(z => z.Rate); } else if (SortBy == "Lowest") { exchangeRates = exchangeRates.OrderBy(z => z.Rate); } var userFavourites = model.Favourites.Where(a => a.Username == username); var x = exchangeRates.Take(10).Select(z => new { z.Rate, z.MoneyChanger.Name, z.MoneyChanger.Location, z.LastUpdated }) .AsEnumerable().Select(z => new { z.Rate, z.Name, z.Location, HasFavourite = userFavourites.Where(a => a.MoneyChanger.Name == z.Name).AsEnumerable().FirstOrDefault() != null ? true : false, LastUpdated = CalculationController.CalculateDate(z.LastUpdated), }).ToList(); var zxc = new { ExchangeRates = x, MoneyChangers = moneyChangers.Count() }; return(zxc); } }
public static async System.Threading.Tasks.Task <Graph> GetGraph(string ExchangeFrom, string ExchangeTo) { Graph historicalRates = new Graph(); if (ExchangeFrom != ExchangeTo) { int numOfDays = 30; historicalRates.Title = ExchangeFrom + " To " + ExchangeTo; historicalRates.ShortDate = new List <string>(); historicalRates.Amount = new List <double>(); historicalRates.RegressionY = new List <double>(); DateTime dateNow = DateTime.Now.AddDays(-1); DateTime historicalDate = dateNow.AddDays(-numOfDays); IQueryable <HistoricalRate> storedHistoricalRates; using (cz2006anythingEntities model = new cz2006anythingEntities()) { model.HistoricalRates.RemoveRange(model.HistoricalRates.Where(z => z.Date < historicalDate)); var exchangeFromCurrId = model.Currencies.Where(x => x.Name == ExchangeFrom).FirstOrDefault().Id; var exchangeToCurrId = model.Currencies.Where(x => x.Name == ExchangeTo).FirstOrDefault().Id; storedHistoricalRates = model.HistoricalRates.Where(z => z.ExchangeFromId == exchangeFromCurrId && z.ExchangeToId == exchangeToCurrId); DateTime latestStoredHistoricalRateDate = new DateTime(); if (storedHistoricalRates != null && storedHistoricalRates.FirstOrDefault() != null) { latestStoredHistoricalRateDate = storedHistoricalRates.OrderByDescending(z => z.Date).FirstOrDefault().Date; } if (!(storedHistoricalRates.FirstOrDefault() == null || latestStoredHistoricalRateDate < dateNow.AddDays(-numOfDays))) { TimeSpan dateDiff = dateNow.Subtract(latestStoredHistoricalRateDate); numOfDays = dateDiff.Days; historicalDate = dateNow.AddDays(-numOfDays); } for (int i = 1; i <= numOfDays; i++) { DateTime thisDate = historicalDate.AddDays(i); MarketRate marketRate = await ApiController.GetHistoryRateAsync(thisDate); if (marketRate.success == true) { double exchangeFrom = marketRate.rates.Where(z => z.Key == ExchangeFrom).FirstOrDefault().Value; double exchangeTo = marketRate.rates.Where(z => z.Key == ExchangeTo).FirstOrDefault().Value; HistoricalRate newHistoricalRate = new HistoricalRate(); newHistoricalRate.ExchangeFromId = exchangeFromCurrId; newHistoricalRate.ExchangeToId = exchangeToCurrId; newHistoricalRate.Rate = CalculationController.ConvertCurrency(1, exchangeFrom, exchangeTo); newHistoricalRate.Date = thisDate; model.HistoricalRates.Add(newHistoricalRate); } } model.SaveChanges(); float exchangeFromToday = ApiController.rates.Where(z => z.Key == ExchangeFrom).FirstOrDefault().Value; float exchangeToToday = ApiController.rates.Where(z => z.Key == ExchangeTo).FirstOrDefault().Value; double baseRate = CalculationController.ConvertCurrency(1, exchangeFromToday, exchangeToToday); List <int> RegressionX = new List <int>(); int n = storedHistoricalRates.Count() + 1; for (int i = 0; i < n; i++) { RegressionX.Add(i); } int sumX = RegressionX.Sum(); double sumY = 0; List <double> xY = new List <double>(); List <double> squareX = new List <double>(); for (int i = 0; i < n - 1; i++) { squareX.Add(RegressionX[i] * RegressionX[i]); double y = storedHistoricalRates.OrderByDescending(z => z.Date).Skip(n - i - 2).FirstOrDefault().Rate; sumY += y; xY.Add(RegressionX[i] * y); } squareX.Add(RegressionX[n - 1] * RegressionX[n - 1]); sumY += baseRate; xY.Add(RegressionX[n - 1] * baseRate); double b = (n * xY.Sum() - sumX * sumY) / (n * squareX.Sum() - sumX * sumX); double a = (sumY / n) - b * (sumX / n); int counter = 0; foreach (var x in storedHistoricalRates) { historicalRates.ShortDate.Add(x.Date.ToString("dd MMM")); historicalRates.Amount.Add(x.Rate); historicalRates.RegressionY.Add(a + b * (counter)); counter++; } historicalRates.ShortDate.Add(DateTime.Now.ToString("dd MMM")); historicalRates.Amount.Add(baseRate); historicalRates.RegressionY.Add(a + b * (counter)); } } return(historicalRates); }