public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log, [Inject] IApprenticeshipService apprenticeshipService) { using (var streamReader = new StreamReader(req.Body)) { var requestBody = await streamReader.ReadToEndAsync(); Apprenticeship fromBody = null; Apprenticeship persisted = null; try { fromBody = JsonConvert.DeserializeObject <Apprenticeship>(requestBody); } catch (Exception e) { return(new BadRequestObjectResult(e)); } try { persisted = (Apprenticeship)await apprenticeshipService.AddApprenticeship(fromBody); } catch (Exception e) { return(new InternalServerErrorObjectResult(e)); } return(new CreatedResult(persisted.id.ToString(), persisted)); } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log, [Inject] IApprenticeshipService apprenticeshipService) { using (var streamReader = new StreamReader(req.Body)) { var requestBody = await streamReader.ReadToEndAsync(); // Default to processing in parallel if query parameter is not supplied var processInParallel = !req.Query["parallel"].ToString().Equals("false", StringComparison.OrdinalIgnoreCase); var apprenticeships = JsonConvert.DeserializeObject <IEnumerable <Apprenticeship> >(requestBody); if (processInParallel) { using (var throttle = new SemaphoreSlim(MaxConcurrency)) { await Task.WhenAll(apprenticeships.Select(async a => { await throttle.WaitAsync(); try { await apprenticeshipService.AddApprenticeship(a); } finally { throttle.Release(); } })); } } else { foreach (var app in apprenticeships) { await apprenticeshipService.AddApprenticeship(app); } } return(new OkResult()); } }
public async Task <IActionResult> Summary(SummaryViewModel theModel) { int UKPRN; if (_session.GetInt32("UKPRN") != null) { UKPRN = _session.GetInt32("UKPRN").Value; } else { return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." })); } var Apprenticeship = _session.GetObject <Apprenticeship>("selectedApprenticeship"); var mode = _session.GetObject <ApprenticeshipMode>("ApprenticeshipMode"); if (mode == ApprenticeshipMode.EditYourApprenticeships) { var result = await _apprenticeshipService.UpdateApprenticeshipAsync(Apprenticeship); if (result.IsSuccess) { return(RedirectToAction("Index", "ProviderApprenticeships", new { apprenticeshipId = result.Value.id, message = "You edited " + result.Value.ApprenticeshipTitle })); } else { //Action needs to be decided if failure return(RedirectToAction("Summary", "Apprenticeships")); } } else { Apprenticeship.id = Guid.NewGuid(); var result = await _apprenticeshipService.AddApprenticeship(Apprenticeship); if (result.IsSuccess) { return(RedirectToAction("Complete", "Apprenticeships")); } else { //Action needs to be decided if failure return(RedirectToAction("Summary", "Apprenticeships")); } } }