public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "comment/{id}")] HttpRequest req, [FromQuery] string id, CancellationToken ct, ILogger log) { if (!await _authorization.IsAuthorized(req, Authorization.Roles.Approve, Authorization.Roles.Admin)) { return(new UnauthorizedResult()); } if (string.IsNullOrWhiteSpace(id)) { return(new BadRequestResult()); } var table = await _tableClientFactory.GetTable(Tables.Comments); var comment = await table.QueryAsync <Comment>(c => c.RowKey == id).FirstOrDefaultAsync(); if (comment == null) { return(new OkResult()); } var result = await table.DeleteEntityAsync(comment.PartitionKey, comment.RowKey); if (!result.IsError && comment.Approved) { comment.Approved = false; await _notifier.NotifyCommentPublished(_mapper.MapEvent(comment, "Deleted"), log, ct); } return(result.IsError ? new OkResult() : new StatusCodeResult(result.Status)); }
public async Task <IActionResult> ApproveComment( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "comment/approve/{id}")] HttpRequest req, [FromQuery] string id, CancellationToken ct, ILogger log) { if (!await _authorization.IsAuthorized(req, Authorization.Roles.Approve, Authorization.Roles.Admin)) { return(new UnauthorizedResult()); } if (string.IsNullOrWhiteSpace(id)) { return(new BadRequestResult()); } var table = await _tableClientFactory.GetTable(Tables.Comments); var comment = await table.QueryAsync <Comment>(c => c.RowKey == id).FirstOrDefaultAsync(); if (comment == null) { return(new BadRequestResult()); } if (comment.Approved) { return(new OkResult()); } comment.Approved = true; var response = await table.UpdateEntityAsync(comment, Azure.ETag.All, Azure.Data.Tables.TableUpdateMode.Replace, cancellationToken : ct); if (response.IsError) { log.LogError("Failed to approve comment {id}. Table client failed with status {status}", id, response.Status); } await _notifier.NotifyCommentPublished(_mapper.MapEvent(comment, "Approved"), log, ct); return(new OkResult()); }