ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
866 stars 107 forks source link

[Question] What would be a good way to return CreatedAtAction 201 result? #99

Closed philipp985 closed 5 months ago

philipp985 commented 2 years ago

Hello, thank you for the good library and good ideas from concepts!

What would be an elegant / good way to solve this problem. I could not find anything in the calculation.

[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task<ActionResult<InventoryInspectionDoneDTO>> Create([FromBody] InventoryInspectionDoneCreateDTO dto)
{
       var resp = (await _inventoryInspectionsDoneService.CreateAsync(dto)).Map(x => 
       _mapper.Map<InventoryInspectionDoneDTO>(x));
       if (resp.IsSuccess)
          {
              return CreatedAtAction(nameof(GetById), new { id = resp.Value.Id }, resp.Value);
          }

   return this.ToActionResult(resp);
}

One idea would be write a custom extension but maybe there is chance to include it in this library.

Thanks for any ideas!

ardalis commented 1 year ago

I think some of what is in this PR could be applied: https://github.com/ardalis/Result/pull/92.

ardalis commented 5 months ago

We now have a Created result.