示例#1
0
        public void Setup()
        {
            _address      = Substitute.For <Models.Address>();
            _addressPatch = Substitute.For <Models.AddressPatch>();

            _request = new HttpRequestMessage()
            {
                Content    = new StringContent(string.Empty),
                RequestUri =
                    new Uri($"http://localhost:7071/api/Customers/7E467BDB-213F-407A-B86A-1954053D3C24/Addressess/1e1a555c-9633-4e12-ab28-09ed60d51cb3")
            };

            _log                            = Substitute.For <ILogger>();
            _resourceHelper                 = Substitute.For <IResourceHelper>();
            _validate                       = Substitute.For <IValidate>();
            _httpRequestMessageHelper       = Substitute.For <IHttpRequestMessageHelper>();
            _patchAddressHttpTriggerService = Substitute.For <IPatchAddressHttpTriggerService>();
            _httpRequestMessageHelper.GetTouchpointId(_request).Returns("0000000001");
            _httpRequestMessageHelper.GetApimURL(_request).Returns("http://localhost:7071/");
        }
示例#2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "Customers/{customerId}/Addresses/{addressId}")] HttpRequestMessage req, ILogger log, string customerId, string addressId,
                                                           [Inject] IResourceHelper resourceHelper,
                                                           [Inject] IHttpRequestMessageHelper httpRequestMessageHelper,
                                                           [Inject] IValidate validate,
                                                           [Inject] IPatchAddressHttpTriggerService addressPatchService)
        {
            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("Patch Address C# HTTP trigger function  processed a request. By Touchpoint " + touchpointId);

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

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

            AddressPatch addressPatchRequest;

            try
            {
                addressPatchRequest = await httpRequestMessageHelper.GetAddressFromRequest <AddressPatch>(req);
            }
            catch (JsonException ex)
            {
                return(HttpResponseMessageHelper.UnprocessableEntity(ex));
            }

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

            addressPatchRequest.LastModifiedTouchpointId = touchpointId;

            var errors = validate.ValidateResource(addressPatchRequest, false);

            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 addressForCustomer = await addressPatchService.GetAddressForCustomerAsync(customerGuid, addressGuid);

            if (addressForCustomer == null)
            {
                return(HttpResponseMessageHelper.NoContent(addressGuid));
            }

            var patchedAddress = addressPatchService.PatchResource(addressForCustomer, addressPatchRequest);

            if (patchedAddress == null)
            {
                return(HttpResponseMessageHelper.NoContent(addressGuid));
            }

            var updatedAddress = await addressPatchService.UpdateCosmosAsync(patchedAddress, addressGuid);

            if (updatedAddress != null)
            {
                await addressPatchService.SendToServiceBusQueueAsync(updatedAddress, customerGuid, ApimURL);
            }

            return(updatedAddress == null?
                   HttpResponseMessageHelper.BadRequest(addressGuid) :
                       HttpResponseMessageHelper.Ok(JsonHelper.SerializeObject(updatedAddress)));
        }