public async Task <IActionResult> RangeAdd(string id) { try { // Get original weather station data var form = HttpContext.Request.Form; WeatherStation entity_new = await db.weatherStation.byIdAsync(id); // Instance the new range entity YieldRange range = new YieldRange() { crop = getId(form["crop"]), label = form["label"], lower = int.Parse(form["lower"]), upper = int.Parse(form["upper"]) }; await db.weatherStation.addRangeAsync(entity_new, range); await writeEventAsync(id + "range add: " + range.crop.ToString() + "-" + range.label + "-" + range.lower.ToString() + "-" + range.upper.ToString(), LogEvent.upd); return(RedirectToAction("Range", new { id = id })); } catch (Exception ex) { await writeExceptionAsync(ex); return(RedirectToAction("Range", new { id = id })); } }
/// <summary> /// Method Construct /// </summary> /// <param name="range">Yield Range entity</param> /// <param name="cp">Crops' list</param> public CropYieldRange(YieldRange range, List <Crop> cp) { this.crop = range.crop; this.label = range.label; this.lower = range.lower; this.upper = range.upper; this.crop_name = cp.SingleOrDefault(p => p.id == range.crop).name; }
/// <summary> /// Method that add a new setup to a crop /// </summary> /// <param name="entity">Weather station with the new range</param> /// <param name="range">New range to add to the weather station</param> /// <returns>True if the entity is updated, false otherwise</returns> public async Task <bool> addRangeAsync(WeatherStation entity, YieldRange range) { List <YieldRange> allRanges = entity.ranges.ToList(); allRanges.Add(range); entity.ranges = allRanges; var result = await collection.UpdateOneAsync(Builders <WeatherStation> .Filter.Eq("_id", entity.id), Builders <WeatherStation> .Update.Set("ranges", entity.ranges)); return(result.ModifiedCount > 0); }