private static async Task <IActionResult> PostOrPutExecution(HttpRequest req, string postId, CloudTable collector, ILogger log, FacebookProfileDto fbUser, HttpOperation httpMethod) { log.LogInformation($"Trigger for {httpMethod} http method"); log.LogInformation($"Reading body"); var body = await new StreamReader(req.Body).ReadToEndAsync(); if (string.IsNullOrWhiteSpace(body)) { log.LogWarning($"Empty Body"); return(new BadRequestResult()); } log.LogInformation($"Received body : {body}, trying to deserialize"); var comment = JsonConvert.DeserializeObject <CommentDto>(body); var partitionKey = postId; var rowKey = $"{fbUser.Id}"; log.LogInformation( $"Deserialized, trying insert or replace comment, partitionKey: {partitionKey}, rowKey: {rowKey}"); var tableEntity = new CommentTable { PartitionKey = partitionKey, RowKey = rowKey, Comment = comment.Comment, Title = comment.Title, ETag = "*" }; TableOperation operation = null; switch (httpMethod) { case HttpOperation.Put: log.LogInformation("Have chosen table operation Replace"); operation = TableOperation.Replace(tableEntity); break; case HttpOperation.Post: log.LogInformation("Have chosen table operation Insert"); operation = TableOperation.Insert(tableEntity); break; default: // Unsupported HTTP verbs = HTTP 405 (method not allowed) return(new StatusCodeResult(405)); } await collector.ExecuteAsync(operation); log.LogInformation("Done"); if (httpMethod == HttpOperation.Post) { return(new OkObjectResult(comment)); } return(new OkResult()); }
private static async Task <IActionResult> DeleteExecution(string postId, CloudTable collector, ILogger log, FacebookProfileDto fbUser) { var partitionKey = postId; var rowKey = $"{fbUser.Id}"; log.LogInformation( $"Trying delete comment, partitionKey: {partitionKey}, rowKey: {rowKey}"); await collector.ExecuteAsync( TableOperation.Delete(new CommentTable { PartitionKey = partitionKey, RowKey = rowKey, ETag = "*" })); log.LogInformation("Done"); return(new OkResult()); }