示例#1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            [Table("likes", Connection = "AccountStorage-Conn")] CloudTable cloudTable,
            CancellationToken cancellationToken,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            if (req.Headers.TryGetValue(FacebookTokenValidation.AccessToken, out var accessToken))
            {
                log.LogInformation("Validating fb token");
                var fbUser = await FacebookTokenValidation.IsTokenValid(accessToken, cancellationToken);

                log.LogInformation($"Valid fb token, received userId : {fbUser.Id}");

                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 likeDto = JsonConvert.DeserializeObject <LikeDto>(body);
                log.LogInformation("Deserialized");

                var partitionKey = likeDto.PostId;
                var rowKey       = fbUser.Id;

                log.LogInformation($"Retrieving record by partitionKey: {partitionKey} and rowKey: {rowKey}");
                var likeRecord = await cloudTable.Get <TableEntity>(partitionKey, rowKey);

                if (likeRecord != null)
                {
                    log.LogInformation($"Deleting record :{partitionKey} - {rowKey}");
                    await cloudTable.ExecuteAsync(TableOperation.Delete(likeRecord));

                    log.LogInformation($"Deleted record :{partitionKey} - {rowKey}");
                }
                else
                {
                    log.LogInformation($"Not Found");
                    return(new NotFoundResult());
                }

                log.LogInformation("Done");
                return(new OkResult());
            }

            return(new BadRequestResult());
        }
示例#2
0
        private static async Task <IActionResult> InternalExecution(HttpRequest req, string postId, CloudTable collector,
                                                                    CancellationToken cancellationToken, ILogger log, HttpOperation httpMethod)
        {
            try
            {
                log.LogInformation("C# HTTP trigger function processed a request.");

                if (req.Headers.TryGetValue("access_token", out var accessToken))
                {
                    log.LogInformation("Validating fb token");
                    var fbUser = await FacebookTokenValidation.IsTokenValid(accessToken, cancellationToken);

                    log.LogInformation($"Valid fb token, received userId : {fbUser.Id}");

                    log.LogInformation($"Invoking property method to {httpMethod} http method");
                    switch (httpMethod)
                    {
                    case HttpOperation.Post:
                    case HttpOperation.Put:
                        return(await PostOrPutExecution(req, postId, collector, log, fbUser, httpMethod));

                    case HttpOperation.Delete:
                        return(await DeleteExecution(postId, collector, log, fbUser));

                    default:
                        return(new InternalServerErrorResult());
                    }
                }

                return(new UnauthorizedResult());
            }
            catch (HttpRequestException ex) when(ex.Message.Contains("401"))
            {
                return(new UnauthorizedResult());
            }
            catch (HttpRequestException ex) when(ex.Message.Contains("404") || ex.Message.Contains("not found", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new NotFoundResult());
            }
        }