Flexbase
Search…
INSERT : Controller
BasicCRUD Insert Query with Bridge
Now let us look at the control and action, for our add customer.
We have the controller created. So in the Constructor, we inject all the dependencies that we need in our controller, we can have our own independence and for the add customer since it was create it created the post method and the route is by default at customer.
1
[HttpPost]
2
[Route("AddCustomer")]
3
[ProducesResponseType(typeof(BadRequestResult), 400)]
4
[ProducesResponseType(typeof(string), 201)]
5
public async Task<IActionResult> AddCustomer([FromBody]AddCustomerInputAPIModel value)
6
{
7
if (value == null)
8
{
9
ModelState.AddModelError("requesterror", quot;{nameof(AddCustomerInputAPIModel)} can not be empty");
10
_logger.LogDebug(quot;{nameof(AddCustomerInputAPIModel)} can not be empty" + DateTime.Now.ToString());
11
return ControllerContext.ReturnFlexBadRequestError();
12
}
13
14
if (!ModelState.IsValid)
15
{
16
return BadRequest(ModelState);
17
}
18
19
CommandResult cmdResult = null;
20
21
//Set the values for the parameter
22
AddCustomerParams addParams = new AddCustomerParams();
23
24
FlexHostContextInfoBridge hostContextInfo = new FlexHostContextInfoBridge();
25
hostContextInfo.Populate<IFlexHostHttpContextAccesorBridge>(_appHttpContextAccessor);
26
addParams.HostContextInfo = hostContextInfo;
27
28
addParams.Model = value;
29
30
cmdResult = await ProcessCustomerService.AddCustomer(addParams);
31
32
if (cmdResult.Status != Status.Success)
33
{
34
ModelState.ComposeFlexBadRequestError(cmdResult.Errors());
35
return ControllerContext.ReturnFlexBadRequestError();
36
}
37
return StatusCode(201, cmdResult.result);
38
}
Copied!
Then we do the basic validations, Create a AddCustomerParams as we explained earlier, then in the ContextInfo, the AccessorInfo is mapped. Then it is passed to be filled with the params value and passed in onto the services and any result that comes from the services is written back to the user of the API. If it is successful, then finally it will return the 201 status code with the result, here one interesting thing is what with the result.
Last modified 1yr ago
Copy link