Flexbase
Search…
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.
1
public partial class CustomerController : FlexControllerBridge
2
{
3
[HttpDelete()]
4
[Route("DeleteCustomer/{id}")]
5
public async Task<IActionResult> DeleteCustomer(string id)
6
{
7
CommandResult cmdResult = null;
8
9
DeleteCustomerParams deleteParams = new DeleteCustomerParams();
10
11
FlexHostContextInfoBridge hostContextInfo = new FlexHostContextInfoBridge();
12
hostContextInfo.Populate<IFlexHostHttpContextAccesorBridge>(_appHttpContextAccessor);
13
14
deleteParams.HostContextInfo = hostContextInfo;
15
deleteParams.Id = id;
16
17
cmdResult = await ProcessCustomerService.DeleteCustomer(deleteParams);
18
19
if (cmdResult.Status != Status.Success)
20
{
21
ModelState.ComposeFlexBadRequestError(cmdResult.Errors());
22
return ControllerContext.ReturnFlexBadRequestError();
23
}
24
return StatusCode(200, cmdResult.result);
25
}
26
}
Copied!
So DeleteCustomerParams are created here and the Id of customer is assigned to the DeleteCustomerParams. Lets look at the ProcessServices.
1
public partial class ProcessCustomerService : IProcessCustomerService
2
{
3
4
/// <summary>
5
/// YourRemarksForMethod
6
/// </summary>
7
/// <param name="param"></param>
8
/// <returns></returns>
9
public async Task<CommandResult> DeleteCustomer(DeleteCustomerParams param)
10
{
11
DeleteCustomerDataPacket packet = _flexHost.GetFlexiFlowDataPacket<DeleteCustomerDataPacket>();
12
packet.InputParams = param;
13
14
try
15
{
16
FlexiBusinessRuleSequenceBase<DeleteCustomerDataPacket> sequence = _flexHost.GetFlexiBusinessRuleSequence<DeleteCustomerSequence, DeleteCustomerDataPacket>();
17
18
await FlexiBusinessRule.Run(sequence, packet);
19
}
20
catch (FlexiFlowAbnormalTerminationException ex)
21
{
22
packet.AddError("requesterror", ex);
23
return new CommandResult(Status.Failed, packet.Errors());
24
}
25
26
if (packet.HasError)
27
{
28
return new CommandResult(Status.Failed, packet.Errors());
29
}
30
else
31
{
32
DeleteCustomerCommand cmd = new DeleteCustomerCommand
33
{
34
Id = param.Id,
35
HostContextInfo = param.HostContextInfo
36
37
};
38
39
await ProcessCommand(cmd);
40
41
CommandResult cmdResult = new CommandResult(Status.Success);
42
DeleteCustomerResultModel outputResult = new DeleteCustomerResultModel();
43
cmdResult.result = outputResult;
44
return cmdResult;
45
}
46
}
47
}
48
public class DeleteCustomerResultModel : FlexOutputAPIModelForDelete
49
{
50
}
Copied!
Then the command is sent in a bus and then the command is being received or handle in the Handler.
1
public partial class ProcessCustomerService : IProcessCustomerService
2
{
3
private async Task ProcessCommand(DeleteCustomerCommand cmd)
4
{
5
await _bus.Send(cmd);
6
7
}
8
}
Copied!
Lets look at the Domain model.
1
public virtual Customer DeleteCustomer(DeleteCustomerCommand cmd)
2
{
3
Guard.AgainstNull("Customer model cannot be empty", cmd);
4
5
this.Id = cmd.Id;
6
this.SetDeleted();
7
8
//Set your appropriate SetDeleted for the inner object here
9
10
return this;
11
}
Copied!
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 modified 1yr ago
Copy link