Walkthrough Generated Code Delete

BasicCRUD Part 2

Now let us walk through the generated code for delete. It does not use any input api models since the delete happens by the Id. We will accept the id in the controller action.

We have the delete customer method. We also have HTTP delete and it's deleted by ID.

public partial class CustomerController : FlexControllerBridge
    {
        [HttpDelete()]
        [Route("DeleteCustomer/{id}")]
        public async Task<IActionResult> DeleteCustomer(string id)
        {
            CommandResult cmdResult = null;

            DeleteCustomerParams deleteParams = new DeleteCustomerParams();

            FlexHostContextInfoBridge hostContextInfo = new FlexHostContextInfoBridge();
            hostContextInfo.Populate<IFlexHostHttpContextAccesorBridge>(_appHttpContextAccessor);

            deleteParams.HostContextInfo = hostContextInfo;
            deleteParams.Id = id;

            cmdResult = await ProcessCustomerService.DeleteCustomer(deleteParams);
            
            if (cmdResult.Status != Status.Success)
            {
                ModelState.ComposeFlexBadRequestError(cmdResult.Errors());
                return ControllerContext.ReturnFlexBadRequestError();
            }
            return StatusCode(200, cmdResult.result);
        }
    }

So DeleteCustomerParams are created here and the Id of customer is assigned to the DeleteCustomerParams. Lets look at the ProcessServices.

public partial class ProcessCustomerService : IProcessCustomerService
    {
        
        /// <summary>
        /// YourRemarksForMethod
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<CommandResult> DeleteCustomer(DeleteCustomerParams param)
        {
            DeleteCustomerDataPacket packet = _flexHost.GetFlexiFlowDataPacket<DeleteCustomerDataPacket>();
            packet.InputParams = param;

            try
            {
                FlexiBusinessRuleSequenceBase<DeleteCustomerDataPacket> sequence = _flexHost.GetFlexiBusinessRuleSequence<DeleteCustomerSequence, DeleteCustomerDataPacket>();

                await FlexiBusinessRule.Run(sequence, packet);
            }
            catch (FlexiFlowAbnormalTerminationException ex)
            {
                packet.AddError("requesterror", ex);
                return new CommandResult(Status.Failed, packet.Errors());
            }

            if (packet.HasError)
            {
                return new CommandResult(Status.Failed, packet.Errors());
            }
            else
            {
                DeleteCustomerCommand cmd = new DeleteCustomerCommand
                {
                    Id = param.Id,
                    HostContextInfo = param.HostContextInfo

                };

                await ProcessCommand(cmd);

                CommandResult cmdResult = new CommandResult(Status.Success);
                DeleteCustomerResultModel outputResult = new DeleteCustomerResultModel();
                cmdResult.result = outputResult;
                return cmdResult;
            }
        }
    }
    public class DeleteCustomerResultModel : FlexOutputAPIModelForDelete
    {
    }

Then the command is sent in a bus and then the command is being received or handle in the Handler.

public partial class ProcessCustomerService : IProcessCustomerService
    {
        private async Task ProcessCommand(DeleteCustomerCommand cmd)
        {
            await _bus.Send(cmd);

        }
    }

Lets look at the Domain model.

public virtual Customer DeleteCustomer(DeleteCustomerCommand cmd)
        {
            Guard.AgainstNull("Customer model cannot be empty", cmd);

            this.Id = cmd.Id;
            this.SetDeleted();

            //Set your appropriate SetDeleted for the inner object here

            return this;
        }

The model is filled with the Id and Entity framework will take care of the deletion of the object by id when its set to SetDeleted().

Last updated