public async Task <IActionResult> New(int id, [Bind("id,itemName,alertType,lowStockThreshold")] StockAlert stockAlert) { if (id != stockAlert.id) { return(NotFound()); } if (ModelState.IsValid) { try { // Create new alert User user = HttpContext.Items["user"] as User; var qStock = from s in _context.Stock where s.itemName == stockAlert.itemName && s.userId == user.id select s; Stock stock = await qStock.SingleAsync(); stockAlert.stock = stock; stockAlert.active = true; stockAlert.user = user; stockAlert.userId = user.id; //Trigger alert if necessary if (stockAlert.alertType == StockAlert.AlertType.outOfStock && stock.count == 0) { stockAlert.triggered = true; } else if (stockAlert.alertType == StockAlert.AlertType.lowStock && stock.count < stockAlert.lowStockThreshold) { stockAlert.triggered = true; } else { stockAlert.triggered = false; } _context.Add(stockAlert); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StockAlertExists(stockAlert.id)) { return(NotFound()); } else { throw; } } } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> updateAlertStatus(int id, string status) { User user = HttpContext.Items["user"] as User; bool success = false; var qAlert = from a in _context.StockAlert where a.id == id && a.userId == user.id select a; StockAlert alert = await qAlert.SingleAsync(); try { alert.active = (status == "On"); if (alert.active) // If set to active, see if it should be triggered { // Get stock var qStock = from s in _context.Stock where s.id == alert.stockId && s.userId == user.id select s; Stock stock = await qStock.SingleAsync(); if (alert.alertType == StockAlert.AlertType.outOfStock && stock.count == 0) { alert.triggered = true; } else if (stock.count <= alert.lowStockThreshold) { alert.triggered = true; } } else // Make sure alert is no longer triggered if inactive { alert.triggered = false; } _context.SaveChanges(); success = true; } catch (DbUpdateConcurrencyException) { if (!StockAlertExists(alert.id)) { return(NotFound()); } else { throw; } } return(Json( new { success = success, } )); }
public async Task <IActionResult> Edit(int id, int newType, int th) { // Get alert User user = HttpContext.Items["user"] as User; var qAlert = from a in _context.StockAlert where a.id == id && a.userId == user.id select a; StockAlert alert = await qAlert.SingleAsync(); // Get stock to check for alert trigger condition var qStock = from s in _context.Stock where s.id == alert.stockId select s; Stock stock = await qStock.SingleAsync(); try { // Update alert alert.alertType = (StockAlert.AlertType)newType; alert.lowStockThreshold = th; // See if alert should be triggered if (alert.alertType == StockAlert.AlertType.outOfStock && stock.count == 0) { alert.triggered = true; } else if (alert.alertType == StockAlert.AlertType.lowStock && stock.count <= th) { alert.triggered = true; } else { alert.triggered = false; } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StockAlertExists(alert.id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index")); }
public async Task <IActionResult> Edit(int id, string itemName) { if (ModelState.IsValid) { try { User user = HttpContext.Items["user"] as User; var qOrder = from o in _context.Order where o.userId == user.id && o.stockId == id select o; var qStock = from s in _context.Stock where s.userId == user.id && s.id == id select s; var qAlert = from a in _context.StockAlert where a.userId == user.id && a.stockId == id select a; IEnumerable <Order> orders = await qOrder.ToListAsync(); Stock stock = await qStock.SingleAsync(); stock.itemName = itemName; StockAlert alert = await qAlert.SingleAsync(); alert.itemName = itemName; foreach (var o in orders) { o.itemName = stock.itemName; } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StockExists(id)) { Console.WriteLine("foo"); return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(RedirectToAction("Index")); }
private async Task CreateStockAlertAsync(Stock stock, string alertType, string priority) { StockAlert prevStockAlert = _context.StockAlerts.Where(sa => sa.StockID == stock.StockID).Where(sa => sa.AlertType == alertType).FirstOrDefault(); if (prevStockAlert == null) { StockAlert stockAlert = new StockAlert { StockID = stock.StockID, Stock = stock, AlertType = alertType, Priority = priority, CreatedDate = DateTime.Now, Status = "NEW" }; _context.StockAlerts.Add(stockAlert); await _context.SaveChangesAsync(); } }
public void GenerateAlert() { if (busy) return; busy = true; alertDefs.Clear(); alertDefs.Add(cciEx); alertDefs.Add(barAbove); alertDefs.Add(barBelow); alertDefs.Add(trailHL); alertDefs.Add(ResistanceBroken); alertDefs.Add(trailHLSR); try { var stockList = this.WatchLists.Find(wl => wl.Name == "Alert").StockList; if (AlertDetectionStarted != null) { if (this.InvokeRequired) { this.Invoke(this.AlertDetectionStarted, stockList.Count); } else { this.AlertDetectionStarted(stockList.Count); } } StockAlertLog stockAlertLog = StockAlertLog.Instance; DateTime lookBackDate = DateTime.Today.AddDays(-7); foreach (string stockName in stockList) { if (AlertDetectionProgress != null) { if (this.InvokeRequired) { this.Invoke(this.AlertDetectionProgress, stockName); } else { this.AlertDetectionProgress(stockName); } } if (!this.StockDictionary.ContainsKey(stockName)) continue; StockSerie stockSerie = this.StockDictionary[stockName]; StockDataProviderBase.DownloadSerieData(Settings.Default.RootFolder, stockSerie); if (!stockSerie.Initialise()) continue; StockSerie.StockBarDuration previouBarDuration = stockSerie.BarDuration; lock (alertDefs) { foreach (StockAlertDef alertDef in alertDefs) { stockSerie.BarDuration = alertDef.BarDuration; var values = stockSerie.GetValues(alertDef.BarDuration); for (int i = values.Count - 2; i > 0 && values[i].DATE > lookBackDate; i--) { var dailyValue = values.ElementAt(i); if (stockSerie.MatchEvent(alertDef, i)) { StockAlert stockAlert = new StockAlert(alertDef, dailyValue.DATE, stockSerie.StockName, dailyValue.CLOSE, stockSerie.GetValues(StockSerie.StockBarDuration.Daily).Last().CLOSE); if (!stockAlertLog.Alerts.Any(a => a == stockAlert)) { if (this.InvokeRequired) { this.Invoke(new Action(() => stockAlertLog.Alerts.Insert(0, stockAlert))); } else { stockAlertLog.Alerts.Insert(0, stockAlert); } } } } } } stockSerie.BarDuration = previouBarDuration; } stockAlertLog.Save(); if (this.AlertDetected != null) { this.Invoke(this.AlertDetected); } StockSplashScreen.CloseForm(true); } finally { busy = false; } }