public async Task <int> AddOrUpdateMetricBarHeightsAsync( BarHeightsForm <MetricBarHeight> barHeightsForm, CancellationToken ct) { var query = _context.MetricBarHeights .Where(e => e.eventId == barHeightsForm.eventId).ToArray(); _context.MetricBarHeights.RemoveRange(query); foreach (MetricBarHeight bh in barHeightsForm.Heights) { _context.MetricBarHeights .Add(new MBarHeightEntity { eventId = barHeightsForm.eventId, HeightNum = bh.HeightNum, Meters = bh.Meters }); } var created = await _context.SaveChangesAsync(ct); if (created < 1) { throw new InvalidOperationException ("Could not Add/Update bar heights"); } return(0); }
public async Task <IActionResult> AddOrUpdateMetricBarHeightsAsync( [FromBody] BarHeightsForm <MetricBarHeight> barHeightsForm, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(new ApiError(ModelState))); } var evt = await _eventService.GetEventByIdAsync(barHeightsForm.eventId, ct); if (evt.Params.EventType[0] != 'V') { return(BadRequest("Event type not vertical")); } if (evt.Params.MeasurementType[0] != 'M') { return(BadRequest("Measurement type not metric")); } await _eventService.AddOrUpdateMetricBarHeightsAsync( barHeightsForm, ct); return(Created(Url.Link(nameof(EventsController.GetEventByIdAsync), new { barHeightsForm.eventId }), null)); }