Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Refactor CreateOrder() MVC Controller #40

Closed
CESARDELATORRE opened this issue Feb 10, 2017 · 3 comments
Closed

Refactor CreateOrder() MVC Controller #40

CESARDELATORRE opened this issue Feb 10, 2017 · 3 comments
Assignees

Comments

@CESARDELATORRE
Copy link
Collaborator

CESARDELATORRE commented Feb 10, 2017

Need to refactor. Our current implementation is:

    [Route("new")]
    [HttpPost]
    public async Task<IActionResult> CreateOrder([FromBody]CreateOrderCommand createOrderCommand)
    {
        if (createOrderCommand.CardTypeId == 0)
        {
            createOrderCommand.CardTypeId = 1;
        }
        createOrderCommand.BuyerIdentityGuid = _identityService.GetUserIdentity();

        var result = await _mediator.SendAsync(createOrderCommand);
        if (result)
        {
            return Ok();
        }
        return BadRequest();
    }
  1. We need to move the obtention of the identity to the CommandHandler, so the Command could be Immutable as we won't need to update the Identity into the Command object
    _identityService.GetUserIdentity();
    This issue is related to issue Make immutable Commands and remove behavior from the Command classes - i.e. CreateOrderCommand #36 - Make immutable Commands and remove behavior from the Command classes - i.e. CreateOrderCommand #36

  2. The validations of the command could be done based on a MediatR Decorator Pattern, instead of a control sentence within the controller. See:
    Validating Commands with the Decorator Pattern
    http://codeopinion.com/validating-commands/

So the final controller should look like this:

    [Route("new")]
    [HttpPost]
    public async Task<IActionResult> CreateOrder([FromBody]CreateOrderCommand createOrderCommand)
    {
        var result = await _mediator.SendAsync(createOrderCommand);
        if (result)
        {
            return Ok();
        }
        return BadRequest();
    }
@ardalis
Copy link
Collaborator

ardalis commented Feb 10, 2017

Agreed.

@eiximenis
Copy link
Contributor

Done in commit 67ab115

In branch vs2017

@CESARDELATORRE
Copy link
Collaborator Author

Solved. Closing it.

erjain pushed a commit that referenced this issue Aug 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants