Flexbase
  • Flexbase
  • Introduction
  • #JoyofCoding with Flexbase
  • Core Concepts
    • Flexbase
    • Flexstudio
      • Commands & Events
  • Thinking in Flexbase
    • Features and Modules
    • Designing APIs using Flexbase Control Flow
  • Sample Application
  • Solution Structure
    • Getting Started
      • Application
        • Introduction to Solution
        • Demo - Solution Generation
        • Solution Structure Overview
        • Architecture Overview
        • Clean architecture layer
        • Common Configurations
      • Features
        • Understanding Feature
        • Use Case
        • First Feature - Add Customer
        • Second Feature - Update Customer
        • Third Feature - Delete Customer
        • Query Features
  • Input Output Models
  • Messages
  • Domain
  • Pre Bus
  • Post Bus
  • Subscribers
  • Bus Gamma
  • Persistence
  • Hosting
  • Startup Code - WebAPI
  • Startup Code - Background Job
  • Endpoint - AppSettings
  • Bridge
  • Basic CRUD - Insert & Update
  • Introduction
  • View Generated API Definition in Swagger
  • Walkthrough Generated Code Insert
  • Walkthrough Generated Code Query GetById
  • Domain Model
  • Migration
  • AppSettings
  • Bus : How Basic Routing Config Works
  • INSERT : Input API Model
  • INSERT : Attribute Validation
  • INSERT : Plugin Validation
  • INSERT : Mapper
  • INSERT : Controller
  • INSERT : Services
  • INSERT : PostBus Handler
  • INSERT : PostBus Subscriber
  • INSERT : Demo In Action
  • QUERY : Output API Model
  • QUERY : Build and Fetch Pattern
  • QUERY : Demo In Action
  • Basic CRUD 2
    • Introduction
    • View Generated API Definition In Swagger
  • Walkthrough Generated Code Update
  • Walkthrough Generated Code Delete
  • Walkthrough Generated Code GetList
  • Walkthrough Generated Code Get Paged List
  • Update In Action
  • Delete In Action
  • GetList In Action
  • GetPagedList In Action
  • Konarch
Powered by GitBook
On this page

INSERT : Controller

BasicCRUD Insert Query with Bridge

PreviousINSERT : MapperNextINSERT : Services

Last updated 4 years ago

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.

[HttpPost]
        [Route("AddCustomer")]
        [ProducesResponseType(typeof(BadRequestResult), 400)]
        [ProducesResponseType(typeof(string), 201)]
        public async Task<IActionResult> AddCustomer([FromBody]AddCustomerInputAPIModel value)
        {
            if (value == null)
            {
                ModelState.AddModelError("requesterror", $"{nameof(AddCustomerInputAPIModel)} can not be empty");
                _logger.LogDebug($"{nameof(AddCustomerInputAPIModel)} can not be empty" + DateTime.Now.ToString());
                return ControllerContext.ReturnFlexBadRequestError();
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            CommandResult cmdResult = null;

            //Set the values for the parameter
            AddCustomerParams addParams = new AddCustomerParams();

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

            addParams.Model = value;

            cmdResult = await ProcessCustomerService.AddCustomer(addParams);
            
            if (cmdResult.Status != Status.Success)
            {
                ModelState.ComposeFlexBadRequestError(cmdResult.Errors());
                return ControllerContext.ReturnFlexBadRequestError();
            }
            return StatusCode(201, cmdResult.result);
        }

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.