public void Setup()
        {
            _goal = Substitute.For <Models.Goal>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/" +
                            $"Interactions/aa57e39e-4469-4c79-a9e9-9cb4ef410382/" +
                            $"ActionPlans/d5369b9a-6959-4bd3-92fc-1583e72b7e51/" +
                            $"Goals")
            };

            _log                        = Substitute.For <ILogger>();
            _resourceHelper             = Substitute.For <IResourceHelper>();
            _httpRequestMessageHelper   = Substitute.For <IHttpRequestMessageHelper>();
            _validate                   = Substitute.For <IValidate>();
            _postGoalHttpTriggerService = Substitute.For <IPostGoalHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
        }
コード例 #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "Customers/{customerId}/Interactions/{interactionId}/ActionPlans/{actionPlanId}/Goals/")] HttpRequestMessage req, ILogger log, string customerId, string interactionId, string actionPlanId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPostGoalHttpTriggerService goalPostService)
        {
            var touchpointId = httpRequestMessageHelper.GetTouchpointId(req);

            if (string.IsNullOrEmpty(touchpointId))
            {
                log.LogInformation("Unable to locate 'TouchpointId' in request header.");
                return(HttpResponseMessageHelper.BadRequest());
            }

            var ApimURL = httpRequestMessageHelper.GetApimURL(req);

            if (string.IsNullOrEmpty(ApimURL))
            {
                log.LogInformation("Unable to locate 'apimurl' in request header");
                return(HttpResponseMessageHelper.BadRequest());
            }

            log.LogInformation("Post Goal C# HTTP trigger function  processed a request. " + touchpointId);

            if (!Guid.TryParse(customerId, out var customerGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(customerGuid));
            }

            if (!Guid.TryParse(interactionId, out var interactionGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(interactionGuid));
            }

            if (!Guid.TryParse(actionPlanId, out var actionPlanGuid))
            {
                return(HttpResponseMessageHelper.BadRequest(actionPlanGuid));
            }

            Models.Goal goalRequest;

            try
            {
                goalRequest = await httpRequestMessageHelper.GetGoalFromRequest <Models.Goal>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

            if (goalRequest == null)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(req));
            }

            goalRequest.SetIds(customerGuid, actionPlanGuid, touchpointId);

            var errors = validate.ValidateResource(goalRequest, true);

            if (errors != null && errors.Any())
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(errors));
            }

            var doesCustomerExist = await resourceHelper.DoesCustomerExist(customerGuid);

            if (!doesCustomerExist)
            {
                return(HttpResponseMessageHelper.NoContent(customerGuid));
            }

            var isCustomerReadOnly = await resourceHelper.IsCustomerReadOnly(customerGuid);

            if (isCustomerReadOnly)
            {
                return(HttpResponseMessageHelper.Forbidden(customerGuid));
            }

            var doesInteractionExist = resourceHelper.DoesInteractionExistAndBelongToCustomer(interactionGuid, customerGuid);

            if (!doesInteractionExist)
            {
                return(HttpResponseMessageHelper.NoContent(interactionGuid));
            }

            var doesActionPlanExist = resourceHelper.DoesActionPlanExistAndBelongToCustomer(actionPlanGuid, interactionGuid, customerGuid);

            if (!doesActionPlanExist)
            {
                return(HttpResponseMessageHelper.NoContent(actionPlanGuid));
            }

            var goal = await goalPostService.CreateAsync(goalRequest);

            if (goal != null)
            {
                await goalPostService.SendToServiceBusQueueAsync(goal, ApimURL);
            }

            return(goal == null
                ? HttpResponseMessageHelper.BadRequest(customerGuid)
                : HttpResponseMessageHelper.Created(JsonHelper.SerializeObject(goal)));
        }