try-catch zu CRUDControllerBase hinzugefügt

This commit is contained in:
Developer 02
2024-07-01 16:01:49 +02:00
parent 2cb80ec119
commit 7789605585

View File

@@ -45,6 +45,8 @@ namespace DigitalData.Core.API
/// <returns>A task that represents the asynchronous create operation. The task result contains the action result.</returns> /// <returns>A task that represents the asynchronous create operation. The task result contains the action result.</returns>
[HttpPost] [HttpPost]
public virtual async Task<IActionResult> Create(TCreateDto createDto) public virtual async Task<IActionResult> Create(TCreateDto createDto)
{
try
{ {
return await _service.CreateAsync(createDto).ThenAsync<TId, IActionResult>( return await _service.CreateAsync(createDto).ThenAsync<TId, IActionResult>(
Success: id => Success: id =>
@@ -60,6 +62,12 @@ namespace DigitalData.Core.API
return BadRequest(messages); return BadRequest(messages);
}); });
} }
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
/// <summary> /// <summary>
/// Retrieves an entity by its identifier. /// Retrieves an entity by its identifier.
@@ -68,6 +76,8 @@ namespace DigitalData.Core.API
/// <returns>A task that represents the asynchronous read operation. The task result contains the action result.</returns> /// <returns>A task that represents the asynchronous read operation. The task result contains the action result.</returns>
[HttpGet("{id}")] [HttpGet("{id}")]
public virtual async Task<IActionResult> GetById([FromRoute] TId id) public virtual async Task<IActionResult> GetById([FromRoute] TId id)
{
try
{ {
return await _service.ReadByIdAsync(id).ThenAsync( return await _service.ReadByIdAsync(id).ThenAsync(
Success: Ok, Success: Ok,
@@ -77,6 +87,12 @@ namespace DigitalData.Core.API
return NotFound(messages); return NotFound(messages);
}); });
} }
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
/// <summary> /// <summary>
/// Retrieves all entities. /// Retrieves all entities.
@@ -84,6 +100,8 @@ namespace DigitalData.Core.API
/// <returns>A task that represents the asynchronous read-all operation. The task result contains the action result.</returns> /// <returns>A task that represents the asynchronous read-all operation. The task result contains the action result.</returns>
[HttpGet] [HttpGet]
public virtual async Task<IActionResult> GetAll() public virtual async Task<IActionResult> GetAll()
{
try
{ {
return await _service.ReadAllAsync().ThenAsync( return await _service.ReadAllAsync().ThenAsync(
Success: Ok, Success: Ok,
@@ -93,6 +111,12 @@ namespace DigitalData.Core.API
return NotFound(messages); return NotFound(messages);
}); });
} }
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
/// <summary> /// <summary>
/// Updates an existing entity based on the provided DTO. /// Updates an existing entity based on the provided DTO.
@@ -101,6 +125,8 @@ namespace DigitalData.Core.API
/// <returns>A task that represents the asynchronous update operation. The task result contains the action result.</returns> /// <returns>A task that represents the asynchronous update operation. The task result contains the action result.</returns>
[HttpPut] [HttpPut]
public virtual async Task<IActionResult> Update(TUpdateDto updateDto) public virtual async Task<IActionResult> Update(TUpdateDto updateDto)
{
try
{ {
return await _service.UpdateAsync(updateDto).ThenAsync( return await _service.UpdateAsync(updateDto).ThenAsync(
Success: Ok, Success: Ok,
@@ -110,6 +136,12 @@ namespace DigitalData.Core.API
return BadRequest(messages); return BadRequest(messages);
}); });
} }
catch(Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
/// <summary> /// <summary>
/// Deletes an entity by its identifier. /// Deletes an entity by its identifier.
@@ -118,6 +150,8 @@ namespace DigitalData.Core.API
/// <returns>A task that represents the asynchronous delete operation. The task result contains the action result.</returns> /// <returns>A task that represents the asynchronous delete operation. The task result contains the action result.</returns>
[HttpDelete("{id}")] [HttpDelete("{id}")]
public virtual async Task<IActionResult> Delete([FromRoute] TId id) public virtual async Task<IActionResult> Delete([FromRoute] TId id)
{
try
{ {
return await _service.DeleteAsyncById(id).ThenAsync( return await _service.DeleteAsyncById(id).ThenAsync(
Success: Ok, Success: Ok,
@@ -127,5 +161,11 @@ namespace DigitalData.Core.API
return BadRequest(messages); return BadRequest(messages);
}); });
} }
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
} }
} }