Refaktorisierung von CRUDControllerBase zur Nutzung des Result-Musters für eine sauberere Fehlerbehandlung und Codestruktur.
This commit is contained in:
parent
f5c0a76f30
commit
efb573b945
@ -50,16 +50,19 @@ namespace DigitalData.Core.API
|
||||
[HttpPost]
|
||||
public virtual async Task<IActionResult> Create(TCreateDto createDto)
|
||||
{
|
||||
var result = await _service.CreateAsync(createDto);
|
||||
if (result.IsSuccess)
|
||||
return await _service.CreateAsync(createDto).ThenAsync<TId, IActionResult>(
|
||||
Success: id =>
|
||||
{
|
||||
var createdResource = new { Id = result.Data };
|
||||
var createdResource = new { Id = id };
|
||||
var actionName = nameof(GetById);
|
||||
var routeValues = new { id = createdResource.Id };
|
||||
return CreatedAtAction(actionName, routeValues, createdResource);
|
||||
}
|
||||
|
||||
return BadRequest(result);
|
||||
},
|
||||
Fail: (messages, notices) =>
|
||||
{
|
||||
_logger.LogNotice(notices);
|
||||
return BadRequest(messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -70,12 +73,13 @@ namespace DigitalData.Core.API
|
||||
[HttpGet("{id}")]
|
||||
public virtual async Task<IActionResult> GetById([FromRoute] TId id)
|
||||
{
|
||||
var result = await _service.ReadByIdAsync(id);
|
||||
if (result.IsSuccess)
|
||||
return await _service.ReadByIdAsync(id).ThenAsync(
|
||||
Success: Ok,
|
||||
Fail: IActionResult (messages, notices) =>
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
return NotFound(result);
|
||||
_logger.LogNotice(notices);
|
||||
return NotFound(messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -85,12 +89,13 @@ namespace DigitalData.Core.API
|
||||
[HttpGet]
|
||||
public virtual async Task<IActionResult> GetAll()
|
||||
{
|
||||
var result = await _service.ReadAllAsync();
|
||||
if (result.IsSuccess)
|
||||
return await _service.ReadAllAsync().ThenAsync(
|
||||
Success: Ok,
|
||||
Fail: IActionResult (messages, notices) =>
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
return NotFound(result);
|
||||
_logger.LogNotice(notices);
|
||||
return NotFound(messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -101,12 +106,13 @@ namespace DigitalData.Core.API
|
||||
[HttpPut]
|
||||
public virtual async Task<IActionResult> Update(TUpdateDto updateDto)
|
||||
{
|
||||
var result = await _service.UpdateAsync(updateDto);
|
||||
if (result.IsSuccess)
|
||||
return await _service.UpdateAsync(updateDto).ThenAsync(
|
||||
Success: Ok,
|
||||
Fail: IActionResult (messages, notices) =>
|
||||
{
|
||||
return Ok(result);
|
||||
}
|
||||
return BadRequest(result);
|
||||
_logger.LogNotice(notices);
|
||||
return BadRequest(messages);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -117,13 +123,13 @@ namespace DigitalData.Core.API
|
||||
[HttpDelete("{id}")]
|
||||
public virtual async Task<IActionResult> Delete([FromRoute] TId id)
|
||||
{
|
||||
var result = await _service.DeleteAsyncById(id);
|
||||
if (result.IsSuccess)
|
||||
return await _service.DeleteAsyncById(id).ThenAsync(
|
||||
Success: Ok,
|
||||
Fail: IActionResult (messages, notices) =>
|
||||
{
|
||||
return Ok(result);
|
||||
_logger.LogNotice(notices);
|
||||
return BadRequest(messages);
|
||||
});
|
||||
}
|
||||
return BadRequest(result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user