public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "GetStop/{Id}")] HttpRequest req, string Id, ILogger log) { log.LogInformation("GET - Get Stop requested"); try { if (!RIPAAuthorization.ValidateAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult()) { return(new UnauthorizedResult()); } } catch (Exception ex) { log.LogError(ex.Message); return(new UnauthorizedResult()); } if (!string.IsNullOrEmpty(Id)) { var response = await _stopCosmosDbService.GetStopAsync(Id); if (response != null) { return(new OkObjectResult(response)); } } return(new BadRequestObjectResult("Not found")); }
private async Task <Stop> GetStop(ILogger log, string id, string runId) { try { log.LogInformation($"GetStop: {id} : {runId}"); return(await _stopCosmosDbService.GetStopAsync(id)); } catch (Exception ex) { log.LogError($"Exception: {ex} --> occurred during GetStop with id {id} : {runId}"); return(null); } }
public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "put", Route = "PutStop/{Id}")] Common.Models.Stop stop, HttpRequest req, string Id, ILogger log) { log.LogInformation($"PUT - Put Stop requested, ID: {Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}"); try { if (!RIPAAuthorization.ValidateUserOrAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult()) { return(new UnauthorizedResult()); } } catch (Exception ex) { log.LogError(ex.Message); return(new UnauthorizedResult()); } try { var objectId = await RIPAAuthorization.GetUserId(req, log); stop.EditStopOfficerId = (await _userProfileCosmosDbService.GetUserProfileAsync(objectId)).OfficerId; } catch (Exception ex) { log.LogError(ex.Message); return(new BadRequestObjectResult("User profile was not found")); } if (string.IsNullOrEmpty(Id)) { return(new BadRequestObjectResult("Stop Id cannot be empty or null")); } if (stop.OfficerId.Length != 9) { return(new BadRequestObjectResult("Officer ID must be 9 char")); } if (stop.Location.City == null) { return(new BadRequestObjectResult("City is required")); } if (stop.Status == null) { stop.Status = SubmissionStatus.Unsubmitted.ToString(); } stop.Ori = Environment.GetEnvironmentVariable("ORI"); //What is an Originating Agency Identification (ORI) Number? A nine-character identifier assigned to an agency. Agencies must identify their ORI Number... bool isDuplicate = await _stopCosmosDbService.CheckForDuplicateStop(stop.Id, stop.Ori, stop.OfficerId, stop.Date, stop.Time); if (isDuplicate) { log.LogError($"This appears to be a duplicate Stop: ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}"); return(new BadRequestObjectResult("This appears to be a duplicate Stop")); } stop.IsEdited = Id == "0" ? false : true; try { if (Id != "0") { //Protect the submission history Common.Models.Stop editingStop = await _stopCosmosDbService.GetStopAsync(Id); editingStop.Id = $"{stop.Id}-{DateTime.UtcNow:yyyyMMddHHmmss}"; await _stopAuditCosmosDbService.UpdateStopAuditAsync(editingStop.Id, editingStop); log.LogInformation($"Saving stop audit ID: {editingStop.Id}"); stop.ListSubmission = editingStop.ListSubmission; } } catch (Exception ex) { log.LogError($"Failed getting stop to protect submission history, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}"); return(new BadRequestObjectResult("Failed getting stop submission history")); } int retryAttemps = GetRetrySetting("RetryAttemps", 3); int retrywait = GetRetrySetting("RetryWait", 1000); while (retryAttemps > 0) { try { stop.Id = Id; if (stop.Id == "0") { await _stopCosmosDbService.AddStopAsync(stop); } else { if (!RIPAAuthorization.ValidateAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult()) { return(new UnauthorizedResult()); } await _stopCosmosDbService.UpdateStopAsync(stop); } log.LogInformation($"PUT - saved stop, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}"); return(new OkObjectResult(stop)); } catch (Exception exception) { log.LogError($"Failed to insert/update stop, attempt counter: {retryAttemps}, ID: {Id}, SID: {stop.Id}, OID: {stop.OfficerId}, DATE: {stop.Date}, TIME: {stop.Time}", exception.GetBaseException()); } await Task.Delay(retrywait); retryAttemps--; } return(new BadRequestObjectResult("The maximum number of attemps to save the STOP was exceeded")); }