Skip to content

Commit

Permalink
#66 Delete Meal
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusMeyer13 committed Mar 3, 2020
1 parent 647ce03 commit 46639b6
Showing 1 changed file with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,73 @@ public static async Task<IActionResult> CreateMeal(
return actionResult;
}

/// <summary>
/// Deletes the meal by identifier.
/// </summary>
/// <param name="req">The req.</param>
/// <param name="id">The identifier.</param>
/// <param name="blob">The BLOB.</param>
/// <param name="log">The log.</param>
/// <param name="context">The context.</param>
/// <returns>IActionResult.</returns>
[ProducesResponseType(typeof(ErrorModel), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status200OK)]
[FunctionName("DeleteMealById")]
public static IActionResult DeleteMealById(
[HttpTrigger(AuthorizationLevel.Function, "delete", Route = "meals/{id}")] HttpRequest req,
string id,
[Blob("meals/{id}.json", FileAccess.ReadWrite, Connection = "StorageSend")] CloudBlockBlob blob,
ILogger log,
ExecutionContext context)
{
Guid correlationId = Util.ReadCorrelationId(req.Headers);
var methodName = MethodBase.GetCurrentMethod().Name;
var trace = new Dictionary<string, string>();
EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName);
IActionResult actionResult = null;

using (log.BeginScope("Method:{methodName} CorrelationId:{CorrelationId} Label:{Label}", methodName, correlationId.ToString(), context.InvocationId.ToString()))
{
try
{
trace.Add(Constants.ButlerCorrelationTraceName, correlationId.ToString());
trace.Add("id", id);

if (blob != null)
{
Task task = blob.DeleteIfExistsAsync();
task.Wait();

actionResult = new OkResult();
log.LogInformation(correlationId, $"'{methodName}' - success", trace);
}
}
catch (Exception e)
{
trace.Add(string.Format("{0} - {1}", methodName, "rejected"), e.Message);
trace.Add(string.Format("{0} - {1} - StackTrace", methodName, "rejected"), e.StackTrace);
log.LogInformation(correlationId, $"'{methodName}' - rejected", trace);
log.LogError(correlationId, $"'{methodName}' - rejected", trace);

ErrorModel errorModel = new ErrorModel()
{
CorrelationId = correlationId,
Details = e.StackTrace,
Message = e.Message,
};
actionResult = new BadRequestObjectResult(errorModel);
}
finally
{
log.LogTrace(eventId, $"'{methodName}' - finished");
log.LogInformation(correlationId, $"'{methodName}' - finished", trace);
}
}

return actionResult;
}

/// <summary>
/// Updates the meal by identifier.
/// </summary>
Expand Down

0 comments on commit 46639b6

Please sign in to comment.