Ejemplo n.º 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "customers/{id}")]
            HttpRequest req,
            string id,
            [Inject] ICustomerService customerService,
            ILogger logger
            )
        {
            logger.LogInformation($"Calling {nameof(DeleteCustomerFunction)}");

            if (!int.TryParse(id, out var customerId))
            {
                logger.LogError($"Error: Invalid request, id must be integer: {id}");
                return(new BadRequestObjectResult($"Invalid request, id must be integer: {id}"));
            }

            var deleteCustomerRequest = new DeleteCustomerRequest(customerId);
            var isValid = deleteCustomerRequest.Validate();

            if (!isValid)
            {
                logger.LogError("Error: Invalid request");
                return(new BadRequestObjectResult("Invalid request"));
            }

            var operationResult = await customerService.DeleteCustomerAsync(deleteCustomerRequest).ConfigureAwait(false);

            if (operationResult.Status)
            {
                return(new OkObjectResult("Customer deleted successfully"));
            }

            return(new BadRequestObjectResult(operationResult.Message));
        }
Ejemplo n.º 2
0
        public async Task <OperationResult <DeleteCustomerResponse> > DeleteCustomerAsync(DeleteCustomerRequest request)
        {
            var isValid = request.Validate();

            if (!isValid)
            {
                _logger.LogError($"Error. Invalid request: {JsonConvert.SerializeObject(request)}");
                return(OperationResult <DeleteCustomerResponse> .Failure("Invalid request"));
            }

            var operationResult = await _customerRepository.DeleteCustomerAsync(request.CustomerId).ConfigureAwait(false);

            if (!operationResult.Status)
            {
                _logger.LogError($"Error: {operationResult.Message}");
                return(OperationResult <DeleteCustomerResponse> .Failure("Cannot delete customer, error occured"));
            }

            return(OperationResult <DeleteCustomerResponse> .Success(new DeleteCustomerResponse(request.CustomerId)));
        }
Ejemplo n.º 3
0
        public async Task <OperationResult> DeleteCustomerAsync(DeleteCustomerRequest request)
        {
            var isValid = request.Validate();

            if (!isValid)
            {
                _logger.LogError("Error: Invalid request");

                return(OperationResult.Failure("Invalid request"));
            }

            var operationResult = await _customerRepository.DeleteCustomerAsync(request.Id).ConfigureAwait(false);

            if (operationResult.Status)
            {
                _logger.LogInformation("Customer deleted successfully");

                return(OperationResult.Success());
            }

            _logger.LogError("Error: Cannot delete customer");

            return(OperationResult.Failure("Cannot delete customer"));
        }