public async Task Run([CosmosDBTrigger(
                                   DatabaseName,
                                   CollectionName,
                                   ConnectionStringSetting = ConnectionString,
                                   LeaseCollectionName = LeaseCollectionName,
                                   LeaseCollectionPrefix = LeaseCollectionPrefix,
                                   CreateLeaseCollectionIfNotExists = true
                                   )] IReadOnlyList <Document> documents,
                              ILogger log)
        {
            try
            {
                foreach (var document in documents)
                {
                    var changeFeedMessageModel = new ChangeFeedMessageModel()
                    {
                        Document   = document,
                        IsCustomer = true
                    };

                    _loggerHelper.LogInformationMessage(log, Guid.NewGuid(), string.Format("Attempting to send document id: {0} to service bus queue", document.Id));
                    await _serviceBusClient.SendChangeFeedMessageAsync(document, changeFeedMessageModel);
                }
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(log, Guid.NewGuid(), "Error when trying to send message to service bus queue", ex);
            }
        }
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "paths")] HttpRequest req)
        {
            _loggerHelper.LogMethodEnter(_logger);

            var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req);

            var body = await req.GetBodyAsync <PathModel>();

            if (body == null || body.Value == null)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PayloadMalformed);
                return(new BadRequestResult());
            }

            if (!body.IsValid)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, string.Concat(Message.ValidationFailed, " ", body.ValidationResults));
                return(new BadRequestObjectResult(body.ValidationResults));
            }

            try
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, "Attempting to register path");
                var registeredPath = await _pathService.Register(body.Value);

                _loggerHelper.LogMethodExit(_logger);
                return(new CreatedResult(registeredPath.Path, registeredPath));
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(_logger, correlationId, ex);
                return(new UnprocessableEntityObjectResult(ex));
            }
        }
Beispiel #3
0
        public async Task <bool> UpsertResource(Document document, ILogger log, string commandText, string parameterName)
        {
            try
            {
                _loggerHelper.LogMethodEnter(log);

                await UpsertResource(document.ToString(), log, commandText, parameterName);

                _loggerHelper.LogMethodExit(log);
                return(true);
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(log, _correlationId, ex);
                throw;
            }
        }
Beispiel #4
0
        public async Task <bool> SendToAzureSql(Message queueItem, ILogger log)
        {
            if (queueItem == null)
            {
                return(false);
            }

            string body;

            try
            {
                body = Encoding.UTF8.GetString(queueItem.Body);
            }
            catch (Exception e)
            {
                _loggerHelper.LogException(log, CorrelationId, "unable to get string from queue Item body", e);
                throw;
            }

            if (string.IsNullOrEmpty(body))
            {
                return(false);
            }

            ChangeFeedMessageModel documentModel;

            try
            {
                documentModel = JsonConvert.DeserializeObject <ChangeFeedMessageModel>(body);
            }
            catch (JsonException e)
            {
                _loggerHelper.LogException(log, CorrelationId, "unable to deserialize document model", e);
                throw;
            }

            if (documentModel == null)
            {
                return(false);
            }

            await SendToStoredProc(documentModel, log);

            return(true);
        }
Beispiel #5
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "paths/{path}")] HttpRequest req,
            string path)
        {
            _loggerHelper.LogMethodEnter(_logger);

            var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req);

            if (string.IsNullOrEmpty(path))
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.UnableToLocatePathInQueryString);
                return(new BadRequestResult());
            }

            var body = await req.GetBodyAsync <PathModel>();

            if (body == null || body.Value == null)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PayloadMalformed);
                return(new BadRequestResult());
            }

            if (!body.IsValid)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.ValidationFailed);
                return(new BadRequestObjectResult(body.ValidationResults));
            }

            _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get path for {path}");
            var currentPath = await _pathService.Get(path);

            if (currentPath == null)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PathDoesNotExist);
                return(new NoContentResult());
            }

            try
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get update path for {path}");
                await _pathService.Update(body.Value);

                _loggerHelper.LogMethodExit(_logger);
                return(new OkResult());
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(_logger, correlationId, ex);
                return(new UnprocessableEntityObjectResult(ex));
            }
        }
        public async System.Threading.Tasks.Task RunAsync(
            [ServiceBusTrigger("%QueueName%", Connection = "ServiceBusConnectionString")] Message queueItem,
            ILogger log)
        {
            var correlationId = Guid.NewGuid();

            if (queueItem == null)
            {
                loggerHelper.LogInformationMessage(log, correlationId, "Brokered Message cannot be null");
                return;
            }

            try
            {
                _changeFeedQueueProcessorService.CorrelationId = correlationId;
                await _changeFeedQueueProcessorService.SendToAzureSql(queueItem, log);
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, "Unable to send document to sql", ex);
                throw;
            }
        }
Beispiel #7
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "paths/{path}/regions/{pageRegion}")]
            HttpRequest req,
            ILogger log,
            string path,
            int pageRegion,
            [Inject] ILoggerHelper loggerHelper,
            [Inject] IHttpRequestHelper httpRequestHelper,
            [Inject] IHttpResponseMessageHelper httpResponseMessageHelper,
            [Inject] IJsonHelper jsonHelper,
            [Inject] IRegionService regionService
            )
        {
            loggerHelper.LogMethodEnter(log);

            // validate the parameters are present
            var correlationId = httpRequestHelper.GetOrCreateDssCorrelationId(req);

            if (string.IsNullOrEmpty(path))
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Missing value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            if (pageRegion == 0 || !Enum.IsDefined(typeof(PageRegions), pageRegion))
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Missing/invalid value in request for '{nameof(pageRegion)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            PageRegions pageRegionValue = (PageRegions)pageRegion;

            JsonPatchDocument <Region> regionPatch;

            try
            {
                regionPatch = await httpRequestHelper.GetResourceFromRequest <JsonPatchDocument <Region> >(req);

                if (regionPatch == null)
                {
                    loggerHelper.LogException(log, correlationId, "Request body is empty", null);
                    return(httpResponseMessageHelper.BadRequest());
                }
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.BadRequest());
            }

            loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to get Region {pageRegionValue} for Path {path}");

            var currentRegion = await regionService.GetAsync(path, pageRegionValue);

            if (currentRegion == null)
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Region does not exist for {pageRegionValue} for Path {path}");
                return(httpResponseMessageHelper.NoContent());
            }

            try
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to apply patch to {path} region {pageRegionValue}");
                regionPatch?.ApplyTo(currentRegion);
                var validationResults = currentRegion.Validate(new ValidationContext(currentRegion));

                if (validationResults.Any())
                {
                    loggerHelper.LogInformationMessage(log, correlationId, "Validation Failed");
                    return(httpResponseMessageHelper.UnprocessableEntity(validationResults.ToList()));
                }
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.BadRequest());
            }

            try
            {
                loggerHelper.LogInformationMessage(log, correlationId, $"Attempting to update path {path}");
                var patchedRegion = await regionService.ReplaceAsync(currentRegion);

                loggerHelper.LogMethodExit(log);
                return(httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(patchedRegion, "id", nameof(Models.Region.DocumentId))));
            }
            catch (Exception ex)
            {
                loggerHelper.LogException(log, correlationId, ex);
                return(httpResponseMessageHelper.UnprocessableEntity(new JsonException(ex.Message, ex)));
            }
        }
Beispiel #8
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "paths/{path}")] HttpRequest req,
            string path)
        {
            _loggerHelper.LogMethodEnter(_logger);

            var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req);

            if (string.IsNullOrWhiteSpace(path))
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.UnableToLocatePathInQueryString);
                return(new BadRequestResult());
            }

            JsonPatchDocument <PathModel> pathPatch = null;

            try
            {
                var requestBody = await req.ReadAsStringAsync();

                pathPatch = JsonConvert.DeserializeObject <JsonPatchDocument <PathModel> >(requestBody);
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(_logger, correlationId, ex);
                return(new BadRequestResult());
            }

            _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get path {path}'");
            var currentPath = await _pathService.Get(path);

            if (currentPath == null)
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, Message.PathDoesNotExist);
                return(new NoContentResult());
            }

            try
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to apply patch to {path}");
                pathPatch.ApplyTo(currentPath);
                var validationResults = currentPath.Validate(new ValidationContext(currentPath));

                if (validationResults.Any())
                {
                    _loggerHelper.LogInformationMessage(_logger, correlationId, Message.ValidationFailed);
                    return(new BadRequestObjectResult(validationResults));
                }
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(_logger, correlationId, ex);
                return(new BadRequestResult());
            }

            try
            {
                _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to update path {path}");
                await _pathService.Update(currentPath);

                _loggerHelper.LogMethodExit(_logger);
                return(new OkObjectResult(currentPath));
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(_logger, correlationId, ex);
                return(new UnprocessableEntityObjectResult(ex));
            }
        }