示例#1
0
        private async Task SendToStoredProc(ChangeFeedMessageModel documentModel, ILogger log)
        {
            _loggerHelper.LogMethodEnter(log);

            if (documentModel == null)
            {
                _loggerHelper.LogInformationMessage(log, CorrelationId, "document model is null");
                return;
            }

            var          resourceName  = GetResourceName(documentModel);
            var          commandText   = "Change_Feed_Insert_Update_" + resourceName;
            const string parameterName = "@Json";

            if (string.IsNullOrWhiteSpace(resourceName))
            {
                _loggerHelper.LogInformationMessage(log, CorrelationId, "resource Name is null");
                return;
            }

            try
            {
                _loggerHelper.LogInformationMessage(log, CorrelationId, "attempting to insert document into SQL");
                await _sqlServerProvider.UpsertResource(documentModel.Document, log, commandText, parameterName);
            }
            catch (Exception ex)
            {
                _loggerHelper.LogException(log, Guid.NewGuid(), "Error when trying to insert & update change feed request into SQL", ex);
                throw;
            }

            _loggerHelper.LogMethodExit(log);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", 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());
            }

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

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

            _loggerHelper.LogMethodExit(_logger);

            return(new OkObjectResult(pathModel));
        }
        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));
            }
        }
示例#4
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", 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 dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req);

            if (string.IsNullOrEmpty(dssCorrelationId))
            {
                log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header");
            }

            if (!Guid.TryParse(dssCorrelationId, out var correlationGuid))
            {
                log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid");
                correlationGuid = Guid.NewGuid();
            }

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

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

            PageRegions pageRegionValue = (PageRegions)pageRegion;

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

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

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

            loggerHelper.LogMethodExit(log);

            return(regionModel != null
                ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(regionModel, "id", nameof(Models.Region.DocumentId)))
                : httpResponseMessageHelper.NoContent());
        }
示例#5
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;
            }
        }
示例#6
0
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "paths")] HttpRequest req)
        {
            _loggerHelper.LogMethodEnter(_logger);

            var correlationId = _httpRequestHelper.GetOrCreateDssCorrelationId(req);

            _loggerHelper.LogInformationMessage(_logger, correlationId, $"Attempting to get all paths");
            var result = await _pathService.GetAll();

            _loggerHelper.LogMethodExit(_logger);

            return(new OkObjectResult(result));
        }
示例#7
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 static HttpResponseMessage Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = ApiDefRoute)]
            HttpRequest req,
            ILogger logger,
            [Inject] ISwaggerDocumentGenerator swaggerDocumentGenerator,
            [Inject] ILoggerHelper loggerHelper
            )
        {
            string ApiSuffix = Environment.GetEnvironmentVariable("ApiSuffix");
            string ApiTitle  = "Composite Paths " + ApiSuffix;

            loggerHelper.LogMethodEnter(logger);

            var swagger = swaggerDocumentGenerator.GenerateSwaggerDocument(req, ApiTitle, ApiDescription, ApiDefinitionName, ApiVersion, Assembly.GetExecutingAssembly());

            loggerHelper.LogMethodExit(logger);

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(swagger)
            });
        }
示例#9
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)));
            }
        }
示例#10
0
        public static async Task <HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "paths/{path}/regions")]
            HttpRequest req,
            ILogger log,
            string path,
            [Inject] ILoggerHelper loggerHelper,
            [Inject] IHttpRequestHelper httpRequestHelper,
            [Inject] IHttpResponseMessageHelper httpResponseMessageHelper,
            [Inject] IJsonHelper jsonHelper,
            [Inject] IRegionService regionService
            )
        {
            loggerHelper.LogMethodEnter(log);

            // validate the parameters are present
            var dssCorrelationId = httpRequestHelper.GetDssCorrelationId(req);

            if (string.IsNullOrEmpty(dssCorrelationId))
            {
                log.LogInformation($"Unable to locate '{nameof(dssCorrelationId)}' in request header");
            }

            if (!Guid.TryParse(dssCorrelationId, out var correlationGuid))
            {
                log.LogInformation($"Unable to parse '{nameof(dssCorrelationId)}' to a Guid");
                correlationGuid = Guid.NewGuid();
            }

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

            var pathRegex = new Regex(@"^[A-Za-z0-9.,-_]*$");

            if (path.Length > 100 || !pathRegex.IsMatch(path))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Invalid value in request for '{nameof(path)}'");
                return(httpResponseMessageHelper.BadRequest());
            }

            Models.Region regionRequest;

            try
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, "Attempt to get resource from body of the request");
                regionRequest = await httpRequestHelper.GetResourceFromRequest <Models.Region>(req);

                if (regionRequest == null)
                {
                    loggerHelper.LogInformationMessage(log, correlationGuid, "Missing body in req");
                    return(httpResponseMessageHelper.UnprocessableEntity());
                }
            }
            catch (JsonException ex)
            {
                loggerHelper.LogError(log, correlationGuid, "Unable to retrieve body from req", ex);
                return(httpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (path != regionRequest.Path)
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.Path)}' does not match resource path value");
                return(httpResponseMessageHelper.BadRequest());
            }

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

            const string PlaceMarkerStub = "{0}";
            string       regionEndpoint  = regionRequest.RegionEndpoint;

            if (regionEndpoint.Contains(PlaceMarkerStub))
            {
                // this is allowable, so replace with a valid string to permit the Uri.IsWellFormedUriString to check the resulting string
                regionEndpoint = regionEndpoint.Replace(PlaceMarkerStub, "valid");
            }

            if (!Uri.IsWellFormedUriString(regionEndpoint, UriKind.Absolute))
            {
                loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.RegionEndpoint)}' is not a valid absolute Uri");
                return(httpResponseMessageHelper.BadRequest());
            }

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

            if (!string.IsNullOrEmpty(regionRequest.OfflineHtml))
            {
                var htmlDoc = new HtmlDocument();

                htmlDoc.LoadHtml(regionRequest.OfflineHtml);

                if (htmlDoc.ParseErrors.Any())
                {
                    loggerHelper.LogInformationMessage(log, correlationGuid, $"Request value for '{nameof(regionRequest.OfflineHtml)}' contains malformed HTML");
                    return(httpResponseMessageHelper.BadRequest());
                }
            }

            loggerHelper.LogInformationMessage(log, correlationGuid, string.Format("Attempting to create region {0}", regionRequest.DocumentId));
            var createdRegion = await regionService.CreateAsync(regionRequest);

            loggerHelper.LogMethodExit(log);

            return(createdRegion != null
                ? httpResponseMessageHelper.Ok(jsonHelper.SerializeObjectAndRenameIdProperty(createdRegion, "id", nameof(Models.Region.DocumentId)))
                : httpResponseMessageHelper.NoContent());
        }
示例#11
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));
            }
        }